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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

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
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

Power BI Carousel June 2024

Power BI Monthly Update - June 2024

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

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.