Table.TransformColumnNames

D

T

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

What is the Table.TransformColumnNames Function?

The Table.TransformColumnNames function in Power Query is used to rename the columns of a table. It takes a table as input and returns a new table with the modified column names. The function is part of the M language, which is the underlying language used by Power Query. M is a functional programming language that is used to describe data transformations. The Table.TransformColumnNames function is just one of many functions available in M that can be used to manipulate data.

How to use the Table.TransformColumnNames Function

The Table.TransformColumnNames function takes two arguments: the table to be modified and a function that specifies how to modify the column names. The function that modifies the column names takes a text value as input and returns a text value. The function can be defined inline, or it can be defined as a separate function and passed as an argument to the Table.TransformColumnNames function.

Here is an example of how to use the Table.TransformColumnNames function to modify the column names of a table:


let

Source = Table.FromRecords({

[Column1 = 1, Column2 = "A"],

[Column1 = 2, Column2 = "B"],

[Column1 = 3, Column2 = "C"]

}),

NewColumnNames = Table.TransformColumnNames(

Source,

each "NewName_" & _

)

in

NewColumnNames


This code creates a table with two columns (Column1 and Column2) and three rows. It then uses the Table.TransformColumnNames function to modify the column names by appending the prefix “NewName_” to each column name. The result of this operation is a new table with modified column names.

The M Code Behind the Table.TransformColumnNames Function

The M code behind the Table.TransformColumnNames function is relatively simple. The function takes two arguments: the table to be modified and a function that specifies how to modify the column names. Here is the M code for the Table.TransformColumnNames function:


(Table as table, NameGenerator as function) as table =>

let

ColumnNames = Table.ColumnNames(Table),

NewColumnNames = List.Transform(ColumnNames, NameGenerator),

RenamedTable = Table.RenameColumns(Table, List.Zip({ColumnNames, NewColumnNames}))

in

RenamedTable


This code defines a function that takes two arguments and returns a table. The first argument is the table to be modified, and the second argument is a function that generates new column names. The function first retrieves the current column names using the Table.ColumnNames function. It then applies the NameGenerator function to each column name using the List.Transform function. The result of this operation is a list of new column names.

The function then uses the List.Zip function to create a list of pairs of old and new column names. It passes this list to the Table.RenameColumns function, which renames the columns of the table. Finally, the function returns the modified table.

The Table.TransformColumnNames function in Power Query is a powerful tool for modifying column names. It is part of the M language, which provides a powerful and flexible way to manipulate data. The M code behind the Table.TransformColumnNames function is relatively simple, but it provides a foundation for much more complex data transformations. Understanding the M code behind this function is essential for anyone who wants to use Power Query effectively.

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)