Table.RenameColumns

Table.RenameColumns is a Power Query M function that renames columns in a table according to a given list of old and new column names. The function returns a new table with the columns renamed, handling missing columns based on an optional parameter.

Compatible with: Power BI Service Power BI Desktop Excel Microsoft 365

Syntax

Table.RenameColumns(
   table as table,
   renames as list,
   optional missingField as nullable number,
) as table
Argument Attribute Description
table
renames
missingField optional The MissingField.Type determines the function’s reaction to operations on missing columns. When omitted, it uses MissingField.Error and generates an error for missing columns. Alternatives include MissingField.UseNull, substituting null for missing columns, and MissingField.Ignore, which ignores missing columns.

Description

Performs the given renames to the columns in table table. A replacement operation renames consists of a list of two values, the old column name and new column name, provided in a list. If the column doesn’t exist, an exception is thrown unless the optional parameter missingField specifies an alternative (eg. MissingField.UseNull or MissingField.Ignore).

Examples

Replace the column name “CustomerNum” with “CustomerID” in the table.

// Output: Table.FromRecords( {[CustomerID = 1, Name = "Bob", Phone = "123-4567"]} )
Table.RenameColumns( 
    Table.FromRecords( {[CustomerNum = 1, Name = "Bob", Phone = "123-4567"]} ),
    {"CustomerNum", "CustomerID"}
 )

Replace the column name “CustomerNum” with “CustomerID” and “PhoneNum” with “Phone” in the table.

// Output: Table.FromRecords( {[CustomerID = 1, Name = "Bob", Phone = "123-4567"]} )
Table.RenameColumns( 
    Table.FromRecords( {[CustomerNum = 1, Name = "Bob", PhoneNum = "123-4567"]} ),
    {
        {"CustomerNum", "CustomerID"},
        {"PhoneNum", "Phone"}
    }
 )

Replace the column name “NewCol” with “NewColumn” in the table, and ignore if the column doesn’t exist.

// Output: Table.FromRecords( {[CustomerID = 1, Name = "Bob", Phone = "123-4567"]} )
Table.RenameColumns( 
    Table.FromRecords( {[CustomerID = 1, Name = "Bob", Phone = "123-4567"]} ),
    {"NewCol", "NewColumn"},
    MissingField.Ignore
 )

Learn more about Table.RenameColumns in the following articles:

BI Gorilla Youtube Channel

Last update: December 1, 2023 | Contribute » | Contributors: Rick de Groot
Microsoft documentation: https://learn.microsoft.com/en-us/powerquery-m/table-renamecolumns
© 2023 BI Gorilla. All rights reserved. Content derived from Microsoft documentation is property of Microsoft Corp.