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
sabedin
Helper I
Helper I

PIVOT - UNPIVOT Rows

Hello,

 

Please refer to the attached screenshot. I am not sure how to best prepare this data for creating Visuals. I guess I have to create on column State with the data from the first Row, and one other column L-O with the data from the second Row. I tried different transformation steps but no luck.

 

Any ideas please

 

Thank you

Sabedin

Unpivot.jpg

1 ACCEPTED SOLUTION
ImkeF
Super User
Super User

You should find sth on this blog about doing this with the UI.

 

But I prefer a formula-solution (for performance-reasons, especially if your data is large):

 

let
// Replace this with your source-data
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WUtJRcgRiJyB2VorViVZySSxJBXJ8EvNSEJRzfl4JWNbAUA+IjAwMzYGihkBsBMTGECkjZCkDkByIMDaAyBqjyEKkIfJABbEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Spalte1 = _t, Spalte2 = _t, Spalte3 = _t, Spalte4 = _t]), Rename = Table.RenameColumns(Source, List.Transform(Table.ColumnNames(Source), each {_, Text.Combine({Record.Field(Source{0}, _), Record.Field(Source{1}, _)}, "--")})), RemoveTopTwo = Table.Skip(Rename,2), // Depending on how the blanks in your first fields come in, you might need to chang {"--DATE"} to {"DATE"}
UnpivotOthers = Table.UnpivotOtherColumns(RemoveTopTwo, {"--DATE"}, "Attribute", "Value"), Split = Table.SplitColumn(UnpivotOthers,"Attribute",Splitter.SplitTextByDelimiter("--", QuoteStyle.Csv),{"Country", "Category"}) in Split

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

1 REPLY 1
ImkeF
Super User
Super User

You should find sth on this blog about doing this with the UI.

 

But I prefer a formula-solution (for performance-reasons, especially if your data is large):

 

let
// Replace this with your source-data
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WUtJRcgRiJyB2VorViVZySSxJBXJ8EvNSEJRzfl4JWNbAUA+IjAwMzYGihkBsBMTGECkjZCkDkByIMDaAyBqjyEKkIfJABbEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Spalte1 = _t, Spalte2 = _t, Spalte3 = _t, Spalte4 = _t]), Rename = Table.RenameColumns(Source, List.Transform(Table.ColumnNames(Source), each {_, Text.Combine({Record.Field(Source{0}, _), Record.Field(Source{1}, _)}, "--")})), RemoveTopTwo = Table.Skip(Rename,2), // Depending on how the blanks in your first fields come in, you might need to chang {"--DATE"} to {"DATE"}
UnpivotOthers = Table.UnpivotOtherColumns(RemoveTopTwo, {"--DATE"}, "Attribute", "Value"), Split = Table.SplitColumn(UnpivotOthers,"Attribute",Splitter.SplitTextByDelimiter("--", QuoteStyle.Csv),{"Country", "Category"}) in Split

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

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.