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
thisguy
Advocate I
Advocate I

display only the last n months of a rolling multi year total while still being able to filter

I need to be able to show the last 6 months headcount of the full org and be able to filter by department. I am having trouble working out the correct DAX.

 

in the measure below If I use ALL then when a slicer is selected it will not filter by department, if i use ALLSELECTED it does not show the correct count which only shows the total of the last 6 months (filter on visual) starts - finishes.

 

I have the following FACT_Data table structure: 

EMPID   Start/Finish     Date       DEPT

0322Employment Date7/03/20201
0366Employment Date19/04/20211
1043Employment Date7/03/20201
1044Employment Date1/03/20222
1177Employment Date11/02/20222
1232Employment Date13/05/20212
1239Employment Date27/04/20201
1239Termination Date10/07/20201
1369Employment Date4/01/20212
1369Termination Date26/02/20212

 

I have a measure that gives a rolling headcount: 

Employee Count =
VAR started =
CALCULATE (
COUNT ( 'FACT_Data'[Start/Finish] ),
'FACT_Data'[Start/Finish] = "Employment Date",
FILTER (
ALLSELECTED ( 'FACT_Data' ),
'FACT_Data'[Date] <= MAX ( DIM_Calendar[Date] )
),all(DIM_Calendar[Date])
)
VAR ended =
CALCULATE (
COUNT ( 'FACT_ Data'[Start/Finish] ),
'FACT_Data'[Start/Finish] = "Termination Date",
FILTER (
ALLSELECTED ( 'FACT_Data' ),
'FACT_ Data'[Date] <= MAX ( DIM_Calendar[Date] )
),all(DIM_Calendar[Date])
)

RETURN
started-ended

 

 

 

1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

Hi @thisguy 
The following seems to work fine 

Employee Count = 
VAR CurrentDate = MAX ( Dim_Calendar[Date] )
VAR SelectedData = ALLSELECTED ( FACT_Data )
VAR EmpData = FILTER ( SelectedData, FACT_Data[Start/Finish] = "Employment Date" )
VAR TerData = FILTER ( SelectedData, FACT_Data[Start/Finish] = "Termination Date" )
VAR Employed = COUNTROWS ( FILTER ( EmpData, FACT_Data[ Date ] <= CurrentDate ) )
VAR Terminated = COUNTROWS ( FILTER ( TerData, FACT_Data[ Date ] <= CurrentDate ) )
RETURN
    Employed - Terminated

2.png1.png

View solution in original post

2 REPLIES 2
tamerj1
Super User
Super User

Hi @thisguy 
The following seems to work fine 

Employee Count = 
VAR CurrentDate = MAX ( Dim_Calendar[Date] )
VAR SelectedData = ALLSELECTED ( FACT_Data )
VAR EmpData = FILTER ( SelectedData, FACT_Data[Start/Finish] = "Employment Date" )
VAR TerData = FILTER ( SelectedData, FACT_Data[Start/Finish] = "Termination Date" )
VAR Employed = COUNTROWS ( FILTER ( EmpData, FACT_Data[ Date ] <= CurrentDate ) )
VAR Terminated = COUNTROWS ( FILTER ( TerData, FACT_Data[ Date ] <= CurrentDate ) )
RETURN
    Employed - Terminated

2.png1.png

OMG!   amazing thankyou so much!!!

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.

Top Solution Authors