Record.FromList

D

T

The M Code Behind the Power Query M function Record.FromList

What is `Record.FromList`?

First of all, let’s have a brief introduction to `Record.FromList`. This function takes a list of key-value pairs and returns a record. The list should be in the format of `{{key1, value1}, {key2, value2}, …}`. For example, `Record.FromList({{“Name”, “John”}, {“Age”, 30}})` will create a record with two fields, “Name” and “Age”, and their corresponding values, “John” and 30.

The M Code Behind `Record.FromList`

The M code of `Record.FromList` is quite simple. Here is the code:


Record.FromList = (list as list) =>

let

keys = List.Transform(list, each _{0}),

values = List.Transform(list, each _{1}),

result = [Record = Record.FromColumns({values}, keys)]

in

result[Record];


As you can see, the code takes a list as its input parameter, and it consists of three main steps.

Step 1: Extract keys

The first step is to extract all the keys from the input list. This is done by using the `List.Transform` function to iterate over each item in the list and return its first element (which is the key). The result is a list of keys.


keys = List.Transform(list, each _{0}),


Step 2: Extract values

The second step is to extract all the values from the input list. This is also done by using the `List.Transform` function to iterate over each item in the list and return its second element (which is the value). The result is a list of values.


values = List.Transform(list, each _{1}),


Step 3: Create the record

The final step is to create the record using the `Record.FromColumns` function. This function takes two parameters: a list of values and a list of column names (which are the keys). In our case, we pass the `values` list as the first parameter and the `keys` list as the second parameter. The result is a record with the fields named after the keys and their corresponding values.


result = [Record = Record.FromColumns({values}, keys)]


In this article, we have discussed the M code behind the `Record.FromList` function in Power Query. We have shown you the M code and explained how it works. We hope that this article has been helpful to you, and you now have a better understanding of how Power Query works behind the scenes.

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)