How to simplify a special combining of two lists into one

anvg

Active Member
Joined
Feb 14, 2012
Messages
478
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!
 

Some videos you may like

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Ozeroth

Active Member
Joined
Dec 14, 2013
Messages
264
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
 

anvg

Active Member
Joined
Feb 14, 2012
Messages
478
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?
 

anvg

Active Member
Joined
Feb 14, 2012
Messages
478
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!
 

ChrisWebb

New Member
Joined
Sep 2, 2014
Messages
27
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
 

anvg

Active Member
Joined
Feb 14, 2012
Messages
478
ChrisWebb, thank you very much!

It was very informative for me.
 

Watch MrExcel Video

Forum statistics

Threads
1,095,168
Messages
5,442,792
Members
405,196
Latest member
rmuir12

This Week's Hot Topics

  • Copy entire row if CountA <>0 to another sheet
    [B]I want to copy entire row if CountA <>0 for column J7:AM7 (headers on J6:AM6) and so on till the last used cell is column D and paste the...
  • Select last used Row in Table
    I have created a Table in a Worksheet which is locked to prevent user errors and protect formula. Some of the cells require freetext entries which...
  • excel workbook: do not allow certain file name
    Hello all, Don't think this has ever been asked before, but how do I restrict file save [Before_Save Event] if the name of the file being saved...
  • fixing problem autofilter
    hello i need help about my code when i search by code in textbox it doesn't show anything this is my data [ATTACH type="full"...
  • “Weight”
    Hi, i’ve got a long sheet filled with weights such as kg,g,L & ml. i can build a formula to convert kg into g and liter into ml. How ever, my...
  • How to capitalize everything before a certain character?
    In column A, I have some text: Hello good day.mp3 Hello good day.flac etc. I'd like to capitalize everything before the period. I don't need the...
Top