How to simplify a special combining of two lists into one

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
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"} }
I created a query for this task. It is
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
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
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
But I interest other solve, maybe with list functions.
Many thanks!
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Here are two ideas :):

Code:
let
    listA = {"a","b","c"},
    listD = {"1","2","3"},
    listAD = Table.ToRows(Table.FromColumns({listA,listD}) )
in
    listAD

Code:
let
    listA = {"a","b","c"},
    listD = {"1","2","3"},
    listAD = List.Generate(
                  ()=> [ Position = 0, ItemA = listA{0} , ItemD = listD{0} ],
                  each [Position] < List.Count(listA),
                  each [ Position = [Position]+1, ItemA=listA{[Position]+1}, ItemD = listD{[Position]+1} ],
                  each {[Item1], [Item2]}
            )
                  
in
    listAD
 
Upvote 0
Excelent! Thank you very much!

each {[Item1], [Item2]} is necessary to correct on {[ItemA], [ItemD]}.

Could I ask you another question? What other iteration varinats has Power Query?
 
Upvote 0
Your first link I have already read. But with than "Create Running Totals" solution is one problem. It's difficulty is O(N^2). Your solution and other in your second link are more interesting, because to allow solving that task with O(N). I will try.

Thank you very much for your collaboration!
 
Upvote 0
Here's another option, a bit simpler than using List.Generate():

Code:
let
    listA = {"a","b","c"},
    listD = {"1","2","3"},
    listpositions = List.Positions(listA),
    output = List.Transform(listpositions, each {listA{_}, listD{_}})
in
    output

Chris
 
Upvote 0

Forum statistics

Threads
1,213,551
Messages
6,114,272
Members
448,558
Latest member
aivin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top