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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Markando
Frequent Visitor

Average employees rolling 3 months

Hi!

I got tables as below and using this dax to calculate the nr of employees in current month. However I can't get my head around how I should calculate the average on the last three months. Any good ideas?

 

Headcount =
VAR currentDate=IF(max(Calender[Date])>TODAY();TODAY();max(Calender[Date]))

Return

CALCULATE(
countrows(Employees);
FILTER(Employees;
Employees[FromDate]<= currentDate&&
Employees[ToDate]>= currentDate)
)

 

Tables:

Calender 
DateAccountingPeriod
1..31201801
1..28201802
1..31201803
1..30

201804

 

Employees 
IDFromDateToDate
12018-01-019999-12-31
22018-01-019999-12-31
32018-03-019999-12-31
42018-04-019999-12-31

 

Wanted result 
AccountingperiodHeadcountAverage 3 month
20180122,0
20180222,0
20180332,3
20180443,0
1 ACCEPTED SOLUTION
Stachu
Community Champion
Community Champion

try this code - it uses TOPN to get last 3 months, ADDCOLUMN to add [Headcount] and finally AVERAGEX to calculate the average on the output

Average 3 month =
VAR CurrentPeriod =
    MAX ( Calender[AccountingPeriod] )
VAR Last3Periods =
    ADDCOLUMNS (
        TOPN (
            3,
            FILTER (
                ALL ( Calender[AccountingPeriod] ),
                Calender[AccountingPeriod] <= CurrentPeriod
            ),
            [AccountingPeriod], DESC
        ),
        "HeadcountLast3", [Headcount]
    )
RETURN
    AVERAGEX ( Last3Periods, [HeadcountLast3] )


Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

View solution in original post

4 REPLIES 4
Stachu
Community Champion
Community Champion

try this code - it uses TOPN to get last 3 months, ADDCOLUMN to add [Headcount] and finally AVERAGEX to calculate the average on the output

Average 3 month =
VAR CurrentPeriod =
    MAX ( Calender[AccountingPeriod] )
VAR Last3Periods =
    ADDCOLUMNS (
        TOPN (
            3,
            FILTER (
                ALL ( Calender[AccountingPeriod] ),
                Calender[AccountingPeriod] <= CurrentPeriod
            ),
            [AccountingPeriod], DESC
        ),
        "HeadcountLast3", [Headcount]
    )
RETURN
    AVERAGEX ( Last3Periods, [HeadcountLast3] )


Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

Thanks! Beautiful piece of code that worked perfectly 

LivioLanzo
Solution Sage
Solution Sage

What is the From Date  and To Date the period in which employees stay in the company? are there cases when this is not in 9999 ?

do you want to perform the analysis only at month level ?

 


 


Did I answer your question correctly? Mark my answer as a solution!


Proud to be a Datanaut!  

Hi

 

The fromDate and ToDate is the employment period. When ToDate is 9999 that means the employee is still active. If the employee had left the company it could say 2018-03-31 for example, then he shouldnt be added to the monthly value in april (but still two active months to the average)

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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