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 / Pivot to include multiple columns?

Hi all,

 

I cant work out if I need to transpose / unpivot or something else for the following, maybe having a brain freeze moment here. All help appreciated as always:

 

I have a data table as such

 

NameYearDeliveredAuthorised% Delivered/Authorised
A201831300%
A201962300%
A202031300%
B20189010900%
B20193333100%
B202091090%

 

But I want to unpivot the data so the year column runs along the top and it looks like below

 

 2018  2019  2020  
A31300%62300%31300%
B9010900%3333100%91090%

 

I can't work out why this is so complicated, feel like I'm maybe overthinking, would be grateful for any help and logic here, thanks!

1 ACCEPTED SOLUTION
selimovd
Super User
Super User

Hey @Anonymous ,

 

I think to perform that you have to merge the 3 columns first, do a pivot for column Year and then split them again afterwards.

Check the following M-Query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTIyMLQAUsZAbAiiDQxUlWJ14HKWQMoMxMSUMzLAps8JYaYlSIGhAZiFKgsy1dgYRhiiyoLNtURoBcrFAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, Year = _t, Delivered = _t, Authorised = _t, #"% Delivered/Authorised" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Year", Int64.Type}, {"Delivered", Int64.Type}, {"Authorised", Int64.Type}, {"% Delivered/Authorised", Percentage.Type}}),
    #"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Changed Type", {{"Delivered", type text}, {"Authorised", type text}, {"% Delivered/Authorised", type text}}, "de-CH"),{"Delivered", "Authorised", "% Delivered/Authorised"},Combiner.CombineTextByDelimiter("_", QuoteStyle.None),"Merged"),
    #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Merged Columns", {{"Year", type text}}, "de-CH"), List.Distinct(Table.TransformColumnTypes(#"Merged Columns", {{"Year", type text}}, "de-CH")[Year]), "Year", "Merged"),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Pivoted Column", "2018", Splitter.SplitTextByDelimiter("_", QuoteStyle.Csv), {"2018.1", "2018.2", "2018.3"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"2018.1", Int64.Type}, {"2018.2", Int64.Type}, {"2018.3", Int64.Type}}),
    #"Split Column by Delimiter1" = Table.SplitColumn(#"Changed Type1", "2019", Splitter.SplitTextByDelimiter("_", QuoteStyle.Csv), {"2019.1", "2019.2", "2019.3"}),
    #"Changed Type2" = Table.TransformColumnTypes(#"Split Column by Delimiter1",{{"2019.1", Int64.Type}, {"2019.2", Int64.Type}, {"2019.3", Int64.Type}}),
    #"Split Column by Delimiter2" = Table.SplitColumn(#"Changed Type2", "2020", Splitter.SplitTextByDelimiter("_", QuoteStyle.Csv), {"2020.1", "2020.2", "2020.3"}),
    #"Changed Type3" = Table.TransformColumnTypes(#"Split Column by Delimiter2",{{"2020.1", Int64.Type}, {"2020.2", Int64.Type}, {"2020.3", type number}})
in
    #"Changed Type3"

 

If you need any help please let me know.
If I answered your question I would be happy if you could mark my post as a solution ✔️ and give it a thumbs up 👍
 
Best regards
Denis
 

View solution in original post

2 REPLIES 2
selimovd
Super User
Super User

Hey @Anonymous ,

 

I think to perform that you have to merge the 3 columns first, do a pivot for column Year and then split them again afterwards.

Check the following M-Query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTIyMLQAUsZAbAiiDQxUlWJ14HKWQMoMxMSUMzLAps8JYaYlSIGhAZiFKgsy1dgYRhiiyoLNtURoBcrFAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, Year = _t, Delivered = _t, Authorised = _t, #"% Delivered/Authorised" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Year", Int64.Type}, {"Delivered", Int64.Type}, {"Authorised", Int64.Type}, {"% Delivered/Authorised", Percentage.Type}}),
    #"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Changed Type", {{"Delivered", type text}, {"Authorised", type text}, {"% Delivered/Authorised", type text}}, "de-CH"),{"Delivered", "Authorised", "% Delivered/Authorised"},Combiner.CombineTextByDelimiter("_", QuoteStyle.None),"Merged"),
    #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Merged Columns", {{"Year", type text}}, "de-CH"), List.Distinct(Table.TransformColumnTypes(#"Merged Columns", {{"Year", type text}}, "de-CH")[Year]), "Year", "Merged"),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Pivoted Column", "2018", Splitter.SplitTextByDelimiter("_", QuoteStyle.Csv), {"2018.1", "2018.2", "2018.3"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"2018.1", Int64.Type}, {"2018.2", Int64.Type}, {"2018.3", Int64.Type}}),
    #"Split Column by Delimiter1" = Table.SplitColumn(#"Changed Type1", "2019", Splitter.SplitTextByDelimiter("_", QuoteStyle.Csv), {"2019.1", "2019.2", "2019.3"}),
    #"Changed Type2" = Table.TransformColumnTypes(#"Split Column by Delimiter1",{{"2019.1", Int64.Type}, {"2019.2", Int64.Type}, {"2019.3", Int64.Type}}),
    #"Split Column by Delimiter2" = Table.SplitColumn(#"Changed Type2", "2020", Splitter.SplitTextByDelimiter("_", QuoteStyle.Csv), {"2020.1", "2020.2", "2020.3"}),
    #"Changed Type3" = Table.TransformColumnTypes(#"Split Column by Delimiter2",{{"2020.1", Int64.Type}, {"2020.2", Int64.Type}, {"2020.3", type number}})
in
    #"Changed Type3"

 

If you need any help please let me know.
If I answered your question I would be happy if you could mark my post as a solution ✔️ and give it a thumbs up 👍
 
Best regards
Denis
 
Anonymous
Not applicable

This solved it,

Many thanks! 

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.