Forum Discussion

faysanderson's avatar
faysanderson
New Member
1 year ago
Solved

Group by Max date without losing other bits of data

Using this table...

Client IDAssessment IDAssess Date
12310110/1/24
1231021/1/24
23430016/3/24
23430220/6/24
3454056/9/24
34541010/9/24

 

I want to group by Client ID number to show the Max Assess Date. When I use the UI to do this, the code comes out as...

= Table.Group(#"Changed Type", {"Client ID"}, {{"Max Assess Date", each List.Max([Assess Date]), type nullable datetime}})

and in the result, I lose the Assessment ID that corresponds to the Max Assess Date.  Result looks like this...

Client IDMax Assess Date
12310/1/24
23420/6/24
34510/9/24

 

But what I want is to keep the Assessment ID that corresponds to the Max Assess Date...

Client IDAssessment IDMax Assess Date
12310110/1/24
23430220/6/24
34541010/9/24

 

How do I change the code to do this?  Or is it possible to do through the UI boxes?

  • Hi faysanderson  Try below code:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Zc1BCsAwCETRu7gOOI5W6FlC7n+NxpSSQjZ/MQ+xdzG6NDHYqpoSDBltC6t/oMecHChI9VPqhNDc4nHNLVBNvQ8wvP8/GQ8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Client ID" = _t, #"Assessment ID" = _t, #"Assess Date" = _t]),
    
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Client ID", Int64.Type}, {"Assessment ID", Int64.Type}, {"Assess Date", type text}}),
    
        #"Changed Type with Locale" = Table.TransformColumnTypes(#"Changed Type", {{"Assess Date", type date}}, "en-GB"),
    
        #"Grouped Rows" = Table.Group( 
                 #"Changed Type with Locale",
                 {"Client ID"},
                 {
                    {"Max Assess Date", each List.Max([Assess Date]), type nullable date},
                    {"Assessment ID", each 
                        let
                            maxDate = List.Max([Assess Date]),
                            maxRow = Table.SelectRows(_, each [Assess Date] = maxDate)
                        in
                        maxRow{0}[Assessment ID], 
                    type nullable number}
                }
    )
    in
        #"Grouped Rows"

    Desired output:

     

    Hope this helps!!

    If this solved your problem, please accept it as a solution!!

     

    Best Regards,
    Shahariar Hafiz

4 Replies

  • Hi faysanderson  Try below code:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Zc1BCsAwCETRu7gOOI5W6FlC7n+NxpSSQjZ/MQ+xdzG6NDHYqpoSDBltC6t/oMecHChI9VPqhNDc4nHNLVBNvQ8wvP8/GQ8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Client ID" = _t, #"Assessment ID" = _t, #"Assess Date" = _t]),
    
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Client ID", Int64.Type}, {"Assessment ID", Int64.Type}, {"Assess Date", type text}}),
    
        #"Changed Type with Locale" = Table.TransformColumnTypes(#"Changed Type", {{"Assess Date", type date}}, "en-GB"),
    
        #"Grouped Rows" = Table.Group( 
                 #"Changed Type with Locale",
                 {"Client ID"},
                 {
                    {"Max Assess Date", each List.Max([Assess Date]), type nullable date},
                    {"Assessment ID", each 
                        let
                            maxDate = List.Max([Assess Date]),
                            maxRow = Table.SelectRows(_, each [Assess Date] = maxDate)
                        in
                        maxRow{0}[Assessment ID], 
                    type nullable number}
                }
    )
    in
        #"Grouped Rows"

    Desired output:

     

    Hope this helps!!

    If this solved your problem, please accept it as a solution!!

     

    Best Regards,
    Shahariar Hafiz

    • faysanderson's avatar
      faysanderson
      New Member

      Thank you.  I haven't completely wrapped my head round why this works or how to implement it in other scenarios, but it has solved my issue. ðŸ˜€

  • Hi faysanderson 

     

    = Table.Group(#"Changed Type", {"Client ID"}, {{"Max Assess Date", each Table.Max(_, "Assess Date"), type record}})

     Expand "Assessment ID" and "Max Assess Date"

     

    Stéphane