The ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM.
Get registeredCompete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.
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_ID | CONTRACT_STARTDATE | CONTRACT_ENDDATE |
12545 | 1-1-2022 | 12-31-2022 |
12545 | 1-1-2023 | 12-31-2023 |
54654 | 5-1-2021 | 9-30-2021 |
54654 | 10-1-2021 | 12-31-2021 |
54654 | 1-1-2022 | 12-31-2022 |
54654 | 1-1-2023 | 1-1-2024 |
84562 | 6-1-2022 | 6-1-2026 |
21536 | 1-1-2022 | 12-31-2022 |
21536 | 1-1-2023 | 1-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!
Solved! Go to Solution.
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
)
)
)
)
Awesome, worked like charm 👍
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
)
)
)
)
User | Count |
---|---|
28 | |
12 | |
8 | |
7 | |
5 |
User | Count |
---|---|
36 | |
14 | |
12 | |
7 | |
7 |