Forum Discussion
CPaceFOTL
9 years agoFrequent Visitor
Renaming Columns by Column Number
I have been working with the Power BI Desktop to create reports based on a CSV file. Things are going well; however, the headers in the CSV files have some "dynamic" headers which change from month ...
- 9 years ago
This function renames columns based on their position.
The second and third argument are lists, each with the same number of elements.
ColumnNumbers is 0-based, so the first column is 0.
let RenameColumns = (InputTable as table, ColumnNumbers as list, NewColumnNames as list) => let OldColumnNames = Table.ColumnNames(InputTable), Indexed = List.Zip({OldColumnNames, {0..-1+List.Count(OldColumnNames)}}), Filtered = List.Select(Indexed, each List.Contains(ColumnNumbers,_{1})), IndexRemoved = List.Transform(Filtered, each _{0}), RenameList = List.Zip({IndexRemoved,NewColumnNames}), RenamedColumns = Table.RenameColumns(InputTable, RenameList) in RenamedColumns in RenameColumns
Anonymous
6 years agoNot applicable
Here's how to rename the first column in Power Query, without needing to know its name:
= Table.RenameColumns(#"Previous step",{{Table.ColumnNames(#"Previous step"){0}, "New name"}})
amirshiloh
3 years agoAdvocate II
Great ! Thanks !