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
frosty911
New Member

Need to calculate the Average headcount over 12 months

Hi Community,

 

I am working with some employee data. I need to create a measure to calculate the running 12 month average from static data. (Sample data below):

 

Employee IDDepartmentStart EndEnd DateCurrent Status
10000HR1/1/2023 Active
10001IT1/1/2023 Active
10002IT1/1/202312/1/2023Terminated
10003HR1/1/2023 Active
10004HR1/1/2023 Active
10005Sales2/1/2023 Active
10006HR2/1/2023 Active
10007Finance2/1/20235/1/2023Terminated
10008Finance2/1/20237/1/2023Terminated
10009Finance2/1/2023 Active
10010Finance3/1/2023 Active
10011IT5/1/20237/1/2023Terminated
10012Sales5/1/2023 Active
10013Sales5/1/2023 Active
10014Sales5/1/202311/1/2023Terminated
10015Sales7/1/202311/1/2023Terminated
10016Sales7/1/2023 Active
10017Admin8/1/2023 Active
10018Finance8/1/202311/1/2023Terminated
10019Finance8/1/2023 Active
10020IT8/1/2023 Active
10021Sales9/1/202310/2/2023Terminated
10022Sales9/1/2023 Active
10023Sales9/1/2023 Active
10024Sales10/1/2023 Active
10025HR10/1/2023 Active
10026Finance10/1/202312/1/2023Terminated
10027Finance10/1/2023 Active
10028Finance11/1/2023 Active
10029Finance11/1/2023 Active
10030IT12/1/2023 Active

 

I have a department slicer, so the final average should account for the slicer changes as well. 

 

I am facing the following challenges:

 

1. Accounting for the Hires and leavers differences to get the MoM headcount over 12 months

2. Every month, this list will keep growing with more hires and leavers, so need something that can work as a rolling average.

 

Help on this would be much appreciated. thanks so much in advance!

7 REPLIES 7
frosty911
New Member

Edit: Column name is Start Date not Start End. 

 

Thanks

Yeah, that's going to require some efforts! It took us quite a bit of work to get it right. 

 

Basically, your first objective is to get a count of active employees by date. However, your employee data
only contains start date and termination date. You can't use either of these to trend current headcount,

because you'll just be aggregating headcount by the date they were hired or terminated. What we did was transform into a table that had fields [Employee ID] and [AsOfDate]. Each employee would have a record for every date they were active between their hire date and termination date. And for active employees, you need to replace the terminaton date with today's date. 

 

So, as an example, employee A was hired 12/1/2023, and employee B was hired 12/6/2023, and terminated 12/10/2023, the table would look like:

EmployeeIDAsOfDate
A12/1/2023
A12/2/2023
A12/3/2023
A12/4/2023
A12/5/2023
A12/6/2023
B12/6/2023
A12/7/2023
B12/7/2023
A12/8/2023
B12/8/2023
A12/9/2023
B12/9/2023
A12/10/2023
B12/10/2023
A12/11/2023

Thanks for the quick reply. is there any quick formula to transform the data into this fomat?

You can use this formula if you wanted to do it in DAX as opposed to PQ:

fact Employee = GENERATE( 'dim Employee' , CALENDAR( 'dim Employee'[Start Date] , IF( ISBLANK( 'dim Employee'[Termination Date] ) , TODAY() , 'dim Employee'[Termination Date] ) ) )

 

This was my sample data:

CoreyP_0-1702369101153.png

 

And this is the output:

CoreyP_1-1702369135099.png

 

Then you can count your active:

CoreyP_2-1702369294257.png

 

From there, you can create a measure for active employees by using a distinct count. Once you have that, then you can have an averagex for rolling period averages.

Hi @CoreyP 

Thanks for the help so far. 

I have managed to get the rolling average following your steps.

However, when i plot the Rolling Average on a line chart, for some reason, i get the future month as well, and the values dont match the rolling average value 

frosty911_0-1702453577352.png



I am expecting it to stop at November and show the value 25.25

Here is my rolling average formula

 

Active Employees rolling average final =
IF(
    ISFILTERED('Calendar'[Date]),
    ERROR("Time intelligence quick measures can only be grouped or filtered by the Power BI-provided date hierarchy or primary date column."),
    VAR __LAST_DATE = ENDOFMONTH('Calendar'[Date].[Date])
    VAR __DATE_PERIOD =
        DATESBETWEEN(
            'Calendar'[Date].[Date],
            STARTOFMONTH(DATEADD(__LAST_DATE, -12, MONTH)),
            __LAST_DATE
        )
    RETURN
        AVERAGEX(
            CALCULATETABLE(
                SUMMARIZE(
                    VALUES('Calendar'),
                    'Calendar'[Date].[Year],
                    'Calendar'[Date].[Month]
                ),
                __DATE_PERIOD
            ),
            CALCULATE([Active Employees], ALLSELECTED('Calendar'[Date].[Day]))
        )

       
)



Helpful resources

Announcements
OCT PBI Update Carousel

Power BI Monthly Update - October 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

October NL Carousel

Fabric Community Update - October 2024

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

Users online (2,352)