Forum Discussion

neilmc's avatar
neilmc
Frequent Visitor
3 years ago
Solved

Filter rows by max date keeping ID

Hello I have the data below

 

Ideally I would like to select for each PerID the containing the latest date.  I've tried groupby in query editor (groupby perID then select Max date and all rows, however when I expand the table to get the DBID field I end up with duplicates

 

Any pointers gratefully recieved

 

Cheers

Neil

 

PerIDDBIDStartDate
PER1000733918908 
PER100073392855002/08/2010 23:00
PER100223390044609/05/2017 00:00
PER1002233918909 
PER1002233923009 
PER100223392855106/11/2007 00:00
PER100293387368206/07/2020 23:00
PER100293389722401/02/2023 00:00
PER100293390044703/10/2018 00:00
PER100293390044808/10/2018 00:00
PER100293390044918/10/2018 00:00
PER100293390045001/11/2018 00:00
PER100293390045105/04/2019 00:00
PER100293390045222/07/2019 00:00
PER100293390045313/08/2019 00:00
PER100293391086015/09/2020 00:00
PER100293391261006/02/2023 00:00
PER100293391261112/04/2023 00:00
PER100293391744812/04/2023 00:00
  • Anonymous's avatar
    Anonymous
    3 years ago

    Hi neilmc ,

     

    According to your description, here are my steps you can follow as a solution.

    (1) My test data is the same as yours.

    (2) We can create a table.

    Table 2 = 
    var _a=ADDCOLUMNS('Table',"max", CALCULATE(MAX('Table'[StartDate]),FILTER('Table','Table'[PerID]=EARLIER('Table'[PerID]))))
    var _b= FILTER(_a,[StartDate]=[max]) 
    return SELECTCOLUMNS(_b,"PerID",[PerID],"DBID",[DBID],"StartDate",[StartDate])

    (3) Then the result is as follows.

     

    Best Regards,

    Neeko Tang

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

     

3 Replies

  • neilmc's avatar
    neilmc
    Frequent Visitor

    Hello - I think I was being I bit of an idiot!  It looks like the grouping method worked in power query but the issue was that I had duplicate person ids with exactly the same start date

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi neilmc ,

     

    According to your description, here are my steps you can follow as a solution.

    (1) My test data is the same as yours.

    (2) We can create a table.

    Table 2 = 
    var _a=ADDCOLUMNS('Table',"max", CALCULATE(MAX('Table'[StartDate]),FILTER('Table','Table'[PerID]=EARLIER('Table'[PerID]))))
    var _b= FILTER(_a,[StartDate]=[max]) 
    return SELECTCOLUMNS(_b,"PerID",[PerID],"DBID",[DBID],"StartDate",[StartDate])

    (3) Then the result is as follows.

     

    Best Regards,

    Neeko Tang

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