Table.RenameColumns

D

T

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

Understanding the Table.RenameColumns Function

In Power Query, the Table.RenameColumns function is used to rename the columns of a table. The syntax of the function is as follows:


Table.RenameColumns(table as table, columnNames as list, newNames as list)


The function takes three arguments:

– `table`: this is the table whose columns you want to rename.

– `columnNames`: this is a list of the current column names.

– `newNames`: this is a list of the new column names.

For example, suppose we have a table with three columns named “Name”, “Age”, and “Gender”. We can rename these columns using the Table.RenameColumns function as follows:


Table.RenameColumns(

Source,

{"Name", "Age", "Gender"},

{"Full Name", "Years", "Sex"}

)


In this example, we are renaming the “Name” column to “Full Name”, the “Age” column to “Years”, and the “Gender” column to “Sex”.

The M Code Behind the Table.RenameColumns Function

The Table.RenameColumns function is a built-in function in Power Query, and its M code is not visible by default. However, you can view the M code by clicking on the “Advanced Editor” button on the “View” tab in Power Query.

When you click on the “Advanced Editor” button, the Power Query Editor will show you the M code for the current query. The M code for the Table.RenameColumns function looks like this:


let

Source = table,

RenamedColumns = Table.RenameColumns(Source, columnNames, newNames)

in

RenamedColumns


In this code, the `let` statement defines a variable named “Source”, which is assigned the value of the input table. The `Table.RenameColumns` function is then called with the `Source` variable, `columnNames`, and `newNames` as its arguments. The result of the function is assigned to a variable named “RenamedColumns”. Finally, the “RenamedColumns” variable is returned as the output of the query.

In conclusion, the Table.RenameColumns function in Power Query is a very useful function that simplifies the process of renaming columns in a table. The M code behind this function is simple and easy to understand. By understanding the M code behind the Table.RenameColumns function, you can customize the function to suit your specific needs. With Power Query, you can transform and analyze your data quickly and efficiently.

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)