Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Filter to keep latest date for unique ID values in another column

I am trying to figure out a way within Power Query to filter for the latest date in my LoadDate column but also looking at the JobID column to only filter out old dates for each JobID. In my example ...
  • Jimmy801's avatar
    6 years ago

    Hello Anonymous 

     

    use Table.Group with a special function like this

     

    {{"AllRows", (group)=> Table.SelectRows(group, each [LoadDate]= List.Max(group[LoadDate]))}}

     

    here the complete code

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQ3VtJRMjLXM7DQMzJQitUhUcgMv5CRqQFQyMBIz8CSoJCRiZ6BOUEhvBpjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [JOBID = _t, LoadDate = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"JOBID", Int64.Type}, {"LoadDate",type date,"de-DE"}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"JOBID"}, {{"AllRows", (group)=> Table.SelectRows(group, each [LoadDate]= List.Max(group[LoadDate]))}}),
        #"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows", "AllRows", {"LoadDate"}, {"LoadDate"})
    in
        #"Expanded AllRows"

     

    Copy paste this code to the advanced editor in a new blank query to see how the solution works.

    If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
    Kudoes are nice too

    Have fun

    Jimmy

  • v-alq-msft's avatar
    6 years ago

    Hi, Anonymous 

     

    Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.

    Table:

     

    You may create add a new step with following m codes.

    Custom1= Table.SelectRows(#"Changed Type",each 
    let jobid=[JobID],loaddate=[LoadDate],
    tab = Table.SelectRows(#"Changed Type",each [JobID]=jobid),
    maxdate = List.Max(tab[LoadDate])
    in 
    loaddate=maxdate
    )

     

    Here are the codes in 'Advanced Editor'.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lZFLDsMgDAXvwjoKtsF8zhLl/tcoIS2lxgR1AxJi9J49x2EQM6QAZjMY3XUCYiw3IMH1mixFS0D1hzm3kciYcALQziqCFHOqTB4Y2KMOucB3MT8W05t9Z0EcY2jSrUFMbyg8L6CLCf8SDgYCVsisFnHgVNfOoJjMlpoXFRAiu//u41FGCI99RNuvYITGDvGzFKGxQ1CfRDos1n1D/CokSIRggdwWfxC3QoZiVIqdLw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ClientID = _t, JobID = _t, StaffID = _t, ServiceCode = _t, LoadDate = _t, ActualHoursAtETC = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"ClientID", Int64.Type}, {"JobID", Int64.Type}, {"StaffID", Int64.Type}, {"ServiceCode", Int64.Type}, {"LoadDate", type date}, {"ActualHoursAtETC", type number}}),
        Custom1 = Table.SelectRows(#"Changed Type",each 
    let jobid=[JobID],loaddate=[LoadDate],
    tab = Table.SelectRows(#"Changed Type",each [JobID]=jobid),
    maxdate = List.Max(tab[LoadDate])
    in 
    loaddate=maxdate
    )
    in
        Custom1

     

    Result:

     

    Best Regards

    Allan

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.