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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
karo
Post Patron
Post Patron

Unpivot 2 types of columns in Direct Query

Hi,

 

I am wonder if there is any clever way to unpivot columns in Power BI  with two types of columns (sales ammount by month and Quantity per month) in  Direct Query.

 

From:

ClientCityProduct IDJune 2020 (Sales Amount)June 2020 (Quantity)July 2020 (Sales Amount)July 2020 (Quantity)August 2020 (Sales Amount)August 2020 (Quantity)
AX1234100110001010000100
BY13455012500505000100
CZ145610150500

 

To:

 CityProduct IDDateSales AmountQuantity
AX1234June 20201001
AX1234July 2020100010
AX1234August 202010000100
BXY1345June 2020501
BXY1345July 2020250050
BXY1345August 20205000100
CXYZ1456June 2020101
CXYZ1456July 2020505
CXYZ1456August 202000

 

Thank you in advance for any hints!,

Karo

3 REPLIES 3
AllisonKennedy
Super User
Super User

Paste this code into the Advanced Editor of Power Query Editor to see how to unpivot then repivot:

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dYwxD8IgEIX/CmHSpANScMd20UnjohKGRolpgjSxMPTfe0BFGTrcvfd49yElbkyvrcMVbno3gRzfw8PfHdq3EA7eakQJJWh17owekXgN3rp1WZ18Zx3Q6dlMi0Su/gnhn350C0xR/ihVSSygvsBsaM2CEBJ2ctGS2c8aoR34a8g14yD8y1Aez3haBdKAv4XM+DZ/m29hSBylPg==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"(blank)" = _t, #"(blank).1" = _t, #"(blank).2" = _t, #"(blank).3" = _t, #"(blank).4" = _t, #"(blank).5" = _t, #"(blank).6" = _t, #"(blank).7" = _t, #"(blank).8" = _t]),
#"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
#"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Client", type text}, {"City", type text}, {"Product ID", Int64.Type}, {"June 2020 (Sales Amount)", Int64.Type}, {"June 2020 (Quantity)", Int64.Type}, {"July 2020 (Sales Amount)", Int64.Type}, {"July 2020 (Quantity)", Int64.Type}, {"August 2020 (Sales Amount)", Int64.Type}, {"August 2020 (Quantity)", Int64.Type}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type1", {"Client", "City", "Product ID"}, "Attribute", "Value"),
#"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByEachDelimiter({"("}, QuoteStyle.Csv, false), {"Attribute.1", "Attribute.2"}),
#"Extracted Text Before Delimiter" = Table.TransformColumns(#"Split Column by Delimiter", {{"Attribute.2", each Text.BeforeDelimiter(_, ")"), type text}}),
#"Pivoted Column" = Table.Pivot(#"Extracted Text Before Delimiter", List.Distinct(#"Extracted Text Before Delimiter"[Attribute.2]), "Attribute.2", "Value"),
#"Changed Type" = Table.TransformColumnTypes(#"Pivoted Column",{{"Attribute.1", type date}}),
#"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Attribute.1", "Date"}})
in
#"Renamed Columns"

Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

hi @AllisonKennedy ,

thank you for your reply, however few steps are not supported in Direct Query (example: Split Column by Delimiter).

regards,

karo

Hi @karo ,

 

Check the similar thread below:

https://community.powerbi.com/t5/Desktop/Using-dax-to-unpivot-a-table/td-p/421116

 

Best Regards,
Kelly
Did I answer your question? Mark my post as a solution!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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