Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Rahul_SC
Helper IV
Helper IV

How to create excel pivot table in power query

Hi @KT_Bsmart2gethe , and all,

 

Is it possible to create pivot table in power query ? like below, I have a long list of data, what I want to do is create a pivot table using it. In excel, I put, super region in row field, AbsoluteStart_Month in column feild then in value feild last two columns.

 

Group by option can help but in column field, how can I bring 2nd column in Column field option that we can see in excel pivot table or in Power bi Matrix table ?

 

SuperRegionAbsoluteStart_MonthCallCountTotalTEU
Africa1802154827452
Asia1803154829116
Europe1804159851778
Africa1805169909015
Africa1801171907619
Africa1806171886772
2 ACCEPTED SOLUTIONS
KT_Bsmart2gethe
Impactful Individual
Impactful Individual

Hi Rahul,

 

Let me know if this is what you are looking for:

 

Outcome

KT_Bsmart2gethe_1-1661205004504.png

 

 

Step 1: GroupBy

 

Table.Group(#"Promoted Headers", {"SuperRegion"}, {{"1", each Table.PrefixColumns(Table.FirstN(Table.PromoteHeaders(Table.Transpose(Table.DemoteHeaders(Table.RemoveColumns(_, {"SuperRegion"})))),1), "CallCount")}, {"2", each Table.PrefixColumns(Table.LastN(Table.PromoteHeaders(Table.Transpose(Table.DemoteHeaders(Table.RemoveColumns(_, {"SuperRegion"})))),1), "TotalTEU")}})

KT_Bsmart2gethe_0-1661204880613.png

 

Step 2 and 3:

 

Expand the columns

 

Regards

KT

View solution in original post

v-jingzhang
Community Support
Community Support

Hi @Rahul_SC 

 

It is recommended to keep the table a flat table in Power Query rather than pivot it. This is usually more friendly for the downstream calculation and report design in Power BI or somewhere else. 

 

However, if you want to pivot it, you can try the pivot/unpivot feature in Power Query. First select last two columns and click on Unpivot Columns to unpivot them. 

vjingzhang_0-1661308276357.png

 

Add a custom column to concatenate AbsoluteStart_Month and Attribute columns. 

vjingzhang_1-1661308382351.png

 

Remove AbsoluteStart_Month and Attribute columns. Select the custom column and pivot it. When pivoting, select Value column as the Values. 

vjingzhang_2-1661308603537.png

 

You will get the following result then. Actually this is still a flat table as Power Query only supports flat tables. It cannot hold multiple values under one column value in Power Query Editor. 

vjingzhang_3-1661308760668.png

 

Full M code

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Zc49CoAwDAXgu3Tu0NTmb3TwFOIgouCkVLy/TRURXPKG94Wk71275HUanXcgIVpgKlMiJ4xu8AUc61M3n1oBqNbdmbd9vkGqQA0gMMu9/zmAFmRAgwbAHwALhgqYQH+AXiBCzOXF4QI=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [SuperRegion = _t, AbsoluteStart_Month = _t, CallCount = _t, TotalTEU = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"SuperRegion", type text}, {"AbsoluteStart_Month", Int64.Type}, {"CallCount", Int64.Type}, {"TotalTEU", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"SuperRegion", "AbsoluteStart_Month"}, "Attribute", "Value"),
    #"Added Custom" = Table.AddColumn(#"Unpivoted Columns", "Custom", each Text.From([AbsoluteStart_Month]) & [Attribute]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"AbsoluteStart_Month", "Attribute"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Custom]), "Custom", "Value", List.Sum)
in
    #"Pivoted Column"

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

View solution in original post

3 REPLIES 3
Rahul_SC
Helper IV
Helper IV

@KT_Bsmart2gethe @v-jingzhang , both the solutions work. Thanks so much!

v-jingzhang
Community Support
Community Support

Hi @Rahul_SC 

 

It is recommended to keep the table a flat table in Power Query rather than pivot it. This is usually more friendly for the downstream calculation and report design in Power BI or somewhere else. 

 

However, if you want to pivot it, you can try the pivot/unpivot feature in Power Query. First select last two columns and click on Unpivot Columns to unpivot them. 

vjingzhang_0-1661308276357.png

 

Add a custom column to concatenate AbsoluteStart_Month and Attribute columns. 

vjingzhang_1-1661308382351.png

 

Remove AbsoluteStart_Month and Attribute columns. Select the custom column and pivot it. When pivoting, select Value column as the Values. 

vjingzhang_2-1661308603537.png

 

You will get the following result then. Actually this is still a flat table as Power Query only supports flat tables. It cannot hold multiple values under one column value in Power Query Editor. 

vjingzhang_3-1661308760668.png

 

Full M code

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Zc49CoAwDAXgu3Tu0NTmb3TwFOIgouCkVLy/TRURXPKG94Wk71275HUanXcgIVpgKlMiJ4xu8AUc61M3n1oBqNbdmbd9vkGqQA0gMMu9/zmAFmRAgwbAHwALhgqYQH+AXiBCzOXF4QI=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [SuperRegion = _t, AbsoluteStart_Month = _t, CallCount = _t, TotalTEU = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"SuperRegion", type text}, {"AbsoluteStart_Month", Int64.Type}, {"CallCount", Int64.Type}, {"TotalTEU", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"SuperRegion", "AbsoluteStart_Month"}, "Attribute", "Value"),
    #"Added Custom" = Table.AddColumn(#"Unpivoted Columns", "Custom", each Text.From([AbsoluteStart_Month]) & [Attribute]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"AbsoluteStart_Month", "Attribute"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Custom]), "Custom", "Value", List.Sum)
in
    #"Pivoted Column"

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

KT_Bsmart2gethe
Impactful Individual
Impactful Individual

Hi Rahul,

 

Let me know if this is what you are looking for:

 

Outcome

KT_Bsmart2gethe_1-1661205004504.png

 

 

Step 1: GroupBy

 

Table.Group(#"Promoted Headers", {"SuperRegion"}, {{"1", each Table.PrefixColumns(Table.FirstN(Table.PromoteHeaders(Table.Transpose(Table.DemoteHeaders(Table.RemoveColumns(_, {"SuperRegion"})))),1), "CallCount")}, {"2", each Table.PrefixColumns(Table.LastN(Table.PromoteHeaders(Table.Transpose(Table.DemoteHeaders(Table.RemoveColumns(_, {"SuperRegion"})))),1), "TotalTEU")}})

KT_Bsmart2gethe_0-1661204880613.png

 

Step 2 and 3:

 

Expand the columns

 

Regards

KT

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.

Top Solution Authors