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

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.

Reply
Ines_Raquel
New Member

Count number of employees from start date to end date

Hello, everyone.

 

I need to create a metric based on the following logic:

Nº Employees :=
calculate(DISTINCTCOUNT('Employee'[ID]),
filter('Employee table',
year('Employee table'[Entry Date]) <= 2021
&& ('Employee table'[Leave Date] = BLANK() || (year('Employee table'[Leave Date]) >=2021))))

 

However, when i filter the date on PowerBI, it only displays employees that have the entry date of the filtered year.

For instance, if i select 2021, i need to see all employees that have not left until 2021. Not solely those with the entry date at 2021 like it's currently happening.

 

Please, any help will be much appreciated!

Thanks.

1 ACCEPTED SOLUTION

Last thing I can think to try

NumEmp :=
VAR endOfPeriod =
    MAX ( 'Dim Date'[Date] )
VAR startOfPeriod =
    MIN ( 'Dim Date'[Date] )
RETURN
    CALCULATE (
        DISTINCTCOUNT ( 'Fact Internal Employee'[NIF Employee] ),
        ALL ( 'Dim Date' ),
        'Fact Internal Employee'[Employment Date] <= endOfPeriod,
        (
                'Fact Internal Employee'[Resignation Date] >= startOfPeriod
                    || ISBLANK ( 'Fact Internal Employee'[Resignation Date] )
            )
    )

View solution in original post

8 REPLIES 8
johnt75
Super User
Super User

try

Nº Employees :=
CALCULATE (
    DISTINCTCOUNT ( 'Employee'[ID] ),
    REMOVEFILTERS ( 'Employee table' ),
    YEAR ( 'Employee table'[Entry Date] ) <= 2021
        && (
            ISBLANK ( 'Employee table'[Leave Date] )
                || ( YEAR ( 'Employee table'[Leave Date] ) >= 2021 )
        )
)

I appreciate your help.

However, 'REMOVEFILTERS' is not being recognized on AS. 
2021 was more of an example. The year is supposed to be whatever is filtered.

Any work arounds?

You could try

Nº Employees :=
VAR MaxDate =
    YEAR ( MAX ( 'Date'[Date] ) )
RETURN
    CALCULATE (
        DISTINCTCOUNT ( 'Employee'[ID] ),
        ALL ( 'Employee table' ),
        YEAR ( 'Employee table'[Entry Date] ) <= MaxDate
            && (
                ISBLANK ( 'Employee table'[Leave Date] )
                    || ( YEAR ( 'Employee table'[Leave Date] ) >= MaxDate )
            )
    )

I adptaded your formula a bit and got the right results for each year:

NumEmp:=
VAR endOfPeriod =MAX ( 'Dim Date'[Date])
VAR startOfPeriod = MIN( 'Dim Date'[Date] )
RETURN
CALCULATE (
DISTINCTCOUNT('Fact Internal Employee'[NIF Employee]),
FILTER (
ALL('Fact Internal Employee'),
( ('Fact Internal Employee'[Employment Date] <= endOfPeriod )
&& ('Fact Internal Employee'[Resignation Date] >= startOfPeriod || 'Fact Internal Employee'[Resignation Date] = BLANK()))
))
    )

The problem is that when i try to create an object with it against any other field, it presents repeated values:

Staff Turnover Calculation In Power BI Using DAX - HR Insights.png

 

Don't know if it helps, but the relationship is between Employee[entry date] and Date[Date]

you're removing all the filters on the employee table. try

NumEmp :=
VAR endOfPeriod =
    MAX ( 'Dim Date'[Date] )
VAR startOfPeriod =
    MIN ( 'Dim Date'[Date] )
RETURN
    CALCULATE (
        DISTINCTCOUNT ( 'Fact Internal Employee'[NIF Employee] ),
        REMOVEFILTERS ( 'Dim Date' ),
        'Fact Internal Employee'[Employment Date] <= endOfPeriod
            && (
                'Fact Internal Employee'[Resignation Date] >= startOfPeriod
                    || ISBLANK ( 'Fact Internal Employee'[Resignation Date] )
            )
    )

Unfortunaly it doesn't work due to REMOVEFILTERS not being recognized. When i try to replace it with ALL i get an error saying "The expression contains multiple columns..." 😕

Last thing I can think to try

NumEmp :=
VAR endOfPeriod =
    MAX ( 'Dim Date'[Date] )
VAR startOfPeriod =
    MIN ( 'Dim Date'[Date] )
RETURN
    CALCULATE (
        DISTINCTCOUNT ( 'Fact Internal Employee'[NIF Employee] ),
        ALL ( 'Dim Date' ),
        'Fact Internal Employee'[Employment Date] <= endOfPeriod,
        (
                'Fact Internal Employee'[Resignation Date] >= startOfPeriod
                    || ISBLANK ( 'Fact Internal Employee'[Resignation Date] )
            )
    )

Worker perfectly!
Thank you so much for your help!

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.