Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now

Reply
laronny
New Member

Count Employees over time from start and term date

I'm wondering how to count monthly employees from their respective start and term dates using dax? 

 

IDNamestart dateTerm dateJan 2024Feb 2024
001John Doe12/1/20231/28/2024.9030
002

Mary Jane

1/1/20243/1/202411
003Jane Doe11/5/202312/31/999911
Total   2.9032
1 ACCEPTED SOLUTION
v-jianpeng-msft
Community Support
Community Support

Hi, @laronny 

Based on the sample data you provided, I've created the following dataset:

vjianpengmsft_0-1730870228244.png

First, I created a date table:

 

CalenderTable = ADDCOLUMNS(CALENDAR(DATE(2024,1,1),DATE(2024,12,31)),
    "MonthID",MONTH([Date]),
    "Year",YEAR([Date])
)

 

vjianpengmsft_1-1730870288675.png

I've created the following two measures:

 

2024 January = 
VAR _date = MAXX(FILTER('CalenderTable','CalenderTable'[MonthID] = 1),'CalenderTable'[Date])
VAR _mindate = MINX(FILTER(ALL('CalenderTable'),'CalenderTable'[MonthID] = 1),'CalenderTable'[Date])
VAR _diff = DATEDIFF(_date,SELECTEDVALUE('Table'[Term date]),DAY)
VAR _diff2 = DATEDIFF(_date,_mindate,DAY)
RETURN 
IF(_diff < 0,IF(_diff>_diff2,(DATEDIFF(_mindate,SELECTEDVALUE('Table'[Term date]),DAY)+1)/(DATEDIFF(_mindate,_date,DAY)+1),0),1)
2024 February = 
VAR _date = MAXX(FILTER('CalenderTable','CalenderTable'[MonthID] = 2),'CalenderTable'[Date])
VAR _mindate = MINX(FILTER('CalenderTable','CalenderTable'[MonthID] = 2),'CalenderTable'[Date])
VAR _diff = DATEDIFF(_date,SELECTEDVALUE('Table'[Term date]),DAY)
VAR _diff2 = DATEDIFF(_date,_mindate,DAY)
RETURN IF(_diff < 0,IF(_diff>_diff2,(DATEDIFF(_mindate,SELECTEDVALUE('Table'[Term date]),DAY)+1)/(DATEDIFF(_mindate,_date,DAY)+1),0),1)

 

Put it into a table and find that the total is incorrect:

vjianpengmsft_0-1731479292098.png

 

This is very normal behavior. When our measure calculation logic is complicated, we usually get an incorrect total. So I created the following two correct measures:

 

2024 January Correct = SUMX(VALUES('Table'[Term date]),[2024 January])
2024 February Correct = SUMX(VALUES('Table'[Term date]),[2024 February])

 

Make these measures into field parameters so that they can be selected by the slicer:

vjianpengmsft_3-1730870603370.png

vjianpengmsft_4-1730870621185.png

The results are as follows:

vjianpengmsft_1-1731479308936.png

vjianpengmsft_2-1731479325788.png

 

 

 

 

How to Get Your Question Answered Quickly

If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

Best Regards

Jianpeng Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

6 REPLIES 6
v-jianpeng-msft
Community Support
Community Support

Hi, @laronny 

Based on the sample data you provided, I've created the following dataset:

vjianpengmsft_0-1730870228244.png

First, I created a date table:

 

CalenderTable = ADDCOLUMNS(CALENDAR(DATE(2024,1,1),DATE(2024,12,31)),
    "MonthID",MONTH([Date]),
    "Year",YEAR([Date])
)

 

vjianpengmsft_1-1730870288675.png

I've created the following two measures:

 

2024 January = 
VAR _date = MAXX(FILTER('CalenderTable','CalenderTable'[MonthID] = 1),'CalenderTable'[Date])
VAR _mindate = MINX(FILTER(ALL('CalenderTable'),'CalenderTable'[MonthID] = 1),'CalenderTable'[Date])
VAR _diff = DATEDIFF(_date,SELECTEDVALUE('Table'[Term date]),DAY)
VAR _diff2 = DATEDIFF(_date,_mindate,DAY)
RETURN 
IF(_diff < 0,IF(_diff>_diff2,(DATEDIFF(_mindate,SELECTEDVALUE('Table'[Term date]),DAY)+1)/(DATEDIFF(_mindate,_date,DAY)+1),0),1)
2024 February = 
VAR _date = MAXX(FILTER('CalenderTable','CalenderTable'[MonthID] = 2),'CalenderTable'[Date])
VAR _mindate = MINX(FILTER('CalenderTable','CalenderTable'[MonthID] = 2),'CalenderTable'[Date])
VAR _diff = DATEDIFF(_date,SELECTEDVALUE('Table'[Term date]),DAY)
VAR _diff2 = DATEDIFF(_date,_mindate,DAY)
RETURN IF(_diff < 0,IF(_diff>_diff2,(DATEDIFF(_mindate,SELECTEDVALUE('Table'[Term date]),DAY)+1)/(DATEDIFF(_mindate,_date,DAY)+1),0),1)

 

Put it into a table and find that the total is incorrect:

vjianpengmsft_0-1731479292098.png

 

This is very normal behavior. When our measure calculation logic is complicated, we usually get an incorrect total. So I created the following two correct measures:

 

2024 January Correct = SUMX(VALUES('Table'[Term date]),[2024 January])
2024 February Correct = SUMX(VALUES('Table'[Term date]),[2024 February])

 

Make these measures into field parameters so that they can be selected by the slicer:

vjianpengmsft_3-1730870603370.png

vjianpengmsft_4-1730870621185.png

The results are as follows:

vjianpengmsft_1-1731479308936.png

vjianpengmsft_2-1731479325788.png

 

 

 

 

How to Get Your Question Answered Quickly

If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

Best Regards

Jianpeng Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Houston-ho
Advocate I
Advocate I

i believe you need to calculate the number of days between 2 dates using DATEDIFF(<Date1>, <Date2>, DAY), also EOMONTH(<start_date>, 0) to get the last date of the month. Therefore you could get 28/31 = 0.903 for John in Jan 2024.

laronny
New Member

This is great, but I have dummy dates for active instead of nulls. @Greg_Deckler , do you know how I can either change dummy date to null/blank or have the dax work with 12/31/9999?

probably you need to compare system today date to calculate the number of days up to today (or you may have a selector slicer on screen as your "as of date") to calculate dynamically.

If you have a slicer (single selction) from calendar table, you could use the dax SELECTEDVALUE to obtain that, then using another DAX DATEDIFF to calculate the number of days between start date and the as of date (or system today).

Greg_Deckler
Super User
Super User

@laronny Open Tickets - Microsoft Fabric Community



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.

October NL Carousel

Fabric Community Update - October 2024

Find out what's new and trending in the Fabric Community.