Forum Discussion
Need Help with Dax
Hi, I have calendar Dimension Table and i have patient fact table having cardinality one to many from calendar to Patient Table (Cal_Date >>Enrollment Date). Now i want to calculate the counts of patients with respect to different dates(its stages) that are in following table.
| Calendar Table | |
| Cal_Date | Cal_Month |
| 01-01-2024 | Jan 2024 |
| 02-01-2024 | Jan 2024 |
| 03-01-2024 | Jan 2024 |
| 04-01-2024 | Jan 2024 |
| 05-01-2024 | Jan 2024 |
| 06-01-2024 | Jan 2024 |
| 07-01-2024 | Jan 2024 |
| 08-01-2024 | Jan 2024 |
| 09-01-2024 | Jan 2024 |
| 10-01-2024 | Jan 2024 |
| Patient Fact Table | ||||
| Patient ID | Enrollment Date | Admission Date | Billing date | Infusion date |
| A | 01-01-2024 | 02-02-2024 | 03-03-2024 | 03-04-2024 |
| B | 02-01-2024 | 03-02-2024 | 04-03-2024 | 04-04-2024 |
| C | 03-01-2024 | 04-02-2024 | 05-03-2024 | 05-04-2024 |
| D | 04-01-2024 | 05-02-2024 | 06-03-2024 | 06-04-2024 |
| E | 05-01-2024 | 06-02-2024 | 07-03-2024 | 07-04-2024 |
Now i am creating column chart having Cal_month in the x axis, and i want to show each stage count with respect to their month,
For example i need -
for Jan 2024, i need no of enrollments = 5
In Feb 2024, i need no of admissions = 5
in March 2024, i need no of billings = 5
in april 2024, i need no of infusion = 5
currently as cal_date and enrollment date is connected all are counting in Jan Month only. So which is not correct and i am going to implement RLS in future so i am not able to use inactive relationship, Please help.
Hi Krushnab85 ,
To ensure that the calendar filters the correct stage date dynamically, modify your measures using ALL to ignore the active relationship and then apply the correct date filter using TREATAS.
Enrollments Count (Respects Active Relationship)No_of_Enrollments = COUNT(Patient_Fact[Patient ID])(This works as expected since Cal_Date >> Enrollment Date is active)
Admissions Count
No_of_Admissions = CALCULATE( COUNT(Patient_Fact[Patient ID]), ALL(Patient_Fact), -- Removes any filter from Enrollment Date TREATAS( VALUES(Calendar[Cal_Date]), Patient_Fact[Admission Date]) )
Billings CountNo_of_Billings = CALCULATE( COUNT(Patient_Fact[Patient ID]), ALL(Patient_Fact), TREATAS( VALUES(Calendar[Cal_Date]), Patient_Fact[Billing Date]) )
Infusions CountNo_of_Infusions = CALCULATE( COUNT(Patient_Fact[Patient ID]), ALL(Patient_Fact), TREATAS( VALUES(Calendar[Cal_Date]), Patient_Fact[Infusion Date]) )Please mark this post as solution if it helps you. Appreciate Kudos.
6 Replies
- FarhanJeelani
Super User
Hi Krushnab85 ,
Since you cannot use inactive relationships (which would have been a typical solution using USERELATIONSHIP), you need an approach that dynamically counts patients based on different date fields while keeping your Calendar Table as the main filter context.
Solution: Using Measures Without Inactive Relationships
You can create separate measures for each stage and count patients based on the respective date column.1. Measure for Enrollments Count
This measure counts patients where the Enrollment Date falls in the selected month.No_of_Enrollments = CALCULATE( COUNT(Patient_Fact[Patient ID]), TREATAS( VALUES(Calendar[Cal_Date]), Patient_Fact[Enrollment Date]) )
2. Measure for Admissions Count
This measure counts patients where the Admission Date falls in the selected month.No_of_Admissions =CALCULATE( COUNT(Patient_Fact[Patient ID]), TREATAS( VALUES(Calendar[Cal_Date]), Patient_Fact[Admission Date]) )
3. Measure for Billings Count
This measure counts patients where the Billing Date falls in the selected month.No_of_Billings = CALCULATE( COUNT(Patient_Fact[Patient ID]), TREATAS( VALUES(Calendar[Cal_Date]), Patient_Fact[Billing date]) )4. Measure for Infusion Count
This measure counts patients where the Infusion Date falls in the selected month.No_of_Infusions = CALCULATE( COUNT(Patient_Fact[Patient ID]), TREATAS( VALUES(Calendar[Cal_Date]), Patient_Fact[Infusion date]) )
Final Steps:
1. Use a Column ChartX-axis → Calendar[Cal_Month]
Y-axis → Add all four measures:
No_of_Enrollments
No_of_Admissions
No_of_Billings
No_of_Infusions2. Set Data Colors
Different colors for each measure to distinguish the stages.
Please mark this post as solution if it helps you. Appreciate Kudos.
- Krushnab_85Regular Visitor
Thanks for the reply, solution is working fine.. but there is only one problem i am using Cal_Date >>Enrollment Date active relationship for other measures... after activating this relationship its not giving me the correct answer. Please suggest
- Krushnab85
Helper I
FarhanJeelani Any suggestions?
- ryan_mayu
Super User
what about the next few months? Do you also need to show enrollments for Feb Mar and Apr?
- Ray_Minds
Solution Supplier
Hi Krushnab85
1. Prepare Your Data- Import Data:
- Load your Calendar Table and Patient Fact Table into Power BI.
- Ensure that date columns (like Cal_Date, Enrollment Date, Admission Date, etc.) are set to the correct data type (Date).
2. Set Up the Data Model
- Create Relationships:
- In Model View, create an active relationship between Calendar Table[Cal_Date] and Patient Fact Table[Enrollment Date].
3. Create DAX Measures Using TREATAS
- Enrollment Count = COUNTROWS('Patient_Fact')
- Admission Count = CALCULATE(
COUNTROWS('Patient_Fact'),
TREATAS(VALUES('Calendar_Table'[Cal_Date]), 'Patient_Fact'[Admission_Date])) - Billing Count = CALCULATE(
COUNTROWS('Patient_Fact'),
TREATAS(VALUES('Calendar_Table'[Cal_Date]), 'Patient_Fact'[Billing_Date])) - Infusion Count = CALCULATE(
COUNTROWS('Patient_Fact'),
TREATAS(VALUES('Calendar_Table'[Cal_Date]), 'Patient_Fact'[Infusion_Date]))
Best regards,
Ray Minds
http://www.rayminds.com
https://www.linkedin.com/company/rayminds/If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
- In Model View, create an active relationship between Calendar Table[Cal_Date] and Patient Fact Table[Enrollment Date].
- Create Relationships:
- Import Data: