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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
ngaray
Helper I
Helper I

DAX measure help - need to change evaluation context (I think)

Hello DAX experts (or not complete beginners),

 

I am trying to analyze conduct data of students at the university where I work. You can see a simplified and fake version of the table below:

 

Incident IDStudent IDChargeSanctionIncident ID & Student ID
A111100DrinkingDrinking classA111 1
A111100Drinkingmonetary sanctionA111 1
A112200Loud noisePolicies quizA112 2
A113100Disruptive behaviorPolicies quizA113 1
A113100Disruptive behaviormonetary sanctionA113 1
A113100VandalismPolicies quizA113 1
A113100Vandalismmonetary sanctionA113 1
A114300DrinkingDrinking classA114 3
A114300Drinkingmonetary sanctionA114 3
A115200Destruction of propertymonetary sanctionA115 2
A116400Loud noisePolicies quizA116 4

 

I am trying to calculate the recidivism ratio. Recidivism = Students with more than 1 incident / All Students

In the table above, recidivism = 2/4 = 50%.

Numerator -> student 100 & 200.   Denominator -> student 100, 200, 300, & 400

 

Unfortunately, I am unable to calculate the numerator correctly. This is the measure that I am using:

 
 
numerator recidivism =
if([incident distinct count]>1,
    distinctcount('Table'[Student ID]),
    blank()
)
 

 The result of this measure is 4, however the right number, as mentioned above, is 2 (student 1 & 2). I believe I need to change the evaluation context. Unfortunately, I don't think I grasp the concept of context well enough to fix my measure.

 

I am attaching a pbix file as I believe this will help you understand the problem better.

 

folder with pbix doc link

 

I appreciate any help,

 

Nathalia

 

 

1 ACCEPTED SOLUTION

@ngaray add this measure and test it

 

recidivism =
VAR __studentwithmorethanoneincident = 
COUNTROWS(
 FILTER
 (
    SUMMARIZE( Table1, Table1[Student ID],  "Count", DISTINCTCOUNT( Table1[Incident ID] ) ), [Count] > 1
)
)
RETURN DIVIDE( __studentwithmorethanoneincident  , DISTINCTCOUNT( Table1[Student ID] ) ) 


Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

View solution in original post

4 REPLIES 4
parry2k
Super User
Super User

@ngaray isn't it should be 3/4, (100, 200, & 300), all have more than 1 incident where as 400 as only one incident?



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Hi @parry2k,

 

Thank you so much for your response. Something that makes my dataset confusing is that an incident can have multiple rows. This is because a student can be charged for multiple reasons and also be sanctioned with multiple sanctions. This is why in the dataset, student 300 shows up in two rows even though the student is only involved in one incident. Student 300 is involved in incident A113 and is charged with drinking and sanction with a monetary penalty and attending a "drinking class". One row shows the first sanction and the other shows the second sanction.

 

Incident IDStudent IDChargeSanctionIncident ID & Student ID
A113300DrinkingDrinking classA113 300
A113300Drinkingmonetary sanctionA113 300

 

I believe I need to create a subset of the table by getting a distinct count of "Incident ID" column (this will fix having multiple rows for any given incident). On that subset, I would then count the "Student ID" column. Finally, if Count(student ID) > 1 then count in the numerator, if not, ignore/blank. Conceptually, I think I know how to get the result, I just don't know how to write the DAX measure.

 

Ideal Subset table: 

 

Incident IDStudent IDStudent CountStudent Count > 1
A1111002TRUE
A1122002TRUE
A1131002TRUE
A1143001FALSE
A1152002TRUE
A1164001FALSE

TRUE = 2 (Student 100 and 200)

 

I hope I answer your question.

 

Thank you,

 

Nathalia

@ngaray add this measure and test it

 

recidivism =
VAR __studentwithmorethanoneincident = 
COUNTROWS(
 FILTER
 (
    SUMMARIZE( Table1, Table1[Student ID],  "Count", DISTINCTCOUNT( Table1[Incident ID] ) ), [Count] > 1
)
)
RETURN DIVIDE( __studentwithmorethanoneincident  , DISTINCTCOUNT( Table1[Student ID] ) ) 


Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Hi @parry2k ,

 

Thank you so much for your help. I tested the measure and it works perfectly! I really appreciate your assistance.

 

In case anyone else is interested in seeing the measure at work, I updated the pbix with the measure.

 

Best,

 

Nathalia

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.