Forum Discussion
Unpivot Columns
Hi,
I am trying to unpivot columns B to M but I am unable to get the desired result in query editor. I can get the desired results in Microsoft Access by writing the query and using Union All. Is it possible to do this in Power Query Editor? I have tried to add an indexed column so that I can sort through the numerical numbers after unpivot coumns B to M, but that didn't help as by unpivoting columns B to M it also changes the index numbers.
Data:
Desired result:
| Real Orders | Period | Amount |
| 532910 Prf Svcs Rev | 1 | -60000.00 |
| 690296 Lab: External | 1 | 8.22 |
| 692025 Overhead: Misc (lab) | 1 | 1.73 |
| 5610901 KTI-PTO - Fiber GLASS | 1 | -16377.47 |
| 605509 Contracts-Other Svcs | 1 | 833.57 |
| 645200 SI Expense Transfers | 1 | 200.00 |
| 690296 Lab: External | 1 | 0.06 |
| 692025 Overhead: Misc (lab) | 1 | 3305.90 |
| 95142290 TIS-A/N- salesforce | 1 | 20081.83 |
| 532910 Prf Svcs Rev | 2 | -60000.00 |
| 690296 Lab: External | 2 | 8.22 |
| 692025 Overhead: Misc (lab) | 2 | 4.38 |
| 5610901 KTI-PTO - Fiber GLASS | 2 | -11331.24 |
| 605509 Contracts-Other Svcs | 2 | 4962.10 |
| 645200 SI Expense Transfers | 2 | 1000.00 |
| 690296 Lab: External | 2 | 0.89 |
| 692025 Overhead: Misc (lab) | 2 | 3929.57 |
| 95142290 TIS-A/N- salesforce | 2 | 28603.93 |
| 532910 Prf Svcs Rev | 3 | 0.00 |
| 690296 Lab: External | 3 | 0.00 |
| 692025 Overhead: Misc (lab) | 3 | 0.00 |
| 5610901 KTI-PTO - Fiber GLASS | 3 | 0.00 |
| 605509 Contracts-Other Svcs | 3 | 0.00 |
| 645200 SI Expense Transfers | 3 | 0.00 |
| 690296 Lab: External | 3 | 0.00 |
| 692025 Overhead: Misc (lab) | 3 | 0.00 |
| 95142290 TIS-A/N- salesforce | 3 | 0.00 |
7 Replies
- danextianSuper User
Hi Anonymous
Please post a sample data that we can copy-paste (not an image) or a link to an Excel file.
- danextianSuper User
Or have you tried Unpivot Other Columns instead by selecting only the column/s that you want to stay pivoted?
- AnonymousNot applicable
Here is the link to the data: https://docs.google.com/spreadsheets/d/1EMqc3KrH2J6k5LpQakikSZIH3MgHvpNHu6gZwpYuY40/edit?usp=sharing
I tried the Unpivot Other Columns, but it didn't work out.
- danextianSuper User
Hello,
You need more than Unpivoting for your use case. Try the M-Script below.
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("vZJBa8MwDIX/ishpg8az5diJeytjG2XdUprcQg9u6tJCSIcdyn7+5JYx2NnU8FmCKLzk6XVdtnF2gNrvnQ/ZLBMEEpIoCEVooiQqwsQZHq84KTDbzrpMSTSCA6z9AZpLH2DjLvQ015wO4/xfn6JEWW04Gg2wsrs5vHxPzo92iN/JEP9KOjXkqADqi/NHZ/dz+DiFHh4Gu3uMVrDy6hmTVUJRpQU3XAC8t8t83daQw+tp5zy8rRZNE30VWpYlK8prL6QUDIuUf82V4gbg+TxO3vZTyOvpSPpxzdFjKZmK2oXRyETS7RYKOYWqWdJuv9wYHLTejuFwCyr+pkncLVc0oK+lMvfLlZRcMRPfkAbNzexU0kaJAtGQye2yyRdPnzlAsIMLh7Pv3c3kSrAqBhsrzSUzMpX89gc=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t, Column5 = _t, Column6 = _t, Column7 = _t, Column8 = _t, Column9 = _t, Column10 = _t, Column11 = _t, Column12 = _t, Column13 = _t]), #"Transposed Table" = Table.Transpose(Source), #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Transposed Table", {"Column1"}, "Attribute", "Value"), #"Changed Type" = Table.TransformColumnTypes(#"Unpivoted Other Columns",{{"Value", type text}}), RealOrdersName = Table.SelectRows(#"Changed Type", each ([Column1] = "Real Orders")), Custom1 = #"Unpivoted Other Columns", #"Filtered Rows1" = Table.SelectRows(Custom1, each [Column1] <> "Real Orders"), #"Merged Queries" = Table.NestedJoin(#"Filtered Rows1", {"Attribute"}, RealOrdersName, {"Attribute"}, "ColumnName", JoinKind.LeftOuter), #"Expanded ColumnName" = Table.ExpandTableColumn(#"Merged Queries", "ColumnName", {"Value"}, {"Real Orders"}), #"Renamed Columns" = Table.RenameColumns(#"Expanded ColumnName",{{"Column1", "Period"}, {"Value", "Amount"}}), #"Removed Other Columns" = Table.SelectColumns(#"Renamed Columns",{"Real Orders", "Amount", "Period"}), #"Changed Type1" = Table.TransformColumnTypes(#"Removed Other Columns",{{"Amount", type number}, {"Period", Int64.Type}}), #"Sorted Rows" = Table.Sort(#"Changed Type1",{{"Period", Order.Ascending}}) in #"Sorted Rows"Here's a screenshot of the result:
Sample PBIX - https://drive.google.com/file/d/1TD1fOdwaxP2pVDPp2goKprXVIno9rkch/view?usp=sharing
- AnonymousNot applicable
Thank you for working on this and sorry for the delayed reply. Would it be possible to have the desired result data in the same sort order (Real Order column) as the original data?