Forum Discussion

frankyjones's avatar
frankyjones
Regular Visitor
3 years ago
Solved

Distinct Count based on two date columns

Hi all,


I am trying to retrieve a value from a table that has certain conditions (between two dates), but I got lost on my DAX. It would be great if one of you was able to help.

 

My Employee Table looks something like this:

EMPLOYEE_IDCONTRACT_STARTDATECONTRACT_ENDDATE
125451-1-202212-31-2022
125451-1-202312-31-2023
546545-1-20219-30-2021
5465410-1-202112-31-2021
546541-1-202212-31-2022
546541-1-20231-1-2024
845626-1-20226-1-2026
215361-1-202212-31-2022
215361-1-20231-1-2027

 

I want to create a measure that counts the number of employees in a certain date range that is filtered with date slicers (based on my date table) in my report. The Employee table can have an inactive many to one relationship with the date table (not an active one, because it's a dim table).

 

I made a measure that works that calculates the number of employees that started in the period that is selected with the inactive relationship, but that's not the calculation I want. I want the number of employees within the range of Contract start and Contract end.

 

 

 

Number of employees = CALCULATE(DISTINCTCOUNT('Dim Employees'[EMPLOYEE_ID]), USERELATIONSHIP('Dim Calender'[DATE], 'Dim Employees'[CONTRACT_STARTDATE]))

 

 

Is there a way to solve this? Preferably within the dax measure itself.

 

Thanks!

  • Hi frankyjones 

    please try

    Number of employees =
    VAR MaxDate =
    MAX ( 'Calender'[DATE] )
    VAR MinDate =
    MIN ( 'Calender'[DATE] )
    RETURN
    COUNTROWS (
    FILTER (
    VALUES ( 'Dim Employees'[EMPLOYEE_ID] ),
    NOT ISEMPTY (
    FILTER (
    CALCULATETABLE ( 'Dim Employees' ),
    'Dim Employees'[CONTRACT_STARTDATE] <= MaxDate
    && 'Dim Employees'[CONTRACT_ENDDATE] >= MinDate
    )
    )
    )
    )

2 Replies

  • tamerj1's avatar
    tamerj1
    Community Champion

    Hi frankyjones 

    please try

    Number of employees =
    VAR MaxDate =
    MAX ( 'Calender'[DATE] )
    VAR MinDate =
    MIN ( 'Calender'[DATE] )
    RETURN
    COUNTROWS (
    FILTER (
    VALUES ( 'Dim Employees'[EMPLOYEE_ID] ),
    NOT ISEMPTY (
    FILTER (
    CALCULATETABLE ( 'Dim Employees' ),
    'Dim Employees'[CONTRACT_STARTDATE] <= MaxDate
    && 'Dim Employees'[CONTRACT_ENDDATE] >= MinDate
    )
    )
    )
    )