Forum Discussion

RichOB's avatar
RichOB
Icon for Post Partisan rankPost Partisan
1 year ago
Solved

Employee retention data

Hi, using the table below, I'd like to be able to only see the employees who have a duplicate NI_Number with us. How can I filter out the employees with only one NI_number in Power Query?

 

The data I need for 2024 is a table showing:

4 people returned to my company

2 were customer service

2 were managers 

I need a column showing the day count between the date an employee joined us and the date they rejoined us

 

NI_NumberNameTitleDate_JoinedDate_LeftDaysDaysBetween (this is the column I need)
NI112233Dave JonesCustomer Service01/04/202420/05/202449 
NI112233Dave JonesCustomer Service01/06/202410/06/2024912
NI223377Mark EvansCustomer Service01/05/202410/05/2025374 
NI998877Kyle ThomasCustomer Service01/06/2024 474 
NI884455Eric DaviesCustomer Service01/07/202410/10/2024101 
NI884455Eric DaviesCustomer Service20/10/202428/10/2024810
NI009900Lloyd Williams Store Manager01/04/202410/09/202490 
NI009900Lloyd Williams Store Manager01/10/202411/11/20244121
NI887711Ian IansStore Manager10/05/202410/10/2024153 
NI887711Ian IansStore Manager01/11/202401/12/20243022

 

 

  • RichOB ,You can add an if as follows:

     

    Column = varX=CALCULATE(max('Table'[Date_left]), FILTER('Table', 'Table'[NI_Number]=EARLIER('Table'[NI_Number]) && 'Table'[Date_joined]<EARLIER('Table'[Date_joined])))
    return
    DATEDIFF(X,if('Table'[Date_left]=blank(),today(), 'Table'[Date_left]),DAY)
     
    If this post helps, then I would appreciate a thumbs up and mark it as the solution to help the other members find it more quickly.
     

7 Replies

  • Hi RichOB,

    You need First to identify the duplicated NI_Numbers using DAX:

    NI_Count = CALCULATE(COUNTROWS('Employees'), ALLEXCEPT('Employees', 'Employees'[NI_Number]))

     Note: After that you should filter your visual NI_Count > 1 (only employees who returned will appear in your visual or the table you filtered)

    After that calculate the Days_Between by adding Calculated column:

    Days_Between = 
    VAR PrevDateLeft = 
        CALCULATE(
            MAX('Employees'[Date_Left]),
            FILTER(
                'Employees',
                'Employees'[NI_Number] = EARLIER('Employees'[NI_Number]) &&
                'Employees'[Date_Joined] < EARLIER('Employees'[Date_Joined])
            )
        )
    RETURN
    IF(NOT(ISBLANK(PrevDateLeft)), DATEDIFF(PrevDateLeft, 'Employees'[Date_Joined], DAY), BLANK())

     
    Let me know if you find it useful 🙂 .

  • Hi RichOB 

     

    You can write a column as follows:

     

    Column = var X=CALCULATE(max('Table'[Date_left]), FILTER('Table', 'Table'[NI_Number]=EARLIER('Table'[NI_Number]) && 'Table'[Date_joined]<EARLIER('Table'[Date_joined])))
    return
    DATEDIFF(X,'Table'[Date_left],DAY)
     
     
    If this post helps, then I would appreciate a thumbs up and mark it as the solution to help the other members find it more quickly.
    • RichOB's avatar
      RichOB
      Icon for Post Partisan rankPost Partisan

      Hi Selva-Salimi, thanks for your reply!

      This has worked for employees who have end dates. Some of the employees who have returned are still employed, and the end date is a blank cell. When that happens the End_Date should be classed as "TODAY".

      How would the measure look if the end date is blank = Today?

       

      Thanks

       

       

      • Selva-Salimi's avatar
        Selva-Salimi
        Icon for Solution Sage rankSolution Sage

        RichOB ,You can add an if as follows:

         

        Column = varX=CALCULATE(max('Table'[Date_left]), FILTER('Table', 'Table'[NI_Number]=EARLIER('Table'[NI_Number]) && 'Table'[Date_joined]<EARLIER('Table'[Date_joined])))
        return
        DATEDIFF(X,if('Table'[Date_left]=blank(),today(), 'Table'[Date_left]),DAY)
         
        If this post helps, then I would appreciate a thumbs up and mark it as the solution to help the other members find it more quickly.
         
  • RichOB 

     

    Hi, in Power Query it would be (adjust the hard-coded column names if you need to), 

    let
        Source = yourtable,
        #"Changed Type" = Table.TransformColumnTypes(yourtable,{{"NI_Number", type text}}),
        GroupedRows1 = Table.Group(#"Changed Type", {"NI_Number"}, {{"Count", each Table.RowCount(_), Int64.Type}, {"Data", each _, type table [NI_Number=nullable text, Name=nullable text, Title=nullable text, Date_Joined=date, Date_Left=date, Days=nullable number]}}),
        FilteredRows = Table.SelectRows(GroupedRows1, each [Count] > 1),
        Expanded = Table.ExpandTableColumn( FilteredRows, "Data", {"Name", "Title", "Date_Joined", "Date_Left", "Days"}),
        Sorted = Table.Sort(Expanded, {{"NI_Number", Order.Ascending}, {"Date_Joined", Order.Ascending}}),
        GroupRows2 = Table.Group(Sorted, {"NI_Number"}, {
            {"AllData", each Table.AddIndexColumn(_, "Index", 1, 1, Int64.Type), type table [NI_Number=nullable text, Name=nullable text, Title=nullable text, Date_Joined=date, Date_Left=date, Days=nullable number, Index=Int64.Type]}
        }),
        #"Expanded AllData" = Table.ExpandTableColumn(GroupRows2, "AllData", {"Name", "Title", "Date_Joined", "Date_Left", "Days", "Index"}, {"Name", "Title", "Date_Joined", "Date_Left", "Days", "Index"}),
        PrevStep = Table.Group(#"Expanded AllData" , {"NI_Number"}, {
            {"WithPrev", each 
                let 
                    t = _,
                    shifted = Table.AddColumn(t, "Prev_DateLeft", each try t[Date_Left]{[Index]-2} otherwise null)
                in shifted, 
                type table [NI_Number=nullable text, Name=nullable text, Title=nullable text, Date_Joined=date, Date_Left=date, Days=nullable number, Index=Int64.Type, Prev_DateLeft=nullable date]}
        }),
        Expanded3 = Table.ExpandTableColumn(PrevStep, "WithPrev", {"Name", "Title", "Date_Joined", "Date_Left", "Days", "Index", "Prev_DateLeft"}),
        AddedDaysBetween = Table.AddColumn(Expanded3, "DaysBetween", each if [Prev_DateLeft] <> null then Duration.Days([Date_Joined] - [Prev_DateLeft]) else null, Int64.Type),
        FinalSorted = Table.Sort(AddedDaysBetween, {{"NI_Number", Order.Ascending}, {"Date_Joined", Order.Ascending}})
    in
        FinalSorted

     

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi RichOB ,


    Thanks to all the Ahmed-Elfeel , MasonMA  & Selva-Salimi  for providing solutions.

    Did you get a chance to look into their suggestions? If any of the solutions helped, that’s great! If you run into any issues or need further clarification, please let us know happy to help.

    Thanks,
    Akhil.


  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi RichOB ,


    Just checking in were you able to try the suggestions from super users. If any of the solutions worked, that’s great. If you’re still facing issues or need further clarification, we’re happy to help.

    Thanks,
    Akhil.