Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago

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 OrdersPeriodAmount
532910  Prf Svcs Rev1-60000.00
690296  Lab: External18.22
692025  Overhead: Misc (lab)11.73
5610901  KTI-PTO - Fiber GLASS1-16377.47
605509  Contracts-Other Svcs1833.57
645200  SI Expense Transfers1200.00
690296  Lab: External10.06
692025  Overhead: Misc (lab)13305.90
95142290  TIS-A/N-  salesforce120081.83
532910  Prf Svcs Rev2-60000.00
690296  Lab: External28.22
692025  Overhead: Misc (lab)24.38
5610901  KTI-PTO - Fiber GLASS2-11331.24
605509  Contracts-Other Svcs24962.10
645200  SI Expense Transfers21000.00
690296  Lab: External20.89
692025  Overhead: Misc (lab)23929.57
95142290  TIS-A/N-  salesforce228603.93
532910  Prf Svcs Rev30.00
690296  Lab: External30.00
692025  Overhead: Misc (lab)30.00
5610901  KTI-PTO - Fiber GLASS30.00
605509  Contracts-Other Svcs30.00
645200  SI Expense Transfers30.00
690296  Lab: External30.00
692025  Overhead: Misc (lab)30.00
95142290  TIS-A/N-  salesforce30.00

 

7 Replies

  • Hi Anonymous 

     

    Please post a sample data that we can copy-paste (not an image) or a link to an Excel file.

  • Or have you tried Unpivot Other Columns instead by selecting only the column/s that you want to stay pivoted?

    • danextian's avatar
      danextian
      Super 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 

      • Anonymous's avatar
        Anonymous
        Not 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?