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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anwar
Frequent Visitor

DAX: Calculation on related table with date durations

I'm trying to apply a DAX expression to identify patients who have diabetes based on Blood Results.
There are two tables, Patients table and Blood Results table.

According to the literature, diabetes is diagnosed after TWO HbA1c readings >50mmol/mol.

Whoever, in order for a patient to be diagnosed with diebetes, two readings should be 12 months apart.
For example, patient with Id 1 is not diabetic because there is only one reading. Patient with Id 2 IS diabetic because there are 2 readings with less than 12 months apart and both are greater than 50. Patient with Id 3 is not diabetic because the duration is more than 12 months.

Patients table

Patient Id
1
2
3


Blood Results table

Patient IdDateTestResult
130/08/2019ALT12
130/09/2019HbA1c30
201/08/2019HbA1c30.5
201/08/2019HbA1c55
201/09/2019HbA1c60
301/08/2019TP3
33/08/2001HbA1c60
33/08/2019HbA1c61

 

This is the result of the DAX expression

Patient IdIs Diabetic (Calculated Column)
1False
2True
3False

 

How can I write a calculated column (not a measure) on the Patient table?

 

1 ACCEPTED SOLUTION
v-lionel-msft
Community Support
Community Support

Hi @Anwar 

 

For your issue, you can do as follows:
First, create column in "Result" table:

Column = IF([Result]>50,1,0)

Column 2 = CALCULATE(
    LASTNONBLANK('Blood Results'[Date],0),
    FILTER(
        ALLEXCEPT('Blood Results',
        'Blood Results'[Patient Id]
        ),
        'Blood Results'[Test]="HbA1c"&&'Blood Results'[Result]>50&&'Blood Results'[Date]<EARLIER('Blood Results'[Date])
        )
    )

Column 3 = DATEDIFF([Column 2],[Date],MONTH)<=12&&DATEDIFF([Column 2],[Date],MONTH)>0&&[Column]=1

 Second, crearte column in "Patients":

Column = 
var 
lookvalue=LOOKUPVALUE('Blood Results'[Column 3],'Blood Results'[Patient Id],Patients[Patient Id],'Blood Results'[Column 3],TRUE()) 
return 
IF(lookvalue=BLANK(),FALSE(),lookvalue)

 

Best Regards,

Lionel Chen

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

1 REPLY 1
v-lionel-msft
Community Support
Community Support

Hi @Anwar 

 

For your issue, you can do as follows:
First, create column in "Result" table:

Column = IF([Result]>50,1,0)

Column 2 = CALCULATE(
    LASTNONBLANK('Blood Results'[Date],0),
    FILTER(
        ALLEXCEPT('Blood Results',
        'Blood Results'[Patient Id]
        ),
        'Blood Results'[Test]="HbA1c"&&'Blood Results'[Result]>50&&'Blood Results'[Date]<EARLIER('Blood Results'[Date])
        )
    )

Column 3 = DATEDIFF([Column 2],[Date],MONTH)<=12&&DATEDIFF([Column 2],[Date],MONTH)>0&&[Column]=1

 Second, crearte column in "Patients":

Column = 
var 
lookvalue=LOOKUPVALUE('Blood Results'[Column 3],'Blood Results'[Patient Id],Patients[Patient Id],'Blood Results'[Column 3],TRUE()) 
return 
IF(lookvalue=BLANK(),FALSE(),lookvalue)

 

Best Regards,

Lionel Chen

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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