Single.From

D

T

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

What is the Single.From Function?

The Single.From function is used to extract a single value from a table. It takes a table as input and returns the value in the first row and first column of the table. If the table is empty or has more than one row or column, an error is returned.

Here is an example of the Single.From function in action:


let

Source = Table.FromRows({{1, “Red”}, {2, “Blue”}, {3, “Green”}}, {“ID”, “Color”}),

Result = Single.From(Source)

in

Result


In this example, we have a table with two columns: ID and Color. The Single.From function returns the value in the first row and first column of the table, which is 1.

The M Code Behind Single.From

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


(table as table) as any =>

let

result = try table{0}[Column1] otherwise null

in

result


Let's break down this code and see what each part does.

The first line of code defines the function and its input. The function takes a table as input and returns any data type.


(table as table) as any =>


The second line of code is an expression that defines a variable named "result". This variable is used to store the result of the function.


let

result = try table{0}[Column1] otherwise null


The third line of code is a try-except statement. This statement tries to access the first row and first column of the table using the indexing operator ({0}[Column1]). If this operation fails, the "otherwise" clause is executed, which returns a null value.


result = try table{0}[Column1] otherwise null


The final line of code returns the result variable.


in

result


The Single.From function is a powerful tool in Power Query that allows you to extract a single value from a table. The M code behind this function is relatively simple and can be customized to fit your specific needs. By understanding the M code behind the Single.From function, you can gain a deeper understanding of how Power Query works and how you can use it to manipulate your data with ease.

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)