August 1, 2016 at 10:54 am
I have a table of Patient Symptoms and a table of Patients in my model. What I want to do in DAX is an INTERSECT like you can do in SQL.
CREATE TABLE Patient(PatientID INT IDENTITY PRIMARY KEY);
CREATE TABLE Symptom(SymptomName VARCHAR(20) PRIMARY KEY);
CREATE TABLE PatientSymptom(PatientID INT, SymptomName VARCHAR(20), ConsentDate DATE, Severity BYTE)
SELECT PatientID
FROM Patient p
WHERE EXISTS (SELECT 1 FROM PatientSymptoms ps WHERE ps.PatientID = p.PatientID AND ps.SymptomName='Diabetes' )
AND EXISTS (SELECT 1 FROM PatientSymptoms ps WHERE ps.PatientID = p.PatientID AND ps.SymptomName='Heart Failure');
How would i write the equivalent DAX query, so that I can see
1. Which patients had only 1 symptom, 2 symptoms, 3 symptoms.
I can filter just fine.
FILTER(PatientSymptom, [SymptomName]="Diabetes")
I thought I could do two filters and do an INTERSECT, like this:
INTERSECT(
FILTER(PatientSymptom, [SymptomName]="Diabetes"),
FILTER(PatientSymptom, [SymptomName]="Heart Failure")
)
but it looks like I need to project only the PatientID in the filter... the DAX version of this:
SELECT PatientID
FROM Patient p
WHERE EXISTS (SELECT 1 FROM PatientSymptom WHERE [patientID] = p.[PatientID] AND ps.SymptomName="Heart Failure")
August 1, 2016 at 11:08 am
Do you have a PowerBI or Power Pivot sample data / model?
August 1, 2016 at 11:20 am
I have a really simple one in PowerBI with only a handful of Patients. It's really small on purpose, because I only need a few records to prove the query is right.
The basic structure is
Patient--(1,M)--PatientSymptom---(M,1)--SymptomList
The file should be attached. <G>
The problem I'm having (because I'm not very good at DAX) is that I want to project only the PatientID so that I can use set operations like INTERSECT and MINUS (well, the DAX version...)
Thanks!
August 1, 2016 at 12:42 pm
I might be getting somewhere...
Since I only have 3 possible symptoms, and the total combinations is only 3!=3x2=6, I can just create a cheat matrix:
If I put this in the Patient table:
HasDiabetes:=COUNTROWS(FILTER(RELATEDTABLE('PatientSymptoms'),'PatientSymptoms'[Symptom]="Diabetes"))
and create the same calculated column for each of the symptoms, I can then find all the patients that have one and not the other pretty easily...
D_plus_CAD =([HasDiabetes]=1) && ([HasCAD]=1)
still not sure how I would do this if I wanted a matrix of symptoms and the count of overlaps ([Symptom] on both rows and columns and then COUNTROWS(RELATEDTABLE('Patient')) in the intersection...
August 9, 2016 at 2:09 pm
Okay, looks like I got bit by the "Reading Is Fundamental" bug...
The short answer is CALCULATETABLE.
When I actually OPENED Ferrari & Russo's DAX Patterns book, there's a chapter on Surveys (Chapter 13)... the question in the book is "Which customers gave the same answers for both Question1 and Question2?" and the answer is:
CALCULATE (
COUNTROWS ( Customers ),
CALCULATETABLE (
Answers,
USERELATIONSHIP (Answers[AnswerKey], Filter2[AnswerKey] )
),
CALCULATETABLE (
Answers,
USERELATIONSHIP (Anwers[AnswerKey], Filter1[AnswerKey] )
)
)
In case someone is looking for this, it's here[/url].
Yep, "Here's your sign."
... after more playing around with this... I got this working... I guess CALCULATETABLE is something I need to figure out...
-- has HF
CALCULATETABLE('Patient',FILTER('Diagnoses',[SymptomName]="Heart Failure"))
-- has D
CALCULATETABLE('Patient',FILTER('Diagnoses',[SymptomName]="Diabetes"))
-- has both:
INTERSECT('has D', 'has HF')
or
INTERSECT (
CALCULATETABLE('Patient',FILTER('Diagnoses',[SymptomName]="Diabetes")),
CALCULATETABLE('Patient',FILTER('Diagnoses',[SymptomName]="Heart Failure"))
)
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy