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
aTChris
Resolver I
Resolver I

Can I select MAX value in Table.SelectRows

Hi everyone

 

I have a table of quotes from D365 CRM. During negotiation the quotes are revised to reflect the value of the deal. I need to create a table with unique quoteID's and the most recent quote which I can use the RevisionID for.

 

My thought is to use the Table.SelectRows function but I dont seem to be able to defined MAX as a condition.

e.g. 

QuoteIDQuote NameRevisionIDAmount
1Quote 1 0£25000
1Quote 1 £30000
Quote 12£27500
2Quote 2 0

£1500

2Quote 21

£2750

Results

QuoteIDQuote NameRevisionIDAmount
Quote 12£27500
2Quote 21

£2750

 

Any ideas?

 

Thanks

1 ACCEPTED SOLUTION
AlB
Community Champion
Community Champion

Hi @aTChris 

Place the following M code in a blank query to see the steps.

 

let
   Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUQoszS9JVQCxDID40GIjUwMDA6VYHXRZw0MLwPLGBnB5sAhChRFEv7kpVN4ILmsEN90Qu6QhXKtSbCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [QuoteID = _t, #"Quote Name" = _t, RevisionID = _t, Amount = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"QuoteID", Int64.Type}, {"Quote Name", type text}, {"RevisionID", Int64.Type}, {"Amount", Currency.Type}}),

    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"QuoteID", Order.Ascending}, {"RevisionID", Order.Ascending}}),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"QuoteID", "Quote Name"}, {{"Count", each List.Last([Amount]), Currency.Type}})
in
    #"Grouped Rows"

 

or alternatively:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUQoszS9JVQCxDID40GIjUwMDA6VYHXRZw0MLwPLGBnB5sAhChRFEv7kpVN4ILmsEN90Qu6QhXKtSbCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [QuoteID = _t, #"Quote Name" = _t, RevisionID = _t, Amount = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"QuoteID", Int64.Type}, {"Quote Name", type text}, {"RevisionID", Int64.Type}, {"Amount", Currency.Type}}),

    #"Grouped Rows" = Table.Group(#"Changed Type", {"QuoteID", "Quote Name"}, {{"Count", each Table.Max(_, each [RevisionID])[Amount], Currency.Type}})
in
    #"Grouped Rows"

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

SU18_powerbi_badge

View solution in original post

2 REPLIES 2
aTChris
Resolver I
Resolver I

@AlB 

Thanks for that. I think that will work. I have many other attributes in the table that I need to retain. I should be able to apply them in the grouping.

AlB
Community Champion
Community Champion

Hi @aTChris 

Place the following M code in a blank query to see the steps.

 

let
   Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUQoszS9JVQCxDID40GIjUwMDA6VYHXRZw0MLwPLGBnB5sAhChRFEv7kpVN4ILmsEN90Qu6QhXKtSbCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [QuoteID = _t, #"Quote Name" = _t, RevisionID = _t, Amount = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"QuoteID", Int64.Type}, {"Quote Name", type text}, {"RevisionID", Int64.Type}, {"Amount", Currency.Type}}),

    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"QuoteID", Order.Ascending}, {"RevisionID", Order.Ascending}}),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"QuoteID", "Quote Name"}, {{"Count", each List.Last([Amount]), Currency.Type}})
in
    #"Grouped Rows"

 

or alternatively:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUQoszS9JVQCxDID40GIjUwMDA6VYHXRZw0MLwPLGBnB5sAhChRFEv7kpVN4ILmsEN90Qu6QhXKtSbCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [QuoteID = _t, #"Quote Name" = _t, RevisionID = _t, Amount = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"QuoteID", Int64.Type}, {"Quote Name", type text}, {"RevisionID", Int64.Type}, {"Amount", Currency.Type}}),

    #"Grouped Rows" = Table.Group(#"Changed Type", {"QuoteID", "Quote Name"}, {{"Count", each Table.Max(_, each [RevisionID])[Amount], Currency.Type}})
in
    #"Grouped Rows"

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

SU18_powerbi_badge

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.

Top Solution Authors
Top Kudoed Authors