Dynamically rename a set of columns using Power Query
If you have a table with old and new names then you can use following pattern
let
rename_list = Table.ToColumns(Table.Transpose(Table2)),
result = Table.RenameColumns(Table1, rename_list, MissingField.Ignore)
in result
where Table2 is "Rename Table" and Table1 is initial table with data.
This idea is described in details here https://bondarenkoivan.wordpress.com/2015/04/17/dynamic-table-headers-in-power-query-sap-bydesign-odata/
If you have the resulting column names you want, it seems like you could convert Source
back to rows, then call Table.FromRows
on List2
let
//list of original column names
List1= {"Name1","Name2","Name3","Name4"},
//Create test table
Source = Table.FromRows({{1231,1233,4121,5232},{3546,3426,1246,3464}} , List1),
//list of new column names
List2 = {"NewName 1","NewName 2","NewName 3","NewName 4"},
Result = Table.FromRows(Table.ToRows(Source), List2)
in
Result
(Unless it is wrong to assume that e.g. Name 2
will always be the second column.)
Stating the original problem according to Ivan's solution, here goes. Carl's has the same result and is a little simpler for the example I gave, however, my situation will benefit from having the rename pairs set out explicitly in a table (ie. Table2). Plus using the MissingField.Ignore parameter with Table.RenameColumns means that it will only change the selection of columns I want to rename in my production query, the rest will remain unchanged.
let
//list of original column names
List1= {"Name1","Name2","Name3","Name4"},
//Create test table
Source = Table.FromRows({{1231,1233,4121,5232},{3546,3426,1246,3464}} , List1),
//list of new column names
List2 = {"NewName 1","NewName 2","NewName 3","NewName 4"},
//Rename columns (in practice, the two lists of names will be dynamic, not hard coded as below)
//Bring List1 and List2 together as rows in a table
Table2 = Table.FromRows({List1,List2}),
//Create a list of rename pairs
RenameList = Table.ToColumns(Table2),
//Call to Table.RenameColumns
Result = Table.RenameColumns(Source, RenameList, MissingField.Ignore)
in
Result