Table.TransformColumnNames
Renames every column with a function of its name.
Group: Table. Power Query M has it too: a query that uses it is written as in Power BI.
Syntax
Table.TransformColumnNames(table as table, nameGenerator as function, optional options as nullable record) as table
| Parameter | Type | Description |
|---|---|---|
table | table | The table whose columns to rename. |
nameGenerator | function | A function of a column's name (text) that gives its new name. |
options | nullable record optional | Options for the new names: MaxLength cuts each name to that many characters; Comparer says when two names are the same (Comparer.Ordinal when left out). |
Returns
table. The same rows and columns, under the new names.
Remarks
- Handy for names from a file: trim them, take out spaces or change their case in one step.
- Two columns that would get the same name are kept apart: the later one gets a number, as PRODUCT and PRODUCT1, and with
MaxLengththe number fits within the length.
Examples
Names in title case
Table.TransformColumnNames(#table({"product name", "unit price"}, {{"Chair", 50}}), Text.Proper)
| Product Name | Unit Price |
|---|---|
| Chair | 50 |
Without spaces
Table.TransformColumnNames(#table({"Product Name", "Unit Price"}, {{"Chair", 50}}), each Text.Replace(_, " ", ""))
| ProductName | UnitPrice |
|---|---|
| Chair | 50 |
Short names that would clash
Table.TransformColumnNames(#table({"Unit Price", "Unit Cost"}, {{50, 30}}), each _, [MaxLength = 4])
| Unit | Uni1 |
|---|---|
| 50 | 30 |
Both names cut to four letters would be Unit: the second becomes Uni1.
Related functions
Table.RenameColumns, Table.PrefixColumns, Text.Proper, Text.Trim.