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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
rdg515
Resolver I
Resolver I

Percent of Total, Aggregated by Customer, then Ranked

In Power Query I want to create a ranking of Percent of Total Margin by Customer.

 

In this step I'm aggregating Margin by Customer then trying to add another column for the Total Margin for the entire Source table

 

= Table.AddColumn( Table.Group(Source, {"Customer"}, {{"Margin", each List.Sum([Margin]), type nullable number}}), "Total Margin", List.Sum(Source[Margin]))

 

Unfortunately, I'm getting an error: 

Expression.Error: We cannot convert the value 18472392.070000324 to type Function.
Details:
Value=18472392.07
Type=[Type] 

1 ACCEPTED SOLUTION

Thank you this is exactly what I was looking for!  I made a change to the ranking calculation because, while your suggestion preserved the order of the Customer IDs, the order wasn't important and this way performed much faster for me against my real data set.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUTIyMFCK1YlWSgKyjU0h7GSQOJSdAmTrGoMUxQIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Customer ID" = _t, Margin = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer ID", type text}, {"Margin", Int64.Type}}),
    TotalM = Table.AddColumn(#"Changed Type", "TotalM", each List.Sum(#"Changed Type"[Margin])),
    pcTM = Table.AddColumn(TotalM, "pcTM", each [Margin]/List.Sum(#"Changed Type"[Margin]), type number ),
    #"Changed Type1" = Table.TransformColumnTypes(pcTM,{{"pcTM", Percentage.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type1",{{"pcTM", Order.Descending}}),
    #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 1, 1, Int64.Type)
in
    #"Added Index"

 

View solution in original post

7 REPLIES 7
Anonymous
Not applicable

only from a strictly syntactic point of view can I tell you that the third parameter of the Table.addcolumn function must be a function that expects the rows of the table as input.

 

Table.AddColumn(table as table, newColumnName as text, columnGenerator as function, optional columnType as nullable type) as table

 

 Quindi questa:

 

 

 

= Table.AddColumn( Table.Group(Source, {"Customer"}, {{"Margin", each List.Sum([Margin]), type nullable number}}), "Total Margin", List.Sum(Source[Margin]))

 

 

 

 

dovrebbe cambiare in:

 

 

 

 

= Table.AddColumn( Table.Group(Source, {"Customer"}, {{"Margin", each List.Sum([Margin]), type nullable number}}), "Total Margin", (row)=> doSomethinkWithTheRow )

 

 

 

 

 

 

if you want specific help, post your starting data and desired data in an easily copyable form.

 

 

ImkeF
Community Champion
Community Champion

Hi @rdg515 ,

if my understanding is correct, all you have to do is to add the each keyword to your formula:

 

Table.AddColumn( Table.Group(Source, {"Customer"}, {{"Margin", each List.Sum([Margin]), type nullable number}}), "Total Margin", each List.Sum(Source[Margin])) 

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

This solution does give the correct answer but it takes forever.  I was thinking it takes forever because it calculates a sum of the column for each row instead of just doing it once.  Conceptually I'd like to just sum up the entire column, then join that result as a new column to my summarized table and provide that result for every row.

 

The following code:

 

= Table.Group(Source, {"Customer"}, {{"Margin", each List.Sum([Margin])}})

 

 

Produces results that look like this:

Customer IDMargin
a200
b350
c250
d-300

 

The next step I'm working on is to get this:

Customer IDMarginTotal Margin
a200500
b350500
c250500
d-300500

 

I want the final result to look like this:

Customer ID% of Total MarginRank
a40%3
b70%1
c50%2
d-60%4

 

This table will be used to create a whale curve which requires Customers IDs to be ranked on the x-axis (can't put a measure on the axis), and the values will be a Running Total of % of Total Margin.  The line chart will show an increase relative to customers that had positive margin, then a decrease for customers with negative margin, ending and reconciling at 100%. 

Anonymous
Not applicable

try this

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUTIyMFCK1YlWSgKyjU0h7GSQOJSdAmTrGoMUxQIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Customer ID" = _t, Margin = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer ID", type text}, {"Margin", Int64.Type}}),
    TotalM = Table.AddColumn(#"Changed Type", "TotalM", each List.Sum(#"Changed Type"[Margin])),
    pcTM = Table.AddColumn(TotalM, "pcTM", each [Margin]/List.Sum(#"Changed Type"[Margin]), type number ),
    #"Changed Type1" = Table.TransformColumnTypes(pcTM,{{"pcTM", Percentage.Type}}),
    rank = Table.AddColumn(#"Changed Type1", "rank", each List.Count(#"Changed Type"[Margin])-List.PositionOf(List.Sort(#"Changed Type"[Margin]),[Margin]))
in
    rank

Thank you this is exactly what I was looking for!  I made a change to the ranking calculation because, while your suggestion preserved the order of the Customer IDs, the order wasn't important and this way performed much faster for me against my real data set.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUTIyMFCK1YlWSgKyjU0h7GSQOJSdAmTrGoMUxQIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Customer ID" = _t, Margin = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer ID", type text}, {"Margin", Int64.Type}}),
    TotalM = Table.AddColumn(#"Changed Type", "TotalM", each List.Sum(#"Changed Type"[Margin])),
    pcTM = Table.AddColumn(TotalM, "pcTM", each [Margin]/List.Sum(#"Changed Type"[Margin]), type number ),
    #"Changed Type1" = Table.TransformColumnTypes(pcTM,{{"pcTM", Percentage.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type1",{{"pcTM", Order.Descending}}),
    #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 1, 1, Int64.Type)
in
    #"Added Index"

 

@rdg515 - if one of these isn't the answer, can you post some sample data please, with expected results?

How to get good help fast. Help us help you.
How to Get Your Question Answered Quickly
How to provide sample data in the Power BI Forum



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
Greg_Deckler
Community Champion
Community Champion

@rdg515 Seems like this would be straight-forward in DAX but not sure how to rank in Power Query but I am sure someone like @ImkeF and @edhans know.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.