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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
notfred87
Frequent Visitor

Unpivot Multiple pairs of columns in Power Query

Hi all, I am new to Power Query/Power BI. I have a table that I need to unpivot multiple columns into 3 sets of pairs all at once. I used List.Zip({}) and it works perfectly for unpivoting Product columns and Type Columns. However it only supports 2 lists and I can't do the Category columns alongside the other two columns sets. I need to transpose Product, Type and Category Columns at once keeping their positions relative to other columns in the table. Quick note, "Type" columns are INT and will have many NULLs and I need to preserve them. The NULLs in Category column can be replaced with dummy values if needed. 

 

I have included my List.Zip code, current table layout and desired table layout. Thank you very much in advance for any help.

 

List.Zip code example (there are actually 11 of the columns per set that need transposed. I didn't include all in my table example below to save space):

 

 

 

List.Zip( {
{"Product1","Product2","Product3","Product4","Product5","Product6","Product7","Product8","Product9","Product10","Product11"},
{[Product1],[Product2],[Product3],[Product4],[Product5],[Product6],[Product7],[Product8],[Product9],[Product10],[Product11]},
{"Type1","Type2","Type3","Type4","Type5","Type6","Type7","Type8","Type9","Type10","Type11"},
{[Type1],[Type2],[Type3],[Type4],[Type5],[Type6],[Type7],[Type8],[Type9],[Type10],[Type11]}
} )

 

 

 

 

 

 

Table looks like:

Unique_IDRandom 1Random 2Random 3Product 1Product 2Product 3Product 4Product 5Product 6Product 7Type 1 Type 2Type 3Type 4Type 5Type 6Type 7Category 1Category 2Category 3Category 4Category 5Category 6Category 7
123R1R3R5Product Value 1Product Value 2Product Value 3Product Value 4Product Value 5Product Value 6Product Value 7Type Value 1Type Value 2Type Value 3Type Value 4Type Value 5Type Value 6Type Value 7Category Value 1Category Value 2Category Value 3Category Value 4Category Value 5Category Value 6Category Value 7
456R2R4R6Product Value 60Product Value 8Product Value 9Product Value 10Product Value 11Product Value 12Product Value 13Type Value 8Type Value 9Type Value 10Type Value 11Type Value 12Type Value 13Type Value 14Category Value 8Category Value 9Category Value 10Category Value 11Category Value 12Category Value 13Category Value 14

 

I need table to look like: 

Unique_IDRandom 1Random 2Random 3Product CategoryProduct ValueType CategoryType ValueCategory NameCategory Value
123R1R3R5Product 1 Product Value 1Type 1Type Value 1Category 1Category Value 1
123R1R3R5Product 2Product Value 2Type 2Type Value 2Category 2Category Value 2
123R1R3R5Product 3Product Value 3Type 3Type Value 3Category 3Category Value 3
123R1R3R5Product 4Product Value 4Type 4Type Value 4Category 4Category Value 4
123R1R3R5Product 5Product Value 5Type 5Type Value 5Category 5Category Value 5
123R1R3R5Product 6Product Value 6Type 6Type Value 6Category 6Category Value 6
123R1R3R5Product 7Product Value 7Type 7Type Value 7Category 7Category Value 7
456R2R4R6Product 1 Product Value 8Type 1Type Value 8Category 1Category Value 8
456R2R4R6Product 2Product Value 9Type 2Type Value 9Category 2Category Value 9
456R2R4R6Product 3Product Value 10Type 3Type Value 10Category 3Category Value 10
456R2R4R6Product 4Product Value 11Type 4Type Value 11Category 4Category Value 11
456R2R4R6Product 5Product Value 12Type 5Type Value 12Category 5Category Value 12
456R2R4R6Product 6Product Value 13Type 6Type Value 13Category 6Category Value 13
456R2R4R6Product 7Product Value 14Type 7Type Value 14Category 7Category Value 14
1 ACCEPTED SOLUTION
dufoq3
Super User
Super User

Hi @notfred87, check this:

 

Output

dufoq3_0-1737572135540.png

 

I don't know how many columns do you have so you have to edit these 2 steps

dufoq3_1-1737572204743.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bZG7DoMwDEV/BWVmiPMC5v5AFVVdEANqUZdKVAgG/p6kU+Lrxco9UezIZxwVGataFSmX/8mnct/W9/Ham+f8PZaGgBggFogDgp0DkC6Rx/lbitFFNHW0dXR19HUMdcyDbvO+fNbtLIYxZBBZRA6RRxQQdWpqR+V8vop5VsytIq4laEA9kAHN4TNCm4Q6ia22r+PALGmWmTZi3nh3EhbYIxoEZVpggkcSRJJgMv1kmi4=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Unique_ID = _t, #"Random 1" = _t, #"Random 2" = _t, #"Random 3" = _t, #"Product 1" = _t, #"Product 2" = _t, #"Product 3" = _t, #"Product 4" = _t, #"Product 5" = _t, #"Product 6" = _t, #"Product 7" = _t, #"Type 1 " = _t, #"Type 2" = _t, #"Type 3" = _t, #"Type 4" = _t, #"Type 5" = _t, #"Type 6" = _t, #"Type 7" = _t, #"Category 1" = _t, #"Category 2" = _t, #"Category 3" = _t, #"Category 4" = _t, #"Category 5" = _t, #"Category 6" = _t, #"Category 7" = _t]),
    // Change number of first N columns to preserve.
    ColsToPreserve = List.Buffer(List.FirstN(Table.ColumnNames(Source), 4)),
    // Enter type of columns to unpivot in same order as they are in Source step.
    ColsToUnpivot = List.Buffer({"Product", "Type", "Category"}),
    TransformedData = List.TransformMany(Table.ToRows(Source),
        each {List.Split(List.Skip(_, List.Count(ColsToPreserve)), (List.Count(_) - List.Count(ColsToPreserve)) / List.Count(ColsToUnpivot))},
        (x,y)=> List.Split(List.FirstN(x, List.Count(ColsToPreserve)), 1) & y ),
    TransformedColNames = [ a = List.Skip(Table.ColumnNames(Source), List.Count(ColsToPreserve)),
    b = List.Split(a, List.Count(a)/ List.Count(ColsToUnpivot))
  ][b],
    CombinedData = Table.Combine(List.Transform(TransformedData, each Table.FromColumns(List.FirstN(_, List.Count(ColsToPreserve)) & List.Combine(List.Zip({ TransformedColNames, List.Skip(_, List.Count(ColsToPreserve)) }))))),
    NewColNames = ColsToPreserve & List.Combine(List.Zip({ ColsToUnpivot, List.Transform(ColsToUnpivot, each _ & " Value") })),
    RenamedColumns = Table.RenameColumns(CombinedData, List.Zip({ Table.ColumnNames(CombinedData), NewColNames })),
    FilledDown = Table.FillDown(RenamedColumns, ColsToPreserve)
in
    FilledDown

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

View solution in original post

11 REPLIES 11
v-mdharahman
Community Support
Community Support

Hi @notfred87,

Could you please confirm if the issue has been resolved after raising a support case? If a solution has been found, it would be greatly appreciated if you could share your insights with the community. This would be helpful for other members who may encounter similar issues.

 

Thank you.

v-mdharahman
Community Support
Community Support

Hi @notfred87,

I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions. If my response has addressed your query, please accept it as a solution so that other community members can find it easily.


Thank you.

v-mdharahman
Community Support
Community Support

Hi @notfred87,

As we haven’t heard back from you, so just following up to our previous message. I'd like to confirm if you've successfully resolved this issue or if you need further help?

If yes, you are welcome to share your workaround and mark it as a solution so that other users can benefit as well. If you find a reply particularly helpful to you, you can also mark it as a solution.


If you still have any questions or need more support, please feel free to let us know. We are more than happy to continue to help you.
Thank you for your patience and look forward to hearing from you.

v-mdharahman
Community Support
Community Support

Hi @notfred87,

Thanks for reaching out to the Microsoft fabric community forum.

 

It looks like you want to do some transformation with your columns. Please go through the solution provided by @dufoq3. If the solution solves your issue, please consider it accepting as Solution.

 

I would also take a moment to personally thank @dufoq3 ,@AlienSx and @Chewdata for actively participating in the community forum and for the solutions you’ve been sharing in the community forum. Your contributions make a real differenc

 

If I misunderstand your needs or you still have problems on it, please feel free to let us know.  

Best Regards,
Hammad.
Community Support Team

 

If this post helps then please mark it as a solution, so that other members find it more quickly.

Thank you.

dufoq3
Super User
Super User

Hi @notfred87, check this:

 

Output

dufoq3_0-1737572135540.png

 

I don't know how many columns do you have so you have to edit these 2 steps

dufoq3_1-1737572204743.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bZG7DoMwDEV/BWVmiPMC5v5AFVVdEANqUZdKVAgG/p6kU+Lrxco9UezIZxwVGataFSmX/8mnct/W9/Ham+f8PZaGgBggFogDgp0DkC6Rx/lbitFFNHW0dXR19HUMdcyDbvO+fNbtLIYxZBBZRA6RRxQQdWpqR+V8vop5VsytIq4laEA9kAHN4TNCm4Q6ia22r+PALGmWmTZi3nh3EhbYIxoEZVpggkcSRJJgMv1kmi4=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Unique_ID = _t, #"Random 1" = _t, #"Random 2" = _t, #"Random 3" = _t, #"Product 1" = _t, #"Product 2" = _t, #"Product 3" = _t, #"Product 4" = _t, #"Product 5" = _t, #"Product 6" = _t, #"Product 7" = _t, #"Type 1 " = _t, #"Type 2" = _t, #"Type 3" = _t, #"Type 4" = _t, #"Type 5" = _t, #"Type 6" = _t, #"Type 7" = _t, #"Category 1" = _t, #"Category 2" = _t, #"Category 3" = _t, #"Category 4" = _t, #"Category 5" = _t, #"Category 6" = _t, #"Category 7" = _t]),
    // Change number of first N columns to preserve.
    ColsToPreserve = List.Buffer(List.FirstN(Table.ColumnNames(Source), 4)),
    // Enter type of columns to unpivot in same order as they are in Source step.
    ColsToUnpivot = List.Buffer({"Product", "Type", "Category"}),
    TransformedData = List.TransformMany(Table.ToRows(Source),
        each {List.Split(List.Skip(_, List.Count(ColsToPreserve)), (List.Count(_) - List.Count(ColsToPreserve)) / List.Count(ColsToUnpivot))},
        (x,y)=> List.Split(List.FirstN(x, List.Count(ColsToPreserve)), 1) & y ),
    TransformedColNames = [ a = List.Skip(Table.ColumnNames(Source), List.Count(ColsToPreserve)),
    b = List.Split(a, List.Count(a)/ List.Count(ColsToUnpivot))
  ][b],
    CombinedData = Table.Combine(List.Transform(TransformedData, each Table.FromColumns(List.FirstN(_, List.Count(ColsToPreserve)) & List.Combine(List.Zip({ TransformedColNames, List.Skip(_, List.Count(ColsToPreserve)) }))))),
    NewColNames = ColsToPreserve & List.Combine(List.Zip({ ColsToUnpivot, List.Transform(ColsToUnpivot, each _ & " Value") })),
    RenamedColumns = Table.RenameColumns(CombinedData, List.Zip({ Table.ColumnNames(CombinedData), NewColNames })),
    FilledDown = Table.FillDown(RenamedColumns, ColsToPreserve)
in
    FilledDown

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

notfred87
Frequent Visitor

@AlienSx, this didn't work, I got the following error: 

"DataFormat.Error: There were more columns in the result than expected.
Details:
Count=10"

I have created a dummy PBIX file with my table but I cannot upload here. What is the best way to share this here? 

upoad file to any cloud service available to you and provide a working link here. 

AlienSx
Super User
Super User

let
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    headers = List.Buffer(Table.ColumnNames(Source)), 
    first4 = List.FirstN(headers, 4),
    to_unpivot = List.Buffer(List.Skip(headers, 4)), 
    lst = List.TransformMany(
        Table.ToList(Source, each _),
        (x) => List.Transform(List.Zip(List.Split(List.Zip({to_unpivot, List.Skip(x, 4)}), 3)), List.Combine),
        (x, y) => List.FirstN(x, 4) & y
    ), 
    result = Table.FromList(lst, each _, first4 & {"Product Category", "Product Value", "Type Category", "Type Value", "Category Name", "Category Value"})
in
    result

This didn't work, I got the error: "DataFormat.Error: There were more columns in the result than expected.
Details:
Count=10"

 

I have created a dummy PBIX file with my source table. What is the best way to upload the file here? 

This seemed to come close to working with my file but I had an error saying too many columns (there are additional columns in the table). I wasn't able to successfully adapt the code to get it to work. How can I share a pbix or excel file with the exact table I have? 

Chewdata
Super User
Super User

Hey!

You can achieve this by splitting it into three seperate unpivot actions and then combining the three tables into 1:

let
    Source = YOURDATA,
    // Get and unpivot Productdata
    GetProductColumns = Table.SelectColumns(Source,{"Unique_ID", "Product 7", "Product 6", "Product 5", "Product 4", "Product 3", "Product 2", "Product 1"}),
    UnpivotProducts = Table.UnpivotOtherColumns(GetProductColumns, {"Unique_ID"}, "Product", "ProductValue"),
    // Get and unpivot Type data
    GetTypeColumns = Table.SelectColumns(Source,{"Unique_ID", "Type 1 ", "Type 2", "Type 3", "Type 4", "Type 5", "Type 6", "Type 7"}),
    UnpivotType = Table.UnpivotOtherColumns(GetTypeColumns, {"Unique_ID"}, "Type", "Type Value"),
    // Get and unpivot Category data
    GetCategoryColumns = Table.SelectColumns(Source,{"Unique_ID", "Category 1", "Category 2", "Category 3", "Category 4", "Category 5", "Category 6", "Category 7"}),
    UnpivotCategory = Table.UnpivotOtherColumns(GetCategoryColumns, {"Unique_ID"}, "Category", "CategoryValue"),

    // Get the starting columns (ID and Random) and join them with the three seperate queries
    GetFirstColumns = Table.SelectColumns(Source,{"Unique_ID", "Random 1", "Random 2", "Random 3"}),
    leftouter_Products = Table.NestedJoin(GetFirstColumns, {"Unique_ID"}, UnpivotProducts, {"Unique_ID"}, "UnpivotProducts", JoinKind.LeftOuter),
    leftouter_Type = Table.NestedJoin(leftouter_Products, {"Unique_ID"}, UnpivotType, {"Unique_ID"}, "UnpivotType", JoinKind.LeftOuter),
    leftouter_Category = Table.NestedJoin(leftouter_Type, {"Unique_ID"}, UnpivotCategory, {"Unique_ID"}, "UnpivotCategory", JoinKind.LeftOuter),

    // Expand all tables
    #"Expanded UnpivotProducts" = Table.ExpandTableColumn(leftouter_Category, "UnpivotProducts", {"Product", "ProductValue"}, {"Product", "ProductValue"}),
    #"Expanded UnpivotType" = Table.ExpandTableColumn(#"Expanded UnpivotProducts", "UnpivotType", {"Type", "Type Value"}, {"Type", "Type Value"}),
    #"Expanded UnpivotCategory" = Table.ExpandTableColumn(#"Expanded UnpivotType", "UnpivotCategory", {"Category", "CategoryValue"}, {"Category", "CategoryValue"})
in
    #"Expanded UnpivotCategory"

 

Output:

Chewdata_0-1737186838768.png


Hopefully this helps! IF so please consider accepting this as a solution, so other users can quickly find it.

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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