Business Case raised by my youtube channel viewer on the 21st July 2023.
In my employee tenure data, it has employee birthday, hire date and leaving date, and race,
gender, etc. Do you have any idea how to create a dynamic measure of tenure and/or age,
as well as a "tenure group" and "age group" that can change with the calendar date? I would like
to show in a line chart the changing number of active employees across time (as x-axis).
Furthermore, I would like to breakdown the number of employees by their age group or tenure group
(using age group as a legend). The age or tenure group will be dynamically change according to
the dates in the X-axis. Any suggestions are appreciated.
My suggestions:
Assumptions on what we have for the case:
- an employee table with coloumns: table update(or registered) date,
employeeID, birthdate, hiredate and leavingdate.
- an calendar Date table with date, year, querter number or name, month number or name, and
period number ( DAX for the Period number as :
periodnumber = VALUE(CONCATENATE(YEAR([Date]), RIGHT("0" & MONTH([Date]), 2))) )
- Calendar table column date has one to many relation link with UpdateDate column of
employee table.
My quick solution:
Step1 Calcaulte the measures for counting ActiveEmployee, my DAX formula as
EmpCount = CALCULATE(
COUNT([EmplID]),
FILTER(ALL('Date'[PeriodNumber]),
'Date'[PeriodNumber] = MAX('Date'[PeriodNumber])
)
)
ActiveEmployees = CALCULATE([EmpCount], FILTER(Employee, ISBLANK(Employee[LeavingDate])))
OR just one DAX formula as:
ActiveEmployees =
CALCULATE(
COUNT('Employee'[EmplID]),
FILTER('Employee', ISBLANK('Employee'[LeavingDate])),
FILTER(ALL('Date'[PeriodNumber]),
'Date'[PeriodNumber] = MAX('Date'[PeriodNumber]))
)
Step 2: Create column measure in employee table for DynamicTenureDays as:
DynamicTenureDays =
IF(
ISBLANK([LeavingDate]),
IF([UpdateDate]<=[HireDate],[HireDate]-[UpdateDate],[UpdateDate]-[HireDate]),
[LeavingDate] - [HireDate]
)
Step 3 : Create column measure in employee table for DynamicTenureYear as:
DynamicTenureYears = DIVIDE([DynamicTenureDays], 365.25)
Step 4: Create column measure in employee table for DynamicTenureGroup, for eample as:
DynamicTenureGroup =
SWITCH(
TRUE(),
[DynamicTenureYears] <= 1, "0-1 years",
[DynamicTenureYears] <= 3, "1-3 years",
[DynamicTenureYears] <= 5, "3-5 years",
[DynamicTenureYears] <= 10, "5-10 years",
[DynamicTenureYears] <= 20, "10-20 years",
"20+ years"
)
Step 5. Create Column measure in employee table for DynamicAges, for exmaple as:
DynamicAges =
VAR MMDD_Num_UpdateDate = VALUE(FORMAT([UpdateDate],"MMDD"))
VAR MMDD_Num_BD =
IF(ISBLANK([EmployeeBirthDate]),
"Blank",
[EmployeeBirthDate]
)
Return
if(
MMDD_Num_BD="Blank",
0,
if(
MMDD_Num_UpdateDate>= VALUE(FORMAT(MMDD_Num_BD,"MMDD")),
DATEDIFF([EmployeeBirthDate],[date],YEAR),
DATEDIFF([EmployeeBirthDate],[date],YEAR)-1
)
)
Step 6. Create column measure in employee table for DynamicAgeGroup as:
DynamicAgeGroup =
SWITCH(
TRUE(),
[DynamicAges] <= 30, "20-30 years",
[DynamicAges] <= 40, "30-40 years",ate
[DynamicAges] <= 50, "40-50 years",
"50+ years"
)
Step 7. Create line charts to show the changing number of active employees across time (as x-axis) by
by the breakdown of Tenure group or age group:
Line chart creations
- place measure activeEmployee in Y-axis
- place the DynamicTenureGroup or DynamicAgeGroup in legend
- place year or created hierarchy (year/quarter/periodnumber) in X-axis.
Slicer creation:
- place a created hierarchy (year/quarter/periodnumber) in visual field
- format the visual style as vertical list.
The solution visuals looks like:
** Comments from Viewer:
Thanks, Mike. I tried to replicate the analysis you did. But I cannot create the dynamic column measure Dynamic Tenure Years. The error message is that "the value for DyanmicTenureDays cannot be determined. ... no current row for this column". Can you figure our what is the issue?
** Reply from Me:
Hi Lijun, in your employee table do you have update date/time column which we call it as time stamper for your employee table? which tells when did your guy or system update the whole employee table. usually in HR system the update frequency is once a month . with this time stamper you can have hostroical view to now of your emplyees hire status regrading the change trend of tenure group and age group. if you do not have the time stamper in your employee table. one my suggested workaround is just to create additional column measure in employee table as "UpdateDate =Today()" . then follow my blog to calcaulte tenure days , then tenure years. with this solution you alway get current view (also call a basic dynamic view at today) about your active employee tenure group status and age group(but no histroical info). if you want apply line chart for the workaround solution. you need to place activeEemplyee measure in Y axis and age group or tenure group measure in X axis , Calendar date slicer in the report page should cover today's date. please try this workaround first and let me know your feedback. I shall find time to add the scenario (missing time stamper in employee table) in my blog. regards Mike.
** Comments from Viewer:
Thanks Mike. I tried to replicate what you did. I can create the column measure "dynamic tenure days", but when I came to create the column DynamicTenureYears = DIVIDE([DynamicTenureDays], 365.25) in the Employee table, the column measure cannot be created. The error message is that "The value for "DynamicTenureDays" cannot be determined. Either the column does not exit, or there is no current row for this column". The column "dyanmictenuredays" is already created and has numeric values in the employee table. Can you figure out what is the problem?
** Reply from Me:
Please check the speelings for each your created column measure name, start from updatedDate, DynamicTenureDays, and ensure that they are correctly speelings in your all related clacaultions. as my feedback to your raised repliacted issues in my youtube channge. it should work as I proposed. please refer to ScreenCopies in followings:
Thanks Mike. I tried to replicate what you did. I can create the column measure "dynamic tenure days", but when I came to create the column DynamicTenureYears = DIVIDE([DynamicTenureDays], 365.25) in the Employee table, the column measure cannot be created. The error message is that "The value for "DynamicTenureDays" cannot be determined. Either the column does not exit, or there is no current row for this column". The column "dyanmictenuredays" is already created and has numeric values in the employee table. Can you figure out what is the problem?
ReplyDeleteHi Lijun, please check speeling for each your created column measure name, start from updatedDate, DynamicTenureDays, and if they are correctly speelings in your all clacaultions. as my feedback to your raised repliacted issues in my youtube channge. it should work as I proposed. please refer to ScreenCopy in this updated Blog
ReplyDeletealso ensure that data type for each column measures....
DeleteHi Mike. Thanks for your kind reply. The data, which is only for exercise, I have do not have a time stamp for updates, so I have create one with a single value which is the last leaving date of the employees table. Based on your blog, I have replicated all your measures.
DeleteBut when I tried to created the line chart with X as the year-month of the date, and X axis being the active employees, and the legend being the active employees, I found the the X axis only shows the update date (which I created to equal to the last leaving date for the employee in the data). The data values are only the active employees at the updated date instead of dynamic based on the date range from first hiring date to the update date.
I am wondering what is the cause for this. Since there is only one Update date, even if the calendar dates change, there should be only one update date value. So is this the basic dynamic view that you mentioned? But in your screenshot above, I can see there is only one update date (7/28/2023), and you have the lines across time.
I am not sure I understand the nature of the time stamp for data updating? Is it just a single field/column or multiple fields? If it is a single field, can I just make the 'leaving date' of an employee as the update date?
I tried to put some screenshots in the reply, but this cannot be done here. I wonder whether I can email you my data and pbix file, so you can check what is the issue.
I have also post a few screenshots in my blog I just created, I will be thankful if you can check it out.
https://www.blogger.com/blog/post/edit/2716155704887449781/6577632564525498310?hl=en
Thanks a lot for taking time to work on this. I have been frustrated for not being able to find a solution to this problem.
Hi lijun, with the solution you have only one snapshot view about employee status as you always have today as the snapshot date. If you want a trend view dynamically about each employee status view monthly you need to update the employee table for example monthly for the table for each employee. Which means for example. First of may 2023 for all employees and first of jun 2023 for all employees. Then actually you have the jun employee table append to may employee table. For each employee you have two date stamps for him or her(in most case we automate the update process for the employee changes you may refer to my earlier video regarding power automate to fetch source table fro email via SharePoint). Then when you have such updated employee table for many years then you can look at the employee status trend view by moving calendar year, quarter...with power bi visual filter settings or slicer settings. Please share me your pbix file by your Google drive account or send to my email address if you have. .
DeleteSent from my phone
-------- Original message --------
From: YouTube
Date: Mon, 31 Jul 2023, 12:55
To: Mike Y
Subject: lchgoog2 replied to you
Youtube Logo
lchgoog2 replied to your reply on lchgoog2's comment
Power BI DAX: Dynamic Age Calculation with DATEDIFF and FORMAT
lchgoog2
Mike YU (BI&Analytics) Hi Mike. Thanks for your kind reply. I can replicate all your measures. But the final line chart shows only one date value (updatedate as I have only one update date). I have posted the detailed comments in reply to your blog. Please check if you have time.
REPLY
If you no longer wish to receive emails about comments and replies, yoube, LLC 901 Cherry Ave, San Bruno, CA 94066
YouTube Twitter