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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
frankyjones
Regular Visitor

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!

1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

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
)
)
)
)

View solution in original post

2 REPLIES 2
frankyjones
Regular Visitor

Awesome, worked like charm 👍

tamerj1
Super User
Super User

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
)
)
)
)

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.