Forum Discussion

MSargeant's avatar
MSargeant
Frequent Visitor
3 years ago
Solved

Find the Max Date by Year for Multiple IDs Using Two Columns with Multiple Years in Power Query

Hi All,

 

I am looking for a way to filter or trim a data set in Power Query that has dates from multiple years for over 1k unique IDs. I need to keep the rows with the most recent recorded date for each year, for each unique ID. 

 

Thanks!

 

Below are a data sample and expected result:

 

Sample     
IndexIDSDBandRecorded DateYear
16ABD3100170220.AL351Good20/08/20192019
2094ABD3100170220.AL350.8Good23/06/20202020
3073ABD3100170220.AL350.8Good10/11/20202020
6126ABD3100170220.AL350.8Good20/07/20212021
7118ABD3100170220.AL350.8Good19/10/20212021
7992ABD3100170220.AL35nullNo Data20/09/20222022
21ABD31001711001320.AL351.2Good06/11/20182018
2099ABD31001711001320.AL351.2Good23/06/20202020
3078ABD31001711001320.AL351.2Good10/11/20202020
6131ABD31001711001320.AL351.2Good20/07/20212021
7123ABD31001711001320.AL351.1Good19/10/20212021
7997ABD31001711001320.AL352.9Satisfactory20/09/20222022
22ABD31001713201540.AL350.9Good06/11/20182018
2100ABD31001713201540.AL350.9Good23/06/20202020
3079ABD31001713201540.AL350.9Good10/11/20202020
6132ABD31001713201540.AL350.8Good20/07/20212021
7124ABD31001713201540.AL350.8Good19/10/20212021
7998ABD31001713201540.AL351.1Good20/09/20222022
23ABD31001715400.AL351.4Good06/11/20182018
2101ABD31001715400.AL351.9Good23/06/20202020
3080ABD31001715400.AL351.9Good10/11/20202020
6133ABD31001715400.AL352.1Good20/07/20212021
7125ABD31001715400.AL351.7Good19/10/20212021
7999ABD31001715400.AL351.1Good20/09/20222022
17ABD310017220440.AL351.2Good19/02/20192019
2095ABD310017220440.AL350.7Good23/06/20202020
3074ABD310017220440.AL350.7Good10/11/20202020
6127ABD310017220440.AL350.7Good20/07/20212021
7119ABD310017220440.AL351.3Good19/10/20212021
7993ABD310017220440.AL35nullNo Data20/09/20222022

 

 

 

 

  • Hi,

    Thanks for your response. However, I don't have duplicate rows in my data - those have already been removed. I need  the most recent recording date per year (2018, 2019, 2020, 2021 2022) for each unique ID. Each unique ID should be left with the most recent recording date for every year which would result in 4-5 rows per unique ID. 

4 Replies

  • chris88's avatar
    chris88
    Frequent Visitor

    Hi,

     

    I think your best approach would be using the Table.Buffer function.

     

    First sort the column newest to oldest, then create a custom step using Table.Buffer, then remove duplicates while selecting the year and ID columns together.

    Similar example below

     

    let   
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Employee Number", Int64.Type}, {"Employee Name", type text}, {"Date Of Joining"", type datetime}, {"Annual Salary", Int64.Type}, {"Additional Comments", type text}}),
     #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Date Of Joining", Order.Descending}}),
      #"Buffered" = Table.Buffer(#"Sorted Rows"),
     #"Removed Duplicates" = Table.Distinct(#"Buffered", {"Employee Number"})
    in
     #"Removed Duplicates"

     

     

     

    • MSargeant's avatar
      MSargeant
      Frequent Visitor

      Hi,

      Thanks for your response. However, I don't have duplicate rows in my data - those have already been removed. I need  the most recent recording date per year (2018, 2019, 2020, 2021 2022) for each unique ID. Each unique ID should be left with the most recent recording date for every year which would result in 4-5 rows per unique ID. 

      • chris88's avatar
        chris88
        Frequent Visitor

        Yes, by highlighting ID and Year and removing duplicates, it will only remove rows where the Year and ID are the same. By using sort, then table buffer before doing this then it will keep the latest date for each ID in each year. 

        removing duplicates does not necessarily remove duplicate rows. It removes duplicate instances of the column(s) selected. While your dataset doesn't have duplicate rows, it does have rows where both the year and the ID are the same.