List.Single

D

T

The M Code Behind the Power Query M function List.Single

Overview of the List.Single Function

The List.Single function is used to extract a single value from a list of values. It takes a list as input and returns the first item in the list. If the list contains more than one item, it throws an error. Here is the syntax of the List.Single function:


List.Single(list as list) as any


The List.Single function takes a single argument, which is the list of values. The function returns the first item in the list as an any data type.

Understanding the M Code Behind List.Single

The M code behind the List.Single function is relatively simple. Here is the code:


list{0}


The code above extracts the first item in the list by using the indexing operator { } and specifying the index value 0. In M, arrays and lists are zero-indexed, meaning that the first item in the list has an index of 0, the second item has an index of 1, and so on.

The List.Single function is a wrapper function that encapsulates this M code and provides some additional functionality, such as error handling if the list contains more than one item.

Using the List.Single Function

Let's see an example of how to use the List.Single function in Power Query. Suppose we have a table that contains a column called "Fruit" that contains a list of fruit names. We want to extract the first fruit name from each row of the table. Here is how we can do it using the List.Single function:

1. Load the table into Power Query.

2. Select the "Fruit" column.

3. Click on the "Add Column" tab and select "Custom Column" from the dropdown menu.

4. In the "Custom Column" dialog box, enter a name for the new column.

5. In the "Custom Column" dialog box, enter the following formula:


=List.Single([Fruit])


6. Click "OK" to create the new column.

The formula above uses the List.Single function to extract the first fruit name from the "Fruit" column. The formula is applied to each row of the table, so it generates a new column with the first fruit name for each row.

The List.Single function is a powerful tool for extracting a single value from a list of values in Power Query. Its M code is relatively simple, but it provides some useful error handling features. By understanding how the List.Single function works, you can use it to streamline your data transformation and data preparation tasks in Power Query.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

Contact Us

    Subject

    Your Name (required)

    Company/Organisation

    Email (required)

    Telephone

    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)