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
SQLguy
Advocate I
Advocate I

How do I make DAX measures respect cross-filtering from other visuals?

Hi all,

I need help to write a DAX measure that will respond to cross-filtering. I'm developing a report for a hospital on their homeless patient population, and I'm trying to show the percent of appointments that were completed, canceled, and no-showed based on an [Appt Status] column. I have three different sets of measures, one for each possible status. Everything works fine until that column is cross-filtered. The measures for "Canceled" status are below:

Selected Appointments = COUNTROWS( 'Appointments' )
Canceled Appointments = CALCULATE(
    [Selected Appointments],
    Appointments[Appt Status] = "Canceled [3]"
)
% Canceled = IF(
    [Selected Appointments] = 0,    --If there are no appts in the denominator, 
    BLANK(), --then display blank.
    DIVIDE( [Canceled Appointments], [Selected Appointments] ) + 0 --If there were some appts, but none were canceled, show 0.0%.
)

The underlying data looks something like this: 

Underlying data screenshot.png

In the table below, I summarize the % of appointments completed and canceled by department category. The bar chart shows the total number of appointments by status. Not cross-filtered screenshot.pngWhen I click on the bar chart and cross-filter by "Canceled" status, the "Appts" column in the table is filtered, but "Completed Appts" and "Canceled Appts" (which come from my DAX measures) do not change. As a result, "% Completed" has a non-sensical result. How do I rewrite my measures so that they will respect the cross-filtering and evaluate to zero if a particular appointment status is not in the cross-filtered filter context?

Cross-filtered screenshot.png

Thanks in advance for your help!

1 ACCEPTED SOLUTION
jdbuchanan71
Super User
Super User

Try adding a KEEPFILTERS to the CALCULATE:

Canceled Appointments = CALCULATE(
    [Selected Appointments],
    KEEPFILTERS(Appointments[Appt Status] = "Canceled [3]")
)

This will respect other filters that are also flowing into the measure.

View solution in original post

3 REPLIES 3
OwenAuger
Super User
Super User

Hi @SQLguy 

 

I recommend wrapping the Filter argument of CALCULATE in KEEPFILTERS.

Normally, the Filter arguments of CALCULATE overwrite existing filters, but using KEEPFILTERS modifies this behaviour so that they are intersected with existing filters.

 

For example, your Canceled Appointments measure would be:

Canceled Appointments =
CALCULATE(
    [Selected Appointments],
    KEEPFILTERS ( Appointments[Appt Status] = "Canceled [3]" )
)

Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

Thank you very much, @OwenAuger  and @jdbuchanan71! KEEPFILTERS solved the problem! 

jdbuchanan71
Super User
Super User

Try adding a KEEPFILTERS to the CALCULATE:

Canceled Appointments = CALCULATE(
    [Selected Appointments],
    KEEPFILTERS(Appointments[Appt Status] = "Canceled [3]")
)

This will respect other filters that are also flowing into the measure.

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.