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
Anonymous
Not applicable

grouping 5 data columns by Max, with month of max

My query has the following data:

 

Account NameMonthValue 1Value 2Value 3Value 4Value 5
Company A7/1/20205321110
Company A8/1/20201240552244
Company A9/1/2020260333755
Company A10/1/20201933205022
Company A11/1/2020452244117
Company A12/1/20204117709003
Company A1/1/2021713378
Company A2/1/202190963498
Company A3/1/2021104448
Company A4/1/2021228684
Company A5/1/202140878997
Company A6/1/2021249228
Company B7/1/20205321110
Company B8/1/20201240552244
Company B9/1/202026033777464
Company B10/1/20201933205022
Company B11/1/2020452244117
Company B12/1/20204117793
Company B1/1/2021713378
Company B2/1/202112359963498
Company B3/1/2021104448
Company B4/1/2021228684
Company B5/1/202140878997
Company B6/1/2021249228

 

The end result I need is the Max value for each of the 5 value columns for each Account, but I also need the Month of the Max value for each Value column.  I've tried the Grouping option and it can give me the max for all 5 broken down by Account Name, but I can't figure out how to get it to show the Month field related to the Max value for each of the 5 value columns, per Account Name.

 

Any suggestions?

1 ACCEPTED SOLUTION
mahoneypat
Microsoft Employee
Microsoft Employee

Here's one way to do it in the query editor.  To see how it works, just create a blank query, open the Advanced Editor and replace the text there with the M code below.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lZI9c8MgDIb/io85d0GAjRmjzu3YxZfBl8tdh8bukAz590VgPlq1aRkwku95JQHvNInD6bTelmv3Ml/OYiee1+X65vfX+f127iBHKkc6RyZHvTjuJvG0Xj7m5d4d/H+7h72SSvqw94tEVAKoIkiGjwUH4kwQklKF1DCFKwoiBgo0tdE2Sr8LQFY9XKLjhDI2YhIoElMPsx3EcoWqFBmzlDoZRuSSqIj1fJomo2RktCq0C1Xp7Hrr5jivCw9pqLQ4bQodjjpS9W3nT9AXOrwXUZY+zv14OUNVPU1cOtU0tjkImx2EjxxkLd29Gbik0UPY7CH83UPbbWnO/9tA+NVAoAjr3SMPYZOHsMlD2Ogh/NtDx08=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"(blank)" = _t, #"(blank).1" = _t, #"(blank).2" = _t, #"(blank).3" = _t, #"(blank).4" = _t, #"(blank).5" = _t, #"(blank).6" = _t]),
    #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Month", type date}, {"Value 1", Int64.Type}, {"Value 2", Int64.Type}, {"Value 3", Int64.Type}, {"Value 4", Int64.Type}, {"Value 5", Int64.Type}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Account Name", "Month"}, "Attribute", "Value"),
    #"Grouped Rows" = Table.Group(#"Unpivoted Other Columns", {"Account Name", "Attribute"}, {{"AllRows", each _, type table [Account Name=nullable text, Month=nullable date, Attribute=text, Value=number]}}),
    Custom1 = Table.TransformColumns(#"Grouped Rows", {{"AllRows", each Table.Max(_, {"Value"})}}),
    #"Expanded AllRows" = Table.ExpandRecordColumn(Custom1, "AllRows", {"Month", "Value"}, {"Month", "Value"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded AllRows",{{"Month", type date}, {"Value", Int64.Type}})
in
    #"Changed Type1"

 

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

3 REPLIES 3
mahoneypat
Microsoft Employee
Microsoft Employee

Here's one way to do it in the query editor.  To see how it works, just create a blank query, open the Advanced Editor and replace the text there with the M code below.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lZI9c8MgDIb/io85d0GAjRmjzu3YxZfBl8tdh8bukAz590VgPlq1aRkwku95JQHvNInD6bTelmv3Ml/OYiee1+X65vfX+f127iBHKkc6RyZHvTjuJvG0Xj7m5d4d/H+7h72SSvqw94tEVAKoIkiGjwUH4kwQklKF1DCFKwoiBgo0tdE2Sr8LQFY9XKLjhDI2YhIoElMPsx3EcoWqFBmzlDoZRuSSqIj1fJomo2RktCq0C1Xp7Hrr5jivCw9pqLQ4bQodjjpS9W3nT9AXOrwXUZY+zv14OUNVPU1cOtU0tjkImx2EjxxkLd29Gbik0UPY7CH83UPbbWnO/9tA+NVAoAjr3SMPYZOHsMlD2Ogh/NtDx08=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"(blank)" = _t, #"(blank).1" = _t, #"(blank).2" = _t, #"(blank).3" = _t, #"(blank).4" = _t, #"(blank).5" = _t, #"(blank).6" = _t]),
    #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Month", type date}, {"Value 1", Int64.Type}, {"Value 2", Int64.Type}, {"Value 3", Int64.Type}, {"Value 4", Int64.Type}, {"Value 5", Int64.Type}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Account Name", "Month"}, "Attribute", "Value"),
    #"Grouped Rows" = Table.Group(#"Unpivoted Other Columns", {"Account Name", "Attribute"}, {{"AllRows", each _, type table [Account Name=nullable text, Month=nullable date, Attribute=text, Value=number]}}),
    Custom1 = Table.TransformColumns(#"Grouped Rows", {{"AllRows", each Table.Max(_, {"Value"})}}),
    #"Expanded AllRows" = Table.ExpandRecordColumn(Custom1, "AllRows", {"Month", "Value"}, {"Month", "Value"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded AllRows",{{"Month", type date}, {"Value", Int64.Type}})
in
    #"Changed Type1"

 

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Anonymous
Not applicable

Thanks Pat, that did it!!

amitchandak
Super User
Super User

@Anonymous ,

Column like this for each column ?

flag =
var _max = maxx(filter(Table, [Account Name] = earlier([Account Name])), [Value1])
return
if([Value1] =_max, true(), false())

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

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 Solution Authors