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

View all the Fabric Data Days sessions on demand. View schedule

Reply
k1s1
Helper I
Helper I

Pivot showing percentage of column total

Hello,

 

I'm stil trying to get my head round when & how to pivot colmuns

 

If my data looks like the following:

Q1Q2Q3Q4
NeutralLikeDislikeLike
DislikeLikeLikeDislike
DislikeLike  
Like LikeLike
DislikeDislikeLikeDislike
 Dislike  
LikeNeutralLikeLike
 LikeDislikeLike

 

How should I pivot to get counts of the number of each type (excluding blanks), showing the percentage of the column total, so that the ouput looks like this?:

Column1Q1Q2Q3Q4
Like33%57%67%67%
Dislike50%28%33%33%
Neutral17%14%  

 

The steps I used are first to replace the 'empty' cells with null, then add an index column, then slect the other columns and click Unpivot, to get something like below. 

IndexAttributeValue
0Q1Neutral
0Q2Like
0Q3Dislike
0Q4Like
1Q1Dislike
1Q2Like
1Q3Like
1Q4Dislike
2Q1Dislike
2Q2Like
2Q3 
2Q4 
3Q1Like
3Q2 
3Q3Like
3Q4Like
4Q1Dislike
4Q2Dislike
4Q3Like
4Q4Dislike
5Q1 
5Q2Dislike
5Q3 
5Q4 
6Q1Like
6Q2Neutral
6Q3Like
6Q4Like
7Q1 
7Q2Like
7Q3Dislike
7Q4Like

 

Then I highlighted the Index column, and selected pivot from the GUI. Under advanced options I selected "Count (Not Blank)". ..which gives me the interim table:

Column1Q1Q2Q3Q4
Like2444
Dislike3222
Neutral11  

 

But how can I get these values to be percentages of the column total instead?

 

Here's my code so far:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8kstLSlKzFHSUfLJzE4FUi6ZxTkQFlggVicaXQhDKVY1IASSQHB9cBqLoRnZZBR5NFMxXO+DpAuHh2IB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Q1 = _t, Q2 = _t, Q3 = _t, Q4 = _t]),
    #"Replaced Value" = Table.ReplaceValue(Source,"",null,Replacer.ReplaceValue,{"Q1", "Q2", "Q3", "Q4"}),
    #"Add dummy column upon which to pivot" = Table.AddIndexColumn(#"Replaced Value", "Highlight me to Pivot", 0, 1, Int64.Type),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Add dummy column upon which to pivot", {"Highlight me to Pivot"}, "Attribute", "Value"),
    #"Pivoted on Highlight me to Pivot" = Table.Pivot(#"Unpivoted Other Columns", List.Distinct(#"Unpivoted Other Columns"[Attribute]), "Attribute", "Highlight me to Pivot", List.NonNullCount)
in
    #"Pivoted on Highlight me to Pivot"

 

 

2 ACCEPTED SOLUTIONS
k1s1
Helper I
Helper I

I found out how to do the last step (percentage of column) total here:  https://exceloffthegrid.com/power-query-percent-of-total/

View solution in original post

k1s1
Helper I
Helper I

In case it helps somebody else, here's my code:

 

let

// Next 2 lines creates the dummy data table - replace with your source 
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8kstLSlKzFHSUfLJzE4FUi6ZxTkQFlggVicaXQhDKVY1IASSQHB9cBqLoRnZZBR5NFMxXO+DpAuHh2IB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Q1 = _t, Q2 = _t, Q3 = _t, Q4 = _t]),
    #"Replaced Value" = Table.ReplaceValue(Source,"",null,Replacer.ReplaceValue,{"Q1", "Q2", "Q3", "Q4"}),

// This doesn't need to be named as such, but avoids confusion later (The 'Values Column' in the Pivot step is NOT the column named 'Values' in the prior Unpivoting step)
    #"Add dummy column upon which to pivot" = Table.AddIndexColumn(#"Replaced Value", "Select this column as Values Column in pivot step", 0, 1, Int64.Type),

//Then select all the columns except the one just added above, a click choose unpivot columns from the Transform menu
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Add dummy column upon which to pivot", {"Select this column as Values Column in pivot step"}, "Attribute", "Value"),
 
 // Next step isn't necessary - just makes it more obvious to avoid the miselecting the Values column
    #"Renamed Columns" = Table.RenameColumns(#"Unpivoted Other Columns",{{"Attribute", "Attribute highlight this column to pivot"}}),
 
// Then highlight /select the Attribute highlight this column to pivot, then Transform > Pivot from the menu.  elect the dummy / index column as the Values Column (named earlier: "Select this column as Values Column in pivot step") 
    #"Pivoted Attribute Column" = Table.Pivot(#"Renamed Columns", List.Distinct(#"Renamed Columns"[#"Attribute highlight this column to pivot"]), "Attribute highlight this column to pivot", "Select this column as Values Column in pivot step", List.NonNullCount),

//Now to add a percentage of column total for each:
    #"Added Custom" = Table.AddColumn(#"Pivoted Attribute Column", "Q1 % of column", each [Q1]/List.Sum(#"Pivoted Attribute Column"[Q1])),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Q2 % of column", each [Q2]/List.Sum(#"Added Custom"[Q2])),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Q3 % of column", each [Q3]/List.Sum(#"Added Custom"[Q3])),
    #"Added Custom3" = Table.AddColumn(#"Added Custom2", "Q4 % of column", each [Q4]/List.Sum(#"Added Custom2"[Q4])),
    #"Changed Type" = Table.TransformColumnTypes(#"Added Custom3",{{"Q1 % of column", Percentage.Type}, {"Q2 % of column", Percentage.Type}, {"Q3 % of column", Percentage.Type}, {"Q4 % of column", Percentage.Type}})
in
    #"Changed Type"

 

 

(Paste this into a new /blank Query using the Advanced Editor to see how it works. 

View solution in original post

2 REPLIES 2
k1s1
Helper I
Helper I

In case it helps somebody else, here's my code:

 

let

// Next 2 lines creates the dummy data table - replace with your source 
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8kstLSlKzFHSUfLJzE4FUi6ZxTkQFlggVicaXQhDKVY1IASSQHB9cBqLoRnZZBR5NFMxXO+DpAuHh2IB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Q1 = _t, Q2 = _t, Q3 = _t, Q4 = _t]),
    #"Replaced Value" = Table.ReplaceValue(Source,"",null,Replacer.ReplaceValue,{"Q1", "Q2", "Q3", "Q4"}),

// This doesn't need to be named as such, but avoids confusion later (The 'Values Column' in the Pivot step is NOT the column named 'Values' in the prior Unpivoting step)
    #"Add dummy column upon which to pivot" = Table.AddIndexColumn(#"Replaced Value", "Select this column as Values Column in pivot step", 0, 1, Int64.Type),

//Then select all the columns except the one just added above, a click choose unpivot columns from the Transform menu
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Add dummy column upon which to pivot", {"Select this column as Values Column in pivot step"}, "Attribute", "Value"),
 
 // Next step isn't necessary - just makes it more obvious to avoid the miselecting the Values column
    #"Renamed Columns" = Table.RenameColumns(#"Unpivoted Other Columns",{{"Attribute", "Attribute highlight this column to pivot"}}),
 
// Then highlight /select the Attribute highlight this column to pivot, then Transform > Pivot from the menu.  elect the dummy / index column as the Values Column (named earlier: "Select this column as Values Column in pivot step") 
    #"Pivoted Attribute Column" = Table.Pivot(#"Renamed Columns", List.Distinct(#"Renamed Columns"[#"Attribute highlight this column to pivot"]), "Attribute highlight this column to pivot", "Select this column as Values Column in pivot step", List.NonNullCount),

//Now to add a percentage of column total for each:
    #"Added Custom" = Table.AddColumn(#"Pivoted Attribute Column", "Q1 % of column", each [Q1]/List.Sum(#"Pivoted Attribute Column"[Q1])),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Q2 % of column", each [Q2]/List.Sum(#"Added Custom"[Q2])),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Q3 % of column", each [Q3]/List.Sum(#"Added Custom"[Q3])),
    #"Added Custom3" = Table.AddColumn(#"Added Custom2", "Q4 % of column", each [Q4]/List.Sum(#"Added Custom2"[Q4])),
    #"Changed Type" = Table.TransformColumnTypes(#"Added Custom3",{{"Q1 % of column", Percentage.Type}, {"Q2 % of column", Percentage.Type}, {"Q3 % of column", Percentage.Type}, {"Q4 % of column", Percentage.Type}})
in
    #"Changed Type"

 

 

(Paste this into a new /blank Query using the Advanced Editor to see how it works. 

k1s1
Helper I
Helper I

I found out how to do the last step (percentage of column) total here:  https://exceloffthegrid.com/power-query-percent-of-total/

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.