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
_Adrian
Frequent Visitor

DAX to count distinct rows for specific condition

Hi,

 

Please help me with some DAX.

 

Here's a sample of the 'Registrations' data:

PeopleCourseCompletion
Person 1E1100%
Person 1E20%
Person 1M1100%
Person 1M210%
Person 1M320%
Person 2E1100%
Person 2E2100%
Person 2M190%
Person 2M2100%
Person 2M3100%
Person 3E1100%
Person 3E2100%
Person 3M110%
Person 3M210%
Person 3M310%

 

And then a dimension table:

CourseSubject
E1English
E2English
M1Maths
M2Maths
M3Maths

 

The fact table has the same Courses for every person and a completion % for each. I need to create measures that count the number of people that have:

1. Started one or more Courses

2. Completed 80%-100% in one or more Courses

3. Completed 80%-100% in ALL Courses <<<< This is the one I'm stuck on

 

I have measures that solve for 1 and 2:

Learners = DISTINCTCOUNT(Registrations[People])
Started one or more courses = CALCULATE([Learners], Registrations[Completion] > 0)
Completed (80-100%) one or more courses = CALCULATE([Learners], Registrations[Completion] >= 0.8)
 
But I would like help on 3.

Note that in the report, I want to slice by subject: English or Maths or all.

So, the results for 'Completed 80%-100% in ALL Courses' would be:

For English, 2 (Person 2 and Person 3),

For Maths, 1 (Person 2 only).

 

The solution is probably quite simple, but I'm not getting anywhere with my attempts. Help my sanity, please!

3 REPLIES 3
jameszhang0805
Resolver IV
Resolver IV

Please try the below code:

#Learners = 
VAR _Table =
    ADDCOLUMNS (
        CALCULATETABLE (
            SUMMARIZE ( 'Data', 'Data'[People], Subject[Subject] ),
            ALLSELECTED ()
        ),
        "@AVGCOMP",
            CALCULATE (
                AVERAGE ( Data[Completion] ),
                ALLEXCEPT ( Data, Data[People], Subject[Subject] )
            )
    )
VAR _Filtered =
    FILTER ( _Table, [@AVGCOMP] >= 0.8 )
RETURN
    COUNTROWS ( _Filtered )

DAX to count distinct rows for specific condition.pbix 

Hi and thanks for your help. I'm really glad you posted - its interesting to see how you've tackled this.

 

I've tried that measure and tested it; it kind of works but not all the time. I think it hinges upon where you've used average, whereas I'd need it when all completions are 80% or higher (and not just the average). So someone with 100%, 100%, 79% should not by counted. Would you be able to adapt the DAX? I've tried but without success. I think it would be along the lines of: when a 'count of rows' is equal to a 'count of the rows with completion >= 80%'. 

_Adrian
Frequent Visitor

I worked out a solution, but it's by deduction. I'd still be keen to hear if anyone can come up with a way to count them directly.

 

To count how many have achieved 80-100% in all modules, I started with all Learners than deducted any that had a score below 80%:

Completed ALL courses = [Learners] - CALCULATE([Learners], Registrations[Completion] < 0.8)

 

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.