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
MichaelF1
Helper II
Helper II

Power Query - How to use the values in one field as column headers

Hi, this is what I have:

 

YearCountryMeasure
2021GB16
2021FR23
2021NG13
2022GB9
2022FR32
2022NG24
2023GB25
2023FR30
2023NG

17

 

This is what I need:

 

YearGB MeasureFR MeasureNG Measure
2021162313
202293224
2023253017

 

I've tried various combinations of pivot and unpivot but I can't seem to get the right result!

 

Thanks in advance 🙂

 

 

1 ACCEPTED SOLUTION
jpessoa8
Super User
Super User

Hi @MichaelF1 ,

 

In this case you will need to use the "Pivot Column" option on the Country Column and selecting as "Values Column" the Column Measure:

jpessoa8_0-1701194477673.pngjpessoa8_1-1701194494280.png

 

This will be the result:

jpessoa8_2-1701194530173.png

 

Here is the code of my implementation in PowerQuery:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMlTSUXJ3AhKGZkqxOnAhtyAgYWSMLOTnDlIFFzKCabREFgHrMzZCFgLrMzKBCRnD9BmZIgtBNBogC0EsNFeKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Year = _t, Country = _t, Measure = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Measure", Int64.Type}}),
    #"Pivoted Column" = Table.Pivot(#"Changed Type", List.Distinct(#"Changed Type"[Country]), "Country", "Measure", List.Sum)
in
    #"Pivoted Column"

 

Hope this answer solves your problem!
If you need any additional help please @ me in your reply.
If my reply provided you with a solution, please consider marking it as a solution ✔️ or giving it a kudo 👍
Thanks!

You can also check out my LinkedIn!

Best regards,
Jorge Pessoa

 

View solution in original post

1 REPLY 1
jpessoa8
Super User
Super User

Hi @MichaelF1 ,

 

In this case you will need to use the "Pivot Column" option on the Country Column and selecting as "Values Column" the Column Measure:

jpessoa8_0-1701194477673.pngjpessoa8_1-1701194494280.png

 

This will be the result:

jpessoa8_2-1701194530173.png

 

Here is the code of my implementation in PowerQuery:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMlTSUXJ3AhKGZkqxOnAhtyAgYWSMLOTnDlIFFzKCabREFgHrMzZCFgLrMzKBCRnD9BmZIgtBNBogC0EsNFeKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Year = _t, Country = _t, Measure = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Measure", Int64.Type}}),
    #"Pivoted Column" = Table.Pivot(#"Changed Type", List.Distinct(#"Changed Type"[Country]), "Country", "Measure", List.Sum)
in
    #"Pivoted Column"

 

Hope this answer solves your problem!
If you need any additional help please @ me in your reply.
If my reply provided you with a solution, please consider marking it as a solution ✔️ or giving it a kudo 👍
Thanks!

You can also check out my LinkedIn!

Best regards,
Jorge Pessoa

 

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.

Top Solution Authors
Top Kudoed Authors