Table.RenameColumns Function in Power Query
The Table.RenameColumns function returns a table with the columns renamed as specified.
Syntax
The renames list has the syntax:
{
{OldColumnName1, NewColumnName1},
{OldColumnName2, NewColumnName2}
}
Example: Rename the column “Phone" to "Contact" and column "Company" to "Organization”.
Power Query M
let MyTable = Table.FromRecords( { [CustomerID = 1, Name = "Ashish", Phone = "123-4567", Company = "TCS"], [CustomerID = 2, Name = "Katrina", Phone = "987-6543", Company = "TCS"], [CustomerID = 3, Name = "Alia", Phone = "543-7890", Company = "TCS"], [CustomerID = 4, Name = "Vicky", Phone = "676-8479", Company = "TCS"], [CustomerID = 5, Name = "Mohini", Phone = "574-8864", Company = "TCS"], [CustomerID = 6, Name = "Meenakshi", Phone = "574-8864", Company = "TCS"], [CustomerID = 7, Name = "Esha", Phone = "574-8864", Company = "TCS"], [CustomerID = 8, Name = "Anjali", Phone = "574-8864", Company = "TCS"] } ), Return = Table.RenameColumns(MyTable, {{"Phone", "Contact"}, {"Company", "Organization"}}) in Return
The output of the above formula is shown below:
Case: Column Not exists The column name which we are going to rename if does not exist in the table, it will throw an error.
Power Query M
let MyTable = Table.FromRecords( { [CustomerID = 1, Name = "Ashish", Phone = "123-4567", Company = "TCS"], [CustomerID = 2, Name = "Katrina", Phone = "987-6543", Company = "TCS"], [CustomerID = 3, Name = "Alia", Phone = "543-7890", Company = "TCS"], [CustomerID = 4, Name = "Vicky", Phone = "676-8479", Company = "TCS"], [CustomerID = 5, Name = "Mohini", Phone = "574-8864", Company = "TCS"], [CustomerID = 6, Name = "Meenakshi", Phone = "574-8864", Company = "TCS"], [CustomerID = 7, Name = "Esha", Phone = "574-8864", Company = "TCS"], [CustomerID = 8, Name = "Anjali", Phone = "574-8864", Company = "TCS"] } ), Return = Table.RenameColumns(MyTable, {{"Mobile", "Contact"}, {"Company", "Organization"}}) in Return
The output of the above formula is shown below:
To catch this error, we can use the third argument of the function, i.e. MissingField.
Example: Replace the column name "Mobile" with "Contact" in the table and ignore if the column doesn't exist.
Power Query M
let MyTable = Table.FromRecords( { [CustomerID = 1, Name = "Ashish", Phone = "123-4567", Company = "TCS"], [CustomerID = 2, Name = "Katrina", Phone = "987-6543", Company = "TCS"], [CustomerID = 3, Name = "Alia", Phone = "543-7890", Company = "TCS"], [CustomerID = 4, Name = "Vicky", Phone = "676-8479", Company = "TCS"], [CustomerID = 5, Name = "Mohini", Phone = "574-8864", Company = "TCS"], [CustomerID = 6, Name = "Meenakshi", Phone = "574-8864", Company = "TCS"], [CustomerID = 7, Name = "Esha", Phone = "574-8864", Company = "TCS"], [CustomerID = 8, Name = "Anjali", Phone = "574-8864", Company = "TCS"] } ), Return = Table.RenameColumns(MyTable, {{"Mobile", "Contact"}, {"Company", "Organization"}}, MissingField.Ignore) in Return
The output of the above formula is shown below: