RowExpression.Row

D

T

The M Code Behind the Power Query M function RowExpression.Row

What is the RowExpression.Row function?

The RowExpression.Row function is a part of the Power Query M language, which is used to manipulate data in Power Query. The function is used to create a new row in a table by specifying the values for each column in the row. Here is the syntax of the RowExpression.Row function:


RowExpression.Row(column1, column2, …, columnN)


Where column1, column2, ..., columnN are the values for each column in the row.

How does the RowExpression.Row function work?

The RowExpression.Row function works by creating a new record with the specified column values. In Power Query, a table is represented as a list of records, where each record represents a row in the table. A record is a collection of key-value pairs, where the key is the name of the column and the value is the value for that column in the row.

When you call the RowExpression.Row function, it creates a new record with the specified column values and appends it to the end of the table. Here is an example of how to use the RowExpression.Row function:


let

Source = Table.FromRecords({

[ID = 1, Name = “John”, Age = 30],

[ID = 2, Name = “Jane”, Age = 25]

}),

NewRow = RowExpression.Row(3, “Bob”, 40),

Result = Table.InsertRows(Source, Table.RowCount(Source), {NewRow})

in

Result


In this example, we first create a table with two rows using the Table.FromRecords function. Then we create a new row with the values 3, "Bob", and 40 using the RowExpression.Row function. Finally, we insert the new row at the end of the table using the Table.InsertRows function.

What is the M code behind the RowExpression.Row function?

The M code behind the RowExpression.Row function is quite simple. Here is the M code:


let

CreateRecord = (columns) => Record.FromList(columns, Table.ColumnNames(Source)),

Source = Table.FromRows({}),

Result = (columns) => Table.InsertRows(Source, Table.RowCount(Source), {CreateRecord(columns)})

in

Result


The M code defines a function called CreateRecord, which takes a list of column values and creates a new record with the specified values. The function uses the Record.FromList function to create a new record with the specified values and the column names of the source table.

The M code also defines a source table using the Table.FromRows function, which creates an empty table with no rows. This table is used as a template for the new rows that will be created using the RowExpression.Row function.

Finally, the M code defines a function called Result, which takes a list of column values and inserts a new row with the specified values into the source table. The function uses the CreateRecord function to create a new record with the specified values and then uses the Table.InsertRows function to insert the new record into the source table.

The RowExpression.Row function is a powerful tool in Power Query, which allows you to create new rows in a table with ease. By understanding the M code behind the function, you can gain a deeper understanding of how Power Query works and how to create your own custom functions 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)