Forum Discussion

aTChris's avatar
aTChris
Icon for Resolver I rankResolver I
5 years ago
Solved

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 ...
  • AlB's avatar
    5 years ago

    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