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

Distinct count and grouping/filtering

I have the following table in my report:

 

PatientIDNumeratorVisitEndDate
11Jan 1, 2020
11

Feb 1, 2020

10Mar 1, 2020
21Jan 1, 2020
21

Feb 1, 2020

21Mar 1, 2020
31Jan 1, 2020
31

Feb 1, 2020

31Mar 1, 2020

 

I would like to include a measure that brings back the distinct count of patients that have a minimum numerator of 1. In this scenario, the count should be 2 (patientid 2 & 3). How would the expression be written to return this count?

2 ACCEPTED SOLUTIONS
V-pazhen-msft
Community Support
Community Support

@bireportsHG 

 

Try something like this:

 

Measure = 
var Table_unmatch=CALCULATETABLE(VALUES('Table'[PatientID]),FILTER('Table','Table'[Numerator]=0))
Return CALCULATE(DISTINCTCOUNT('Table'[PatientID]),FILTER('Table',NOT([PatientID] in Table_unmatch)))

 

 

Paul Zheng
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

TomMartens
Super User
Super User

Hey @bireportsHG ,

 

here is another DAX statement that is very efficient and compact:

tom = 
SUMX(
    VALUES('Table'[PatientID])
    , var _min = CALCULATE(MIN('Table'[Numerator]))
    return
    IF(_min = 1 , 1 , BLANK())
)

It iterates across the distinct PatientID that are available in the current filter context
SUMX(
    VALUES(,,,
determines the minimum Numerator for each PatientID
CALCULATE(MIN(...
checks if the value matches the condition and returns 1 otherwise BLANK()
Finally, all the 1's will be summed by the iterator function SUMX

 

Hopefully, this provides what you are looking for.

 

Regards,
Tom

 



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

View solution in original post

4 REPLIES 4
Vera_33
Resident Rockstar
Resident Rockstar

You can groupby PatientID, Calculate Min, then filter the table to the Min = 1, then count rows:

 

Count=
VAR T1 = GROUPBY(Table,[PatientID],"@MIN",MINX(CURRENTGROUP(),[Numerator]))

RETURN

COUNTROWS(FILTER(T1,[@MIN]=1))

TomMartens
Super User
Super User

Hey @bireportsHG ,

 

here is another DAX statement that is very efficient and compact:

tom = 
SUMX(
    VALUES('Table'[PatientID])
    , var _min = CALCULATE(MIN('Table'[Numerator]))
    return
    IF(_min = 1 , 1 , BLANK())
)

It iterates across the distinct PatientID that are available in the current filter context
SUMX(
    VALUES(,,,
determines the minimum Numerator for each PatientID
CALCULATE(MIN(...
checks if the value matches the condition and returns 1 otherwise BLANK()
Finally, all the 1's will be summed by the iterator function SUMX

 

Hopefully, this provides what you are looking for.

 

Regards,
Tom

 



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany
V-pazhen-msft
Community Support
Community Support

@bireportsHG 

 

Try something like this:

 

Measure = 
var Table_unmatch=CALCULATETABLE(VALUES('Table'[PatientID]),FILTER('Table','Table'[Numerator]=0))
Return CALCULATE(DISTINCTCOUNT('Table'[PatientID]),FILTER('Table',NOT([PatientID] in Table_unmatch)))

 

 

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

camargos88
Community Champion
Community Champion

Hi @bireportsHG ,

 

Try this code:

 

Measure =

VAR _tbl_0 = SELECTCOLUMNS(CALCULATETABLE('Table'; FILTER(ALL('Table'[Numerator]); 'Table'[Numerator] = 0)); "PatientID"; 'Table'[PatientID])
VAR _tbl_1 = SELECTCOLUMNS(CALCULATETABLE('Table'; FILTER(ALL('Table'[Numerator]); 'Table'[Numerator] >=1)); "PatientID"; 'Table'[PatientID])
VAR _result = EXCEPT(_tbl_1;_tbl_0)

RETURN AVERAGEX(_result; 1)
 
Ricardo


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



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.