Table.ExpandRecordColumn

D

T

The M Code Behind the Power Query M function Table.ExpandRecordColumn

This function is widely used in data transformations, and understanding its M code is essential for advanced users who want to harness the full power of Power Query. In this article, we will take a closer look at the M code behind Table.ExpandRecordColumn and provide some examples of its usage.

Understanding Table.ExpandRecordColumn

Table.ExpandRecordColumn is a Power Query M function that expands a nested record column into separate columns. It takes two arguments: the table to expand and the name of the column that contains the nested records. The nested records are then expanded into separate columns, and the original column is removed.

The syntax for Table.ExpandRecordColumn is as follows:


Table.ExpandRecordColumn(table as table, column as text) as table


Here, `table` is the source table that contains the nested record column, and `column` is the name of the nested record column. The function returns a new table with the expanded columns.

Examples of Table.ExpandRecordColumn

Let’s look at some examples of how Table.ExpandRecordColumn can be used in Power Query.

Example 1: Expanding a single nested record column

Suppose we have a table called `Sales` that contains a nested record column called `Product`. The `Product` column contains multiple properties such as `ProductID`, `ProductName`, `Price`, `Category`, etc. We want to expand the `Product` column into separate columns.

Here’s the M code to achieve this:


let

Source = Sales,

ExpandedProduct = Table.ExpandRecordColumn(Source, "Product"),

in

ExpandedProduct


In this code, we first define the source table as `Sales`. We then use the Table.ExpandRecordColumn function to expand the `Product` column into separate columns. The resulting table is then assigned to the variable `ExpandedProduct`.

Example 2: Expanding multiple nested record columns

Suppose we have a table called `Orders` that contains two nested record columns: `Product` and `Customer`. The `Product` column contains properties such as `ProductID`, `ProductName`, `Price`, `Category`, etc., and the `Customer` column contains properties such as `CustomerID`, `CustomerName`, `Address`, `City`, etc.

Here’s the M code to expand both columns into separate columns:


let

Source = Orders,

ExpandedProduct = Table.ExpandRecordColumn(Source, "Product"),

ExpandedCustomer = Table.ExpandRecordColumn(ExpandedProduct, "Customer")

in

ExpandedCustomer


In this code, we first define the source table as `Orders`. We then use the Table.ExpandRecordColumn function twice: once to expand the `Product` column and once to expand the `Customer` column. The resulting table is assigned to the variable `ExpandedCustomer`.

Table.ExpandRecordColumn is a powerful function in Power Query that allows users to expand nested record columns into separate columns. Understanding its M code is essential for users who want to perform advanced data transformations in Power Query. By following the examples provided in this article, users can gain a better understanding of how Table.ExpandRecordColumn works and how it can be used in their own data transformation projects.

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)