Forum Discussion
Unpivot Multiple pairs of columns in Power Query
- 1 year ago
Hi notfred87, check this:
Output
I don't know how many columns do you have so you have to edit these 2 steps
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
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.