Table.RenameColumns

Updated on

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

Table.RenameColumns applies renames of the form {old, new}. It renames columns in table as per renames list. Errors are raised for non-existent columns unless missingField specifies otherwise.

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:

Contribute » | Contributors: Rick de Groot
Microsoft documentation: https://learn.microsoft.com/en-us/powerquery-m/table-renamecolumns

2023-2024 © BI Gorilla. All rights are reserved. Information from Microsoft docs is property of Microsoft Corp. | Privacy Policy