Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
zaza
Resolver III
Resolver III

How to expand multiple columns to new rows at the same time?

Greetings

 

I have 2 colums with lists like so:

Each list contains several values, each of which corresponds to a value in the same position within the second list. Like so:

Column 1  |  Column 2

[1,2,3,4,5]  |  [A,B,C,D,E]

 

If I expand the first column and than the second column I will end up with a value pair for each element:

 

Column 1  |  Column 2

1                  A

1                  B

1                  C

1                  D

1                  E

2                  A

2                  B

...                 ...

 

However I want to end end up with this:

Column 1  |  Column 2

1                  A

2                  B

3                  C

4                  D

5                  E

 

Does anyone has any idea how could I solve this problem?

 

 

Thanks

Zaza

1 ACCEPTED SOLUTION
Anonymous
Not applicable

@zaza , try this M code:

 

et
    <PreviousStep> = some table,
    Add_Zipped = Table.AddColumn(<PreviousStep>, "Zipped", each List.Zip({[Column1], [Column2]}), type list),
    Expand_Zipped = Table.ExpandListColumn(Add_Zipped, "Zipped"),
    Extract_Zipped = Table.TransformColumns(Expand_Zipped, {"Zipped", each Text.Combine(List.Transform(_, Text.From), ";"), type text}),
    Split_Column = Table.SplitColumn(Extract_Zipped, "Zipped", Splitter.SplitTextByEachDelimiter({";"}, QuoteStyle.Csv, false), {"Zipped.1", "Zipped.2"})
in
    Split_Column

List.Zip() will combine the 2 lists into nested lists of {1, A}, {2, B}, {3, C}, etc.

 

You expand that to get 5 rows, each row will have 1 of these lists.

You extract the values and add a semicolon between values to delimit.

Then split the column by the semicolon delimiter.

 

Hope this helps!

 

~ Chris

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

@zaza , try this M code:

 

et
    <PreviousStep> = some table,
    Add_Zipped = Table.AddColumn(<PreviousStep>, "Zipped", each List.Zip({[Column1], [Column2]}), type list),
    Expand_Zipped = Table.ExpandListColumn(Add_Zipped, "Zipped"),
    Extract_Zipped = Table.TransformColumns(Expand_Zipped, {"Zipped", each Text.Combine(List.Transform(_, Text.From), ";"), type text}),
    Split_Column = Table.SplitColumn(Extract_Zipped, "Zipped", Splitter.SplitTextByEachDelimiter({";"}, QuoteStyle.Csv, false), {"Zipped.1", "Zipped.2"})
in
    Split_Column

List.Zip() will combine the 2 lists into nested lists of {1, A}, {2, B}, {3, C}, etc.

 

You expand that to get 5 rows, each row will have 1 of these lists.

You extract the values and add a semicolon between values to delimit.

Then split the column by the semicolon delimiter.

 

Hope this helps!

 

~ Chris

@Anonymous thank you for the solution, it works perfectly!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.