cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
sreenathv
Solution Sage
Solution Sage

ALLSELECTED Vs KEEPFILTERS : Which is better in terms of performance?

Hi @Greg_Deckler@ImkeF , @TomMartens 

 

I have a quick question based on the following scenario:

 

1) I need a measure with specific filters in it (example Qty * RateInLocalCurrency > 50000)

2) But users would be slicing the visual based on slicers outside the visual. (example: Qty > 200 or DocumentCurrency = USD etc...)

3) I could write the measure in two ways.

 

One using ALLSELECTED()

 

 

Measure  = 
CALCULATE(
    <Something>,
    FILTER(
        ALLSELECTED(<SomeColumnsWithFiltersOutsideTheVisual>),
        <FilterCondition>
    )
)

 

 

Alternatively using KEEPFILTERS()

 

 

Measure =
CALCULATE(
    <Something>,
    KEEPFILTERS(
        FILTER(
            ALL(<SomeColumnsWithFiltersOutsideTheVisual>),
            <FilterCondition>
        )
    )
)

 

 

I tried both and they are giving the same result.

 

I am just curious to know, is there any best practice or optimal suggestion on which will be better? The ALLSELECTED() version or KEEPFILTERS() version?

 

 

1 ACCEPTED SOLUTION

There is a possible difference - assuming the columns are *not* used in the visual and that you are using more than one column in the filter.

By using ALLSELECTED, the cardinality of the iteration could be smaller if there are existing filters outside of the visual.

By using KEEPFILTERS/ALL, the cardinality is always that of the columns reference in the entire table.

In a specific report, ALLSELECTED could be faster if there are filters outside.

However the KEEPFILTERS/ALL version could better reuse the filter in multiple queries thanks to the storage engine cache.

If the cardinality is relatively large and the filters could strongly reduce them, ALLSELECTED could be faster - keeping in mind you might have issues using the measure with ALLSELECTED in other measures that have iterators - in that case, KEEPFILTERS/ALL is safer to use.

As usual, it depends.

View solution in original post

5 REPLIES 5
Greg_Deckler
Super User
Super User

Well, I'm more about what works and makes sense to you, the author. What is more natural to you in terms of your thinking and your patterns? So that when you revisit the measure a year from now you aren't trying to figure out your own code and how it works, that it makes sense to you.

 

In your case, the ALLSELECTED pattern makes more sense to me, but that's just my opinion. I personally find the KEEPFILTERS pattern unintuitive. The purpose of ALLSELECTED is to remove filters inside the query but keep those outside of the query so if that fits the bill, that seems like the best way to go. Otherwise you are starting with the entire table and then filtering down by re-implementing your filters which seems a bit asinine to me. But, I'm not the Power BI thought police.

 

Have you tried the Performance Analyzer to see if there is any difference in performance? Would be interested to know if there is a difference.

 

Might want to read this:

https://www.sqlbi.com/articles/using-keepfilters-in-dax/

 


@ 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!:
Mastering Power BI 2nd Edition

DAX is easy, CALCULATE makes DAX hard...

Hi @Greg_Deckler 

 

Thanks for the link to Marco Russo (@marcorusso )'s article.

 

I have used the performance analyzer and could not find any significant difference between ALLSELECTED version and the KEEPFILTERS version. But the SQLBI article says that whenever there is a multi-column predicate in the filter, it is better to use KEEPFILTERS than adding the filters explicitly using table functions. Apart from the semantics, the DAX engine will handle the KEEPFILTERS version better.

 

So the conclusion is that maybe,  KEEPFILTERS is closer to perfection than ALLSELECTED in this kind of scenario.

 

There is a possible difference - assuming the columns are *not* used in the visual and that you are using more than one column in the filter.

By using ALLSELECTED, the cardinality of the iteration could be smaller if there are existing filters outside of the visual.

By using KEEPFILTERS/ALL, the cardinality is always that of the columns reference in the entire table.

In a specific report, ALLSELECTED could be faster if there are filters outside.

However the KEEPFILTERS/ALL version could better reuse the filter in multiple queries thanks to the storage engine cache.

If the cardinality is relatively large and the filters could strongly reduce them, ALLSELECTED could be faster - keeping in mind you might have issues using the measure with ALLSELECTED in other measures that have iterators - in that case, KEEPFILTERS/ALL is safer to use.

As usual, it depends.

Hi @marcorusso ,

 

Thanks a lot for the explanation. When I posted this question, I was not even sure about what kind of answer to expect. But your prompt response is extremely insightful.

 

Sometimes, apart from the reports I publish,  other users in my organization will be creating other reports by connecting to the PowerBI Dataset that I publish and share with them. In that scenario, I would not have any control over what visuals and filters they will be adding in their reports based on my data model and dataset. Sometimes, I will be using the "Analyse with Excel" option and consuming the reports in excel from Power BI Datasets based on ad-hoc needs.

 

Moreover, whenever there is a 'CALCULATE', there are more things going on behind the scenes like context transitions, filters, nested CALCULATE when we reuse the measures, etc... and I had to keep thinking about more things that are likely to happen in the background than just the result of the formula temporarily. 

 

With your explanation and the perspective, I will be more confident about the reusability of my measures in various reporting scenarios. 

 

Once again thanks a lot and it took nearly 20 minutes of contemplation to actually understand the implication of what you said in your response.

Helpful resources

Announcements
Join Arun Ulag at MPPC23

Join Arun Ulag at MPPC23

Get a sneak peek into this year's Power Platform Conference Keynote.

PBI Sept Update Carousel

Power BI September 2023 Update

Take a look at the September 2023 Power BI update to learn more.

Learn Live

Learn Live: Event Series

Join Microsoft Reactor and learn from developers.

Dashboard in a day with date

Exclusive opportunity for Women!

Join us for a free, hands-on Microsoft workshop led by women trainers for women where you will learn how to build a Dashboard in a Day!

Top Solution Authors