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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Anonymous
Not applicable

Find The Highest aggregated option value

I have a table like this

 

LeadOption 1Value
1Option 1200
1Option 2100
1Option 2100
2Option 1200
2Option 1100
3Option 1200
3Option 1100

 

As it can be seen, each Lead can have multiple option and each option can have multiple line. 

So I need to find the highest value of each sumed option, ideally to a table like this

LeadBest (highest value) OptionCorresponding Value
1Option 2Sum
2Option 1Sum
3Option 1Sum

 

How should I do it in Power BI ? Do I need 2 measue one for the Best Option Column and another for Corresponding value column ?

 

If possible can I have the solution for Power Query as well ?

1 ACCEPTED SOLUTION

Hi, @Anonymous ,

Add another step -group by.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSgWADA6VYHQjfCIShfCOovDES3wjGjwUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Lead = _t, #"Option 1" = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Lead", Int64.Type}, {"Option 1", type text}, {"Value", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Lead"}, {{"maxvalue", each List.Max([Value]), type nullable number}, {"all", each _, type table [Lead=nullable number, Option 1=nullable text, Value=nullable number]}}),
    #"Expanded all1" = Table.ExpandTableColumn(#"Grouped Rows", "all", {"Option 1", "Value"}, {"all.Option 1", "all.Value"}),
    #"Added Conditional Column" = Table.AddColumn(#"Expanded all1", "Custom", each if [maxvalue] = [all.Value] then 1 else 0),
    #"Filtered Rows" = Table.SelectRows(#"Added Conditional Column", each ([Custom] = 1)),
    #"Grouped Rows1" = Table.Group(#"Filtered Rows", {"Lead"}, {{"Minoption", each List.Min([all.Option 1]), type nullable text}, {"max", each List.Max([maxvalue]), type nullable number}})
in
    #"Grouped Rows1"

The final show:

vyalanwumsft_0-1656484339781.png


Best Regards,
Community Support Team _ Yalan Wu
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
v-yalanwu-msft
Community Support
Community Support

Hi, @Anonymous ;

Method 1 In power query.

1.group by 

vyalanwumsft_0-1656052983565.png

2.add custom column.

let _Lead = [Lead], _option = [maxoption]
in List.Sum(Table.SelectRows(#"Changed Type", each [Lead] = _Lead and [Option 1] = _option)[Value])

See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUfIvKMnMz1MAMY0MDJRidVCEjYBMQ/zCRtgNQROGqTbGrtoYi+pYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Lead = _t, #"Option 1" = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Lead", Int64.Type}, {"Option 1", type text}, {"Value", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Lead"}, {{"maxoption", each List.Max([Option 1]), type nullable text}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each let _Lead = [Lead], _option = [maxoption]
in List.Sum(Table.SelectRows(#"Changed Type", each [Lead] = _Lead and [Option 1] = _option)[Value]))
in
    #"Added Custom"

Method 2 in power query.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUfIvKMnMz1MAMY0MDJRidVCEjYBMQ/zCRtgNQROGqTbGrtoYi+pYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Lead = _t, #"Option 1" = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Lead", Int64.Type}, {"Option 1", type text}, {"Value", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Lead"}, {{"maxoption", each List.Max([Option 1]), type nullable text}, {"all", each _, type table [Lead=nullable number, Option 1=nullable text, Value=nullable number]}}),
    #"Expanded all" = Table.ExpandTableColumn(#"Grouped Rows", "all", {"Option 1", "Value"}, {"Option 1", "Value"}),
    #"Added Conditional Column" = Table.AddColumn(#"Expanded all", "Custom", each if [maxoption] = [Option 1] then 1 else 0),
    #"Filtered Rows" = Table.SelectRows(#"Added Conditional Column", each ([Custom] = 1)),
    #"Grouped Rows1" = Table.Group(#"Filtered Rows", {"Lead", "Option 1"}, {{"sumv", each List.Sum([Value]), type nullable number}})
in
    #"Grouped Rows1"

The final show:

vyalanwumsft_1-1656053213217.png


Best Regards,
Community Support Team _ Yalan Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Thanks!

I actually managed to did it by grouping.

1. Group to find sum of each option 

2. Group again to find the highest option value of each lead

The key is to insert  A new column name that has operation as "All Row" to expand out again though. 

 

However now I'm having a new problem: 

My new table after grouping and expanding out

LeadOptionOption ValueMax Option Value
11100200
12200200
21300300
22300300

 

As the above example, it's easy enough for lead 1 to add a custom column and compare Option value vs Max Option value to highlight the best one, but what should I do in the case of Lead 2, option 1 equal option 2 ? I would want to do something like if all option of a lead equal in value then pick option 1. 

Hi, @Anonymous ,

Add another step -group by.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSgWADA6VYHQjfCIShfCOovDES3wjGjwUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Lead = _t, #"Option 1" = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Lead", Int64.Type}, {"Option 1", type text}, {"Value", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Lead"}, {{"maxvalue", each List.Max([Value]), type nullable number}, {"all", each _, type table [Lead=nullable number, Option 1=nullable text, Value=nullable number]}}),
    #"Expanded all1" = Table.ExpandTableColumn(#"Grouped Rows", "all", {"Option 1", "Value"}, {"all.Option 1", "all.Value"}),
    #"Added Conditional Column" = Table.AddColumn(#"Expanded all1", "Custom", each if [maxvalue] = [all.Value] then 1 else 0),
    #"Filtered Rows" = Table.SelectRows(#"Added Conditional Column", each ([Custom] = 1)),
    #"Grouped Rows1" = Table.Group(#"Filtered Rows", {"Lead"}, {{"Minoption", each List.Min([all.Option 1]), type nullable text}, {"max", each List.Max([maxvalue]), type nullable number}})
in
    #"Grouped Rows1"

The final show:

vyalanwumsft_0-1656484339781.png


Best Regards,
Community Support Team _ Yalan Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

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.