Forum Discussion

debojyoty's avatar
debojyoty
Frequent Visitor
2 years ago
Solved

Help with grouping columns

I have a table as shown below and want it to be transformed to

 

Input

 

EIDStartDateMGRID
1008843/1/2018101516
1008848/1/202276728
100776/12/200010080
1020834/1/201882224
1020835/2/202382224
1020855/7/201815384
1020865/7/201815384
1025586/11/201876728
10255812/11/2023101516
1025589/6/202224976

 

Output

 

EIDStartDateMGRID
1008848/1/202276728
100776/12/200010080
1020835/2/202382224
1020855/7/201815384
1020865/7/201815384
10255812/11/2023101516

 

 

For each employee, I only want to keep the latest start date.

 

I have seen several similar posts but just can't make this work. Can someone please help. I want to do this in Power Query Editor /  M Query in Power BI 

 

Regards,

-Debo

 

 

  • In Power Query, sort the date column in descending order, add an index column, then remove duplicates on Employee ID. Only the most recent item for each EID will stay.

     

    Here is an example I did:

    This is when I am on the sorted rows step:

    This is when I am on the remove duplicate step:

     

  • let
        #"Grouped Table" = Table.Group(DATA, "EID", {"grp", each Table.Sort(_, {"StartDate", Order.Descending}){0}}),
        #"Expanded grp" = Table.ExpandRecordColumn(#"Grouped Table", "grp", {"StartDate", "MGRID"}, {"StartDate", "MGRID"})
    in
        #"Expanded grp"

     

    Calculated table is way more concise and elegant,

2 Replies

  • In Power Query, sort the date column in descending order, add an index column, then remove duplicates on Employee ID. Only the most recent item for each EID will stay.

     

    Here is an example I did:

    This is when I am on the sorted rows step:

    This is when I am on the remove duplicate step:

     

  • let
        #"Grouped Table" = Table.Group(DATA, "EID", {"grp", each Table.Sort(_, {"StartDate", Order.Descending}){0}}),
        #"Expanded grp" = Table.ExpandRecordColumn(#"Grouped Table", "grp", {"StartDate", "MGRID"}, {"StartDate", "MGRID"})
    in
        #"Expanded grp"

     

    Calculated table is way more concise and elegant,