Table.ReplaceRows

D

T

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

Understanding the Table.ReplaceRows Function

The Table.ReplaceRows function in Power Query allows you to replace a specific row or set of rows in a table with new ones. This function takes three arguments:





To use the Table.ReplaceRows function, you need to pass in the table you want to replace rows in, the row indices you want to replace, and a function that will create the new rows.

The M Code Behind the Table.ReplaceRows Function

When you use the Table.ReplaceRows function in Power Query, it generates M code to perform the replacement. The M code generated by the Table.ReplaceRows function is as follows:


Let's break down this code to understand what it does:




Creating the Replacerows Function

The replacerows function is responsible for creating the new rows that will replace the old ones. This function takes the old row as input and returns the new row.

To create the replacerows function, you can use the Record.AddField function to add or modify the fields in the old row. Here’s an example of the replacerows function that adds a new column to the table and modifies an existing column:


(replacerows) =>

let

newRow = Record.AddField(oldrow, "New Column", "New Value"),

updatedRow = Record.TransformFields(newRow, {{"Existing Column", each _ + 1}})

in

updatedRow


In this example, the replacerows function adds a new column called “New Column” with the value “New Value”. It also modifies an existing column called “Existing Column” by adding 1 to its value.

The Table.ReplaceRows function in Power Query is a powerful tool that allows you to replace specific rows in a table with new ones. To use this function, you need to provide the table you want to replace rows in, the row indices you want to replace, and a function that will create the new rows.

When you use the Table.ReplaceRows function, it generates M code that performs the replacement. The replacerows function is responsible for creating the new rows that will replace the old ones. By using the M code behind the Table.ReplaceRows function, you can write custom functions to replace rows in your table that meet your specific needs.

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)