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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

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
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors