Forum Discussion

Krushnab85's avatar
Krushnab85
Icon for Helper I rankHelper I
1 year ago
Solved

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_DateCal_Month
01-01-2024Jan 2024
02-01-2024Jan 2024
03-01-2024Jan 2024
04-01-2024Jan 2024
05-01-2024Jan 2024
06-01-2024Jan 2024
07-01-2024Jan 2024
08-01-2024Jan 2024
09-01-2024Jan 2024
10-01-2024Jan 2024

 

Patient Fact Table
Patient IDEnrollment DateAdmission DateBilling dateInfusion date
A01-01-202402-02-202403-03-202403-04-2024
B02-01-202403-02-202404-03-202404-04-2024
C03-01-202404-02-202405-03-202405-04-2024
D04-01-202405-02-202406-03-202406-04-2024
E05-01-202406-02-202407-03-202407-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 Count

    No_of_Billings =
    CALCULATE(
    COUNT(Patient_Fact[Patient ID]),
    ALL(Patient_Fact),
    TREATAS( VALUES(Calendar[Cal_Date]), Patient_Fact[Billing Date])
    )


    Infusions Count

    No_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

  • 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 Chart

    X-axis → Calendar[Cal_Month]
    Y-axis → Add all four measures:
    No_of_Enrollments
    No_of_Admissions
    No_of_Billings
    No_of_Infusions

     

    2. 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_85's avatar
      Krushnab_85
      Regular 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

  • what about the next few months? Do you also need to show enrollments for Feb Mar and Apr?

     

  • Ray_Minds's avatar
    Ray_Minds
    Icon for Solution Supplier rankSolution 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.