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
Anonymous
Not applicable

Transpose table with three fields to transpose and two value columns

Hello,

 

I have imported a table with 5 columns: YEAR_QUARTER, YEAR_QUARTER_ORDER, LEVEL, PERCENTAGE_QUARTER and PERCENTAGE_YEAR

 

YEAR_QUARTERYEAR_QUARTER_ORDERLEVELPERCENTAGE_QUARTERPERCENTAGE_YEAR
2019 Q120191EAST97,5697,56
2019 Q120191WEST95,8795,87
2019 Q120191TOTAL96,4696,46
2019 Q220192EAST99,3998,48
2019 Q220192WEST100,7498,31
2019 Q220192TOTAL100,2798,37
2019 Q320193EAST100,1898,49
2019 Q320193WEST98,7497,95
2019 Q320193TOTAL99,2498,14
2019 Q420194EAST95,9097,32
2019 Q420194WEST94,3698,37
2019 Q420194TOTAL94,9098,00
2020 Q120201EAST97,5097,50
2020 Q120201WEST98,8698,86
2020 Q120201TOTAL98,3898,38
2020 Q220202EAST95,7096,60
2020 Q220202WEST101,50100,18
2020 Q220202TOTAL99,4798,93

 

I need to transpose this table in such a way that I get one row per value of YEAR_QUARTER, 3 columns - one per LEVEL - for PERCENTAGE_QUARTER and 3 columns - one per LEVEL - for PERCENTAGE_YEAR.

 

YEAR_QUARTERYEAR_QUARTER_ORDERPERCENTAGE_QUARTER_EASTPERCENTAGE_QUARTER_WESTPERCENTAGE_QUARTER_TOTALPERCENTAGE_YEAR_EASTPERCENTAGE_YEAR_WESTPERCENTAGE_YEAR_TOTAL
2019 Q12019197,5695,8796,4697,5695,8796,46
2019 Q22019299,39100,74100,2798,4898,3198,37
2019 Q320193100,1898,7499,2498,4997,9598,14
2019 Q42019495,9094,3694,9097,3298,3798,00
2020 Q12020197,5098,8698,3897,5098,8698,38
2020 Q22020295,70101,5099,4796,60100,1898,93

 

How can I do this?

 

Thanks

 

R.W.

1 ACCEPTED SOLUTION
v-yiruan-msft
Community Support
Community Support

Hi @Anonymous ,

You can make some transformations in Power Query Editor to achieve it:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ddI9CsMwDAXgq5TMGmRZ/hszZCuU0kCHkPtfo3atyA7Y08vwYV4kHcdCaNLjbRb4f5Xc1s+eIwVwXvOEEf1ulTqIQXNM99e+PovxwF6zWRJLXYMENpWMwHFCpYFBhMDVWjOxV4WCKQju+1rBtnUo1kQpkSb2GkOUDgGSm1AdQwKSvoY7y2K5G4ODhPVZSxN6NWCwfvBjPdUGLM9GQBRLeC0t6/spoOaYthlErzmm2iC3jJrNkli6zSBgvRqPE6qnYGpbWd0Yd3tgOYW8n/P8AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [YEAR_QUARTER = _t, YEAR_QUARTER_ORDER = _t, LEVEL = _t, PERCENTAGE_QUARTER = _t, PERCENTAGE_YEAR = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"YEAR_QUARTER", type text}, {"YEAR_QUARTER_ORDER", Int64.Type}, {"LEVEL", type text}, {"PERCENTAGE_QUARTER", Int64.Type}, {"PERCENTAGE_YEAR", Int64.Type}}),
    #"Unpivoted Only Selected Columns" = Table.Unpivot(#"Changed Type", {"PERCENTAGE_QUARTER", "PERCENTAGE_YEAR"}, "Attribute", "Value"),
    #"Added Custom" = Table.AddColumn(#"Unpivoted Only Selected Columns", "Custom", each [Attribute]&"_"&[LEVEL]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"LEVEL", "Attribute"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Custom]), "Custom", "Value", List.Sum)
in
    #"Pivoted Column"

Transpose table.JPG

Best Regards

Rena

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
v-yiruan-msft
Community Support
Community Support

Hi @Anonymous ,

You can make some transformations in Power Query Editor to achieve it:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ddI9CsMwDAXgq5TMGmRZ/hszZCuU0kCHkPtfo3atyA7Y08vwYV4kHcdCaNLjbRb4f5Xc1s+eIwVwXvOEEf1ulTqIQXNM99e+PovxwF6zWRJLXYMENpWMwHFCpYFBhMDVWjOxV4WCKQju+1rBtnUo1kQpkSb2GkOUDgGSm1AdQwKSvoY7y2K5G4ODhPVZSxN6NWCwfvBjPdUGLM9GQBRLeC0t6/spoOaYthlErzmm2iC3jJrNkli6zSBgvRqPE6qnYGpbWd0Yd3tgOYW8n/P8AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [YEAR_QUARTER = _t, YEAR_QUARTER_ORDER = _t, LEVEL = _t, PERCENTAGE_QUARTER = _t, PERCENTAGE_YEAR = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"YEAR_QUARTER", type text}, {"YEAR_QUARTER_ORDER", Int64.Type}, {"LEVEL", type text}, {"PERCENTAGE_QUARTER", Int64.Type}, {"PERCENTAGE_YEAR", Int64.Type}}),
    #"Unpivoted Only Selected Columns" = Table.Unpivot(#"Changed Type", {"PERCENTAGE_QUARTER", "PERCENTAGE_YEAR"}, "Attribute", "Value"),
    #"Added Custom" = Table.AddColumn(#"Unpivoted Only Selected Columns", "Custom", each [Attribute]&"_"&[LEVEL]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"LEVEL", "Attribute"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Custom]), "Custom", "Value", List.Sum)
in
    #"Pivoted Column"

Transpose table.JPG

Best Regards

Rena

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
parry2k
Super User
Super User

@Anonymous the unpivoted model is better , why not use matrix visual with Level on Column and value on the rows.

 

I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos whoever helped to solve your problem. It is a token of appreciation!

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

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.