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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
JKen1234
Frequent Visitor

Distinct count with filter condition but exclude if another filter condition is met

I want to create a distinct count of catergories if one row meets a filter condition but exclude the entire category if one of the rows within in it meets another filter condition.

 

I want a distinct count of person number with an appointment leaving date within the calendar period. But not if they have another appointment that was active in that period.

 

they way the data should work is each appointment has a start and end date but should only have an leaving date if they leave without another active role. However leaving dates have been put in even if another role was active.

 

Maybe this is soemthing that i Could fix in power query but not sure how I would do that either.

 

Some key person number here are:

189 while they left one role in october they started another so didnt actually leave.

123 left appointment 1187 in 2008 but had an active bank role 1256 so didnt actually leave

 

I tried writing something along the lines of the below but dont think im going about this the right way.

=CALCULATE(DISTINCTCOUNT(Appointments[Person Number]),FILTER(Appointments,
Appointments[Leaving Date]>=MIN('Calendar'[Date])&&Appointments[Leaving Date]<=MAX('Calendar'[Date])&&NOT(Appointments[Start Date]<=MAX('Calendar'[Date])&&([End Date]>MAX('Calendar'[Date])||ISBLANK([End Date])))
)
)

 

Person NumberAppointment NumberStart DateEnd DateLeaving DateAppointment Type
123100301/02/200101/09/200601/09/2006Permament
123118702/01/200702/09/200802/09/2008Temporary
123125003/09/200831/10/202231/10/2022Bank
123125602/01/200731/10/202231/10/2022Bank
123152701/10/201530/06/2016(blank)Temporary
456161323/03/202031/10/202231/10/2022Bank
789161816/12/202007/10/202207/10/2022Permament
101163008/01/202120/10/202220/10/2022Bank
101165103/08/202204/10/2022(blank)Temporary
121163925/01/202219/10/202219/10/2022Permament
14516302/12/202104/10/202204/10/2022Bank
145164604/07/202204/10/202204/10/2022Temporary
167164319/04/202226/10/202226/10/2022Permament
189164921/06/202203/10/202203/10/2022Permament
189165304/10/2022(blank)(blank)Bank
201164407/05/202227/10/202227/10/2022Bank
223165213/09/202213/10/202213/10/2022Permament
245165903/10/202210/10/202210/10/2022Permament

Thanks

1 ACCEPTED SOLUTION
Greg_Deckler
Community Champion
Community Champion

@JKen1234 Maybe:

Measure 10 = 
    VAR __Date = DATE(2022,10,31)
    VAR __PN = DISTINCT('Table7'[Person Number])
    VAR __Table = FILTER('Table7',[End Date] >= __Date || [End Date] = BLANK())
    VAR __PN1 = DISTINCT(SELECTCOLUMNS(__Table,"Person Number",[Person Number]))
    VAR __Result = COUNTROWS(EXCEPT(__PN, __PN1))
RETURN
    __Result


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

2 REPLIES 2
Greg_Deckler
Community Champion
Community Champion

@JKen1234 Maybe:

Measure 10 = 
    VAR __Date = DATE(2022,10,31)
    VAR __PN = DISTINCT('Table7'[Person Number])
    VAR __Table = FILTER('Table7',[End Date] >= __Date || [End Date] = BLANK())
    VAR __PN1 = DISTINCT(SELECTCOLUMNS(__Table,"Person Number",[Person Number]))
    VAR __Result = COUNTROWS(EXCEPT(__PN, __PN1))
RETURN
    __Result


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Thank you! This got me there with a couple of changes:

=
VAR __Date = MAX('Calendar'[Date])
VAR __Table1 = FILTER('Table7',
Table7[Leaving Date]>=MIN('Calendar'[Date])&&Table7[Leaving Date]<=MAX('Calendar'[Date]))
VAR __PN = DISTINCT(DISTINCT(SELECTCOLUMNS(__Table1,"Person Number",[Person Number])))
VAR __Table = FILTER('Table7',Table7[Start Date]<=__Date&&([End Date] > __Date || [End Date] = BLANK()))
VAR __PN1 = DISTINCT(SELECTCOLUMNS(__Table,"Person Number",[Person Number]))
VAR __Result = COUNTROWS(EXCEPT(__PN, __PN1))
RETURN
__Result

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.