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

Microsoft is giving away 50,000 FREE Microsoft Certification exam vouchers. Get Fabric certified for FREE! Learn more

Reply
KartheekJ
Frequent Visitor

Calculating active employees in each month of a year using hire date and end date columns

Hi,

 

I have an employee table with hire date and end date columns and a date table.

There is an active relation between Date[date] and Employee[end date] and an inactive relation between Date[date] and Employee[hire date].

The requirement is to show no of active employees in each month of the year in a column chart.

I have used the below DAX formula

Active Employees =
CALCULATE(
    [No.of Resources],
    'BM EXTERNALS TRACKED DATA'[Hire Date ] <= MAX('Date'[Date]),
    'BM EXTERNALS TRACKED DATA'[End Date ] >= MIN('Date'[Date]),
    USERELATIONSHIP('BM EXTERNALS TRACKED DATA'[Hire Date ],'Date'[Date])
)
and got the below outcome which is wrong
KartheekJ_0-1728995268977.png

 

Then, I have tried this Dax formula 

Head Count = CALCULATE(
                              [No.of Resources],
                              FILTER(
                                  ALL('Date'),
                                 'Date'[Date] >= MIN('Date'[Date])
                              )
                       )
 
and got below outcome
KartheekJ_1-1728995465376.png

This is a bette outcome.

 

But, the issue is this formula is not considering the hire date while calculating active resources.

 

for example, if an employee has a start date as 10/04/24 and end date as 31/07/24, he should only be included in the active employees in April, May, June and July. he shouldn't be included for the months before april 2024.

 

But this 2nd formula is including the above employee in every month till Jul 24.

 

Can someone help me to modify the DAX to calculate active employees considering both hire date and end date.

 

Thanks

Kartheek

        
1 ACCEPTED SOLUTION
DataNinja777
Super User
Super User

Hi @KartheekJ ,

 

Regarding this topic, the key to solving your headcount issue is to set your calendar (date) table as a disconnected table from your employee table, which contains information such as employee ID, joining date, and leaving date. Then, you can write a DAX formula like the one below, enabling you to calculate headcount at any point in time!

Headcount =
SUMX (
    EmployeeFact,
    IF (
        EmployeeFact[Start] <= [SelectedDate]
            && EmployeeFact[End] >= [SelectedDate],
        1,
        BLANK ()
    )
)

Please refer to the article below if you’d like to explore this topic further.

https://community.fabric.microsoft.com/t5/Community-Blog/Dynamic-Headcount-Analysis-using-Dax/ba-p/4...

 

Best regards,

View solution in original post

4 REPLIES 4
DarshanKumar
Frequent Visitor

Hi,

 

How to calculate the active employees if we have duplicate data in the table.

 

Ex: the table will have multiple rows for an empoyee when he has changed the department.

 

Regards,

Darshan

Hi @DarshanKumar ,

 

That's a very relevant question which happens all the time in the real world scenario.   You can deal with this very common situation by maintaining the employee change log table where you have start dates and end dates where employees change departments in the table with department field.  

 

Headcount =
SUMX (
    'Employee Change',
    IF (
        MAX ( 'Calendar'[Date] ) >= 'Employee Change'[Hire date]
            && MAX ( 'Calendar'[Date] ) <= 'Employee Change'[Leaving date],
        1,
        BLANK ()
    )
)


The dax measure above will take care of the headcount situation properly even when the employees change departments as long as the change log for department is also properly maintained as a fact table. 

 

This formula checks if the current report date falls between the hire and leave dates, so even if the employee appears multiple times due to transfers, they'll only be counted once if they're still active.

 

Best regards,

KartheekJ
Frequent Visitor

Hi @DataNinja777.  Thank you very much for the quick response.

DataNinja777
Super User
Super User

Hi @KartheekJ ,

 

Regarding this topic, the key to solving your headcount issue is to set your calendar (date) table as a disconnected table from your employee table, which contains information such as employee ID, joining date, and leaving date. Then, you can write a DAX formula like the one below, enabling you to calculate headcount at any point in time!

Headcount =
SUMX (
    EmployeeFact,
    IF (
        EmployeeFact[Start] <= [SelectedDate]
            && EmployeeFact[End] >= [SelectedDate],
        1,
        BLANK ()
    )
)

Please refer to the article below if you’d like to explore this topic further.

https://community.fabric.microsoft.com/t5/Community-Blog/Dynamic-Headcount-Analysis-using-Dax/ba-p/4...

 

Best regards,

Helpful resources

Announcements
PBIApril_Carousel

Power BI Monthly Update - April 2025

Check out the April 2025 Power BI update to learn about new features.

Notebook Gallery Carousel1

NEW! Community Notebooks Gallery

Explore and share Fabric Notebooks to boost Power BI insights in the new community notebooks gallery.

April2025 Carousel

Fabric Community Update - April 2025

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

Top Kudoed Authors