Forum Discussion
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_Number | Name | Title | Date_Joined | Date_Left | Days | DaysBetween (this is the column I need) |
| NI112233 | Dave Jones | Customer Service | 01/04/2024 | 20/05/2024 | 49 | |
| NI112233 | Dave Jones | Customer Service | 01/06/2024 | 10/06/2024 | 9 | 12 |
| NI223377 | Mark Evans | Customer Service | 01/05/2024 | 10/05/2025 | 374 | |
| NI998877 | Kyle Thomas | Customer Service | 01/06/2024 | 474 | ||
| NI884455 | Eric Davies | Customer Service | 01/07/2024 | 10/10/2024 | 101 | |
| NI884455 | Eric Davies | Customer Service | 20/10/2024 | 28/10/2024 | 8 | 10 |
| NI009900 | Lloyd Williams | Store Manager | 01/04/2024 | 10/09/2024 | 90 | |
| NI009900 | Lloyd Williams | Store Manager | 01/10/2024 | 11/11/2024 | 41 | 21 |
| NI887711 | Ian Ians | Store Manager | 10/05/2024 | 10/10/2024 | 153 | |
| NI887711 | Ian Ians | Store Manager | 01/11/2024 | 01/12/2024 | 30 | 22 |
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])))returnDATEDIFF(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
- Ahmed-Elfeel
Super User
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 🙂 . - Selva-Salimi
Solution Sage
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])))returnDATEDIFF(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
Post 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
Solution 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])))returnDATEDIFF(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.
- MasonMA
Super User
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 - AnonymousNot 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. - AnonymousNot 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.