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

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

Reply
skrishpv
New Member

How to find group name with max value based on Sum after group by. Also, for each year and product

I have a dataset like the one below,

skrishpv_0-1646253590240.png

Now I want to know for each year which salesperson made the most sales for each product. Like in 2018 most sales for Product 123 was made by John. 

This value has to be displayed against each row for product 123 in year 2018.Like below

skrishpv_1-1646253700175.png

I need this to create a table visual eventually. 

Any help would be greatly appreciated!!

 

Thank you!

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @skrishpv ,

 

1. Choose Groupby :

Eyelyn9_0-1646633800171.png

2.Add a Custom column:

Eyelyn9_1-1646633838818.png

3. Then expand the "Table" and "Record" to keep the necessary columns.

 

Here is the whole M syntax:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwtFDSUTI0MgaRBgZA0is/I08pVgcsZwkSNTEFkkZgueDEXJgUQhuGFEKXKbqBCF3GeCwzwWoZWAqHG7E7xAK38xG6THFqMsatyQTJGbEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Year = _t, ProductID = _t, Sales = _t, Salesperson = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Year", Int64.Type}, {"ProductID", Int64.Type}, {"Sales", Int64.Type}, {"Salesperson", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Year", "ProductID"}, {{"All Rows", each _, type table [Year=nullable number, ProductID=nullable number, Sales=nullable number, Salesperson=nullable text]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.Max([All Rows],"Sales")),
    #"Expanded Custom" = Table.ExpandRecordColumn(#"Added Custom", "Custom", {"Salesperson"}, {"Who sold max.Salesperson"}),
    #"Expanded All Rows" = Table.ExpandTableColumn(#"Expanded Custom", "All Rows", {"Sales", "Salesperson"}, {"Sales", "Salesperson"})
in
    #"Expanded All Rows"

 

Final output:

Eyelyn9_3-1646633988912.png

 

Or you could create a measure using  DAX:

Measure = 
var _max=MAXX(FILTER(ALL('Table'),[Year]=MAX('Table'[Year]) && [ProductID]=MAX('Table'[ProductID])),[Sales])
return CALCULATE(MAX('Table'[Salesperson]),FILTER(ALL('Table'),[Year]=MAX('Table'[Year]) && [ProductID]=MAX('Table'[ProductID])&&[Sales]=_max))

 

 

Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

Hi @skrishpv ,

 

1. Choose Groupby :

Eyelyn9_0-1646633800171.png

2.Add a Custom column:

Eyelyn9_1-1646633838818.png

3. Then expand the "Table" and "Record" to keep the necessary columns.

 

Here is the whole M syntax:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwtFDSUTI0MgaRBgZA0is/I08pVgcsZwkSNTEFkkZgueDEXJgUQhuGFEKXKbqBCF3GeCwzwWoZWAqHG7E7xAK38xG6THFqMsatyQTJGbEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Year = _t, ProductID = _t, Sales = _t, Salesperson = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Year", Int64.Type}, {"ProductID", Int64.Type}, {"Sales", Int64.Type}, {"Salesperson", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Year", "ProductID"}, {{"All Rows", each _, type table [Year=nullable number, ProductID=nullable number, Sales=nullable number, Salesperson=nullable text]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.Max([All Rows],"Sales")),
    #"Expanded Custom" = Table.ExpandRecordColumn(#"Added Custom", "Custom", {"Salesperson"}, {"Who sold max.Salesperson"}),
    #"Expanded All Rows" = Table.ExpandTableColumn(#"Expanded Custom", "All Rows", {"Sales", "Salesperson"}, {"Sales", "Salesperson"})
in
    #"Expanded All Rows"

 

Final output:

Eyelyn9_3-1646633988912.png

 

Or you could create a measure using  DAX:

Measure = 
var _max=MAXX(FILTER(ALL('Table'),[Year]=MAX('Table'[Year]) && [ProductID]=MAX('Table'[ProductID])),[Sales])
return CALCULATE(MAX('Table'[Salesperson]),FILTER(ALL('Table'),[Year]=MAX('Table'[Year]) && [ProductID]=MAX('Table'[ProductID])&&[Sales]=_max))

 

 

Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

You can group on year and product ID, use the Sim aggregation on the Sales column, name the column Sum_Sales, and also make an AllRows column, named Details.

Now you can add custom column:

 

Taable.AddColumn(PriorStepOrTableName,"Top", each Table.Max(_[Details], "Sum_Sales")[[Sum_Sales], [SalesPerson]])

Then you can expand the new table column.

 

--Nate

Hi Nate,

 

Thanks for your response.

I am getting below error when creating the custom column,

Expression.Error: The specified sort criteria is invalid.
Details:
Sum_Sales

 

Could you help me understand this?

Helpful resources

Announcements
October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

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.

Top Kudoed Authors