Forum Discussion
notfred87
1 year agoFrequent 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 co...
- 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
Anonymous
1 year agoNot applicable
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.