Skip to main content

Do we really need Keepfilters in DAX?

Writing a DAX can be tricky at times and making it efficient for the system is always a challenge for a BI Developer. The most prominent example is the use of filters in DAX. There are zillion ways to filter something in DAX and attain the same result but when you look behind the scenes you will get to know what effect it makes on performance.



Looking at the above image it all looks the same. In this blog, we will cover the most common scenarios to use filters including the use of filter with all, filter with values, and keepfilters. I am using the sample superstore data for demonstration. The idea is to get sales for "Tables". The DAX is as follows.

The most common and basic approach will be this. Looking at the matrix it gives the sales amount of tables as the total sales for "Furniture". If we use this in a table or matrix results can be perplexing for the user. Considering the total it does make sense. But we want every sub-category to get their respective sales or show blank.

Coming to the second method i.e. use of ALL and filters. What does it actually do? It removes every filter applied to the visual. I am using it with filters and hence we are getting only the Table sales for the whole category. It is a bit similar to the basic filtering.


Someone who has a decent experience in writing DAX always recommends the use of filter and values together. What does it actually do? The use of values removes the duplicates and provides distinct values. The DAX will be similar to the filter with all just replace all with values. 


As mentioned in DAX we only sales for table values and will only consider the table as the only subcategory and showcase blanks for other subcategories. DAX experts always suggest either using a filter with values or the use of keepfilters. 

The use of keepfilters helps to handle multiple filters without losing any external filters applied. Using it with calculating will help to add additional filter context to the existing setup and doesn't overlook the external filters.


Now we are clear with the DAX let's take a look at the performance of every DAX in DAX Studio. BTS time!! To do so I have connected my Power BI report to DAX Studio and defined the measures there. How to do it? Just right-click on the specific measure and you will get an option to define your measure. We will be publishing a detailed blog on how to use DAX Studio soon.


On line 12 I have included an evaluate function to get the same result as in Power BI. Now we will take a glimpse at the server timings.

The first thing I noticed is using filters generates 2 queries in the background which will affect the load time at the end. Also, the number of rows the DAX scans to give the result. Similarly, I did this exercise with a filter with values and the server timings are as follows.


The number of queries is still the same but the number of rows gets reduced which is a good sign for the performance. Let's see the ultimate result for the keepfilters. 

Magic!! the number of queries is reduced to 1. When comparing all of these DAX the first consideration is always the performance and the use of keepfilters turns out to be the most efficient method to get the sales for tables. As a BI developer, you should be aware of the performance of your DAX having said that there is no hard rule to use keepfilters every time it depends on the use case ultimately.




Thanks for Reading  Let's connect on LinkedIn. For more such blogs and pro tips do follow us








Comments

Popular posts from this blog

Ultimate Beginners Guide to DAX Studio

There are zillions of external tools available with Power BI but DAX Studio is one of the most commonly used tools to work with DAX queries. It is a perfect tool to optimize the DAX and the data model. In this blog let's shed some light on the basic functionalities that can take your report to the next level. ARE YOU READY?  To start you will need the latest version of the DAX Studio. You can download it from their website . Don't worry you don't have to pay for the license. Fortunately, DAX Studio is a free tool As a BI Developer, I am using DAX Studio regularly. Based on my experience I use it for several purposes but in this blog, I will highlight the most common ones. Extracting a dump of all the measures used in your PBIX. Why do we need to do this? It can be used for documentation purposes also sometimes we try to reuse the DAX and such a dump comes in handy in this scenario. How to achieve it? Open the DAX Studio it is located under the external tools once you open t...

Identify and Delete Unused Columns & Measures

Heavy dashboards and a bad data model is a nightmare for every BI Developer. Heavy dashboards can be slow due to multiple reasons. It is always advised to stick with best practices. Are you still figuring out about those best practices then you should definitely have a quick read on Best Practice Analyser ( link ). One of the most common issues with slow dashboards is unused columns and unused measures.  It is very normal to load some extra columns and create some test measures in your dashboard but as a part of cleanup process those unused columns and unused measures should be removed. Why we are removing them? Because if you keep them then ultimately it will increase the size of your data model which is not a good practice.  How to identify the culprits (unused columns and unused measures)? In today's blog we will provide you with 2 most common external tools which will help you in identifying the culprits. More external tools😒. Who's going to pay for this? To your surprise...

Best Practice Analyser (BPA) Guide

Do you want to save tons of efforts to check if your data model and PBIX file follows the standard best practices and norms? Then this blog is for you. If you are a follower of our channel we already deep dive into the importance of the DAX Studio as an external tool. If you are a beginner I would highly recommend to visit this blog . In today's blog we will check how Tabular Editor can help to optimize the data model.  Best Practice Analyser allows to define or import best practices. It will make sure that we do not violate the best practices while developing a dashboard. Isn't it exciting!! Before we start make sure you already have Tabular Editor version 2.24.1 installed on your system. To install it do visit this link and select the link for windows installer. Once Tabular Editor is installed it will reflect in your PBIX file under external tool. Also, we need to define the standard rules. To do so in your advanced scripting or C# script copy this and save it via Ctrl+S. An...