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
Mammoth789
New Member

Repeat rows according to several columns

Hi ! 

 

I am actually stick into a problem that I can't figure out an issue. Here's the thing :

 

I have a table similar to :

 

Title 1Title 2Item 1Item 2Item 3Item 4Item 5
MexicoPineapple HH  
TokyoApple  XX 
New YorkStrawberryAAAAA

 

And I would like to transform it so that it returns :

 

Title 1Title 2ValueItem
MexicoPineappleHItem 2
MexicoPineappleHItem 3
TokyoAppleXItem 3
TokyoAppleXItem 4
New YorkStrawberryAItem 1
New YorkStrawberryAItem 2
New YorkStrawberryAItem 3
New YorkStrawberryAItem 4
New YorkStrawberryAItem 5

 

In brief, there are columns 'Title ...' to keep, and the rows has to be repeated as long as there is a value in columns 'Item ...' ; We collect in new column which value was in colums 'Item...' and collect the column's title in a new column.

 

Thanks for helping.

 

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

That is a very frequent operation in Power Query. It is called "Unpivoting other columns"

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8k2tyEzOV9JRCsjMS00sKMhJBbIVgNgDihXAOFYnWikkP7sSpNIRSRUIR0AxRJVfarlCZH5RNlAguKQosTwptaioEqQLA8fGAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Title 1" = _t, #"Title 2" = _t, #"Item 1" = _t, #"Item 2" = _t, #"Item 3" = _t, #"Item 4" = _t, #"Item 5" = _t]),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Title 1", "Title 2"}, "Item", "Value"),
    #"Filtered Rows" = Table.SelectRows(#"Unpivoted Other Columns", each ([Value] <> " "))
in
    #"Filtered Rows"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".

 

 

View solution in original post

1 REPLY 1
lbendlin
Super User
Super User

That is a very frequent operation in Power Query. It is called "Unpivoting other columns"

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8k2tyEzOV9JRCsjMS00sKMhJBbIVgNgDihXAOFYnWikkP7sSpNIRSRUIR0AxRJVfarlCZH5RNlAguKQosTwptaioEqQLA8fGAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Title 1" = _t, #"Title 2" = _t, #"Item 1" = _t, #"Item 2" = _t, #"Item 3" = _t, #"Item 4" = _t, #"Item 5" = _t]),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Title 1", "Title 2"}, "Item", "Value"),
    #"Filtered Rows" = Table.SelectRows(#"Unpivoted Other Columns", each ([Value] <> " "))
in
    #"Filtered Rows"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".

 

 

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.