anvg
Active Member
- Joined
- Feb 14, 2012
- Messages
- 485
Hi
I need a code for Power Query for a combining of two lists into one
I created a query for this task. It is
Is there any way to do my code more simple?
I understand that I can write an udf-function for steps to a join command like this
But I interest other solve, maybe with list functions.
Many thanks!
I need a code for Power Query for a combining of two lists into one
Code:
//it is start lists with equal length
listA = {"a","b","c"},
listD = {"1","2","3"},
//it is a needed list
listAD = { {"a","1"},{"b","2"},{"c","3"} }
Code:
let
//start lists A and D
listA = {"a","b","c"},
listD = {"1","2","3"},
//convert list to tables
tableA = Table.FromList(listA),
tableD = Table.FromList(listD),
//add enumeration column
indexA = Table.AddIndexColumn(tableA,"IdA"),
indexD = Table.AddIndexColumn(tableD,"IdD"),
//change Column1 name to another for a join
renameA = Table.RenameColumns(indexA,{ {"Column1","A"} }),
renameD = Table.RenameColumns(indexD,{ {"Column1","D"} }),
//join tables by enum columns
join = Table.Join(renameA,"IdA",renameD,"IdD")[ [A],[D] ],
//create and return a needed listAD = { {"a","1"},{"b","2"},{"c","3"} }
return = Table.AddColumn(join,"combined",each {[A],[D]})[combined]
in
return
I understand that I can write an udf-function for steps to a join command like this
Code:
let
Creator = (inList as list,suffix as text) as table =>
let
toTable = Table.FromList(inList),
changeName = Table.RenameColumns(toTable,{ {"Column1",suffix} }),
indexCol = Table.AddIndexColumn(changeName,"Id" & suffix)
in
indexCol
in
Creator
Many thanks!