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
Marcel1989
Frequent Visitor

Transform table

Hello,

 

I have the following table:

 

AreaYearQuarterCat1Cat2Cat3
a120221111
a120232321
a220224245

 

I want to transform it to the following:

CategoryValueAreaYearQuarter
Cat11a120221
Cat13a120232
Cat12a220224
Cat21a120221
Cat22a120232
Cat24a220224
Cat31a120221
Cat31a120232
Cat35a220224

 

So basically transpose column Cat1, Cat2, Cat3 from table1 and create a separated line for each combination of Area, Year.

 

How can I do it? Thanks a lot, I am trying this for hours now.

1 ACCEPTED SOLUTION
pranit828
Community Champion
Community Champion

Hi @Marcel1989 

Select three columns(cat1,cat2, cat3) together and the unpivot to get the desired result.
Also, PFB the M code (Advance Editor) 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSjRU0lEyMjAyAlKGKDhWByFrDKKAGEZDZY0Qek2gMiDaVCk2FgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Area = _t, Year = _t, Quarter = _t, Cat1 = _t, Cat2 = _t, Cat3 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Area", type text}, {"Year", Int64.Type}, {"Quarter", Int64.Type}, {"Cat1", Int64.Type}, {"Cat2", Int64.Type}, {"Cat3", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Area", "Year", "Quarter"}, "Attribute", "Value"),
    #"Reordered Columns" = Table.ReorderColumns(#"Unpivoted Columns",{"Attribute", "Value", "Area", "Year", "Quarter"}),
    #"Sorted Rows" = Table.Sort(#"Reordered Columns",{{"Attribute", Order.Ascending}})
in
    #"Sorted Rows"

pranit828_0-1675878449581.png

 

 





PBI_SuperUser_Rank@1x.png


Hope it resolves your issue? 
Did I answer your question? Mark my post as a solution!

Appreciate your Kudos, Press the thumbs up button!!
Linkedin Profile

View solution in original post

2 REPLIES 2
pranit828
Community Champion
Community Champion

Hi @Marcel1989 

Select three columns(cat1,cat2, cat3) together and the unpivot to get the desired result.
Also, PFB the M code (Advance Editor) 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSjRU0lEyMjAyAlKGKDhWByFrDKKAGEZDZY0Qek2gMiDaVCk2FgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Area = _t, Year = _t, Quarter = _t, Cat1 = _t, Cat2 = _t, Cat3 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Area", type text}, {"Year", Int64.Type}, {"Quarter", Int64.Type}, {"Cat1", Int64.Type}, {"Cat2", Int64.Type}, {"Cat3", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Area", "Year", "Quarter"}, "Attribute", "Value"),
    #"Reordered Columns" = Table.ReorderColumns(#"Unpivoted Columns",{"Attribute", "Value", "Area", "Year", "Quarter"}),
    #"Sorted Rows" = Table.Sort(#"Reordered Columns",{{"Attribute", Order.Ascending}})
in
    #"Sorted Rows"

pranit828_0-1675878449581.png

 

 





PBI_SuperUser_Rank@1x.png


Hope it resolves your issue? 
Did I answer your question? Mark my post as a solution!

Appreciate your Kudos, Press the thumbs up button!!
Linkedin Profile

Hi @pranit828 

 

thank you, your solution works perfectly. I think my understanding of unipivot was wrong, but I thanks to your answer I fully understand now.

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.