Power BI

DAX KEEPFILTERS(): Improve Accuracy in Power BI Measures

DAX KEEPFILTERS Explained: Improve Accuracy in Power BI Measures

Table of Contents

Introduction

It is essential to understand filter context in DAX to create useful measures in Power BI. Functions such as CALCULATE(), FILTER(), and ALL() are widely used to control and manipulate this context. In a related guide, Mixing Table Functions: SUMMARIZE() & FILTER(), you can explore how combining table functions helps generate powerful insights by creating virtual tables and defining data conditions. In this article, we will learn how table functions, specifically KEEPFILTERS(), can be used to create more powerful insights.

You can download the Power BI file using the link provided below the blog post and practice this step by step.

In this article, we learn how to use table functions, specifically KEEPFILTER(), to create more powerful insights.

How filters are applied using KEEPFILTERS()

Start by creating a matrix visual:

  1. Add Country(from the Sales Territory table) to the Rows.
  2. Add Total Sales Amount (CM)and Sales in Australia (CM) to the Values.
KEEPFILTERS()
Figure 1: Matrix showing Country in rows with Total Sales Amount (CM) and Sales in Australia (CM) as values.

Notice that the same value is shown for all the countries and this value basically is the sales amount for Australia only. For all countries other than Australia, the initial filter context conflicts with CALCULATE(). CALCULATE() essentially overrides the incoming filter context. The reason for this behavior is that Sales in Australia (CM) is executed internally as below:

KEEPFILTERS()
Figure 2: DAX formula demonstrating the ALL() and FILTER() functions to calculate total sales for Australia.

In the above expression:

  • ALL() ignores the filter coming from Country Column.
  • FILTER() filters the Sales from “Australia” only and stores in a virtual table.
  • The total sales amount for the rows in the virtual table is calculated.

 

Intersect:

Now, you can stop CALCULATE() from overriding the initial filter context by using KEEPFILTERS().

KEEPFILTERS()
Figure 3: Demonstrates the KEEPFILTERS() function in DAX

To see how KEEPFILTERS() work:

 3. Right click on All Measures in the Fields.

 4. Then, click on New measure.

     Measure tools open.

 5. Here, type the following formula in the formula bar and press Enter.

Figure 4: KEEPFILTER() modifies and filters the Sales from “Australia” only and stores results in a virtual table. We calculate the total sales amount for the rows in the virtual table.

 6. Click on the “$” in the formatting section.

 7. Type “2” in the text box.

 8. Drag and drop Sales in Australia using KEEPFILTERS() from the All Measures table to the Values.

KEEPFILTERS()
Figure 5: Matrix showing Country in rows with Total Sales Amount (CM), Sales in Australia (CM), and Sales in Australia using KEEPFILTERS (CM) as values.

Notice the change in behavior of CALCULATE(). Sales in Australia using KEEPFILTERS() shows values only for cells where the incoming filter context is not in conflict with that of CALCULATE().

Conclusion

Using KEEPFILTERS() changes how CALCULATE() interacts with the existing filter context by enforcing an intersection instead of an override. This results in more precise and context-aware calculations, especially when working with visuals like matrices where multiple filters are applied simultaneously. By incorporating KEEPFILTERS(), you can avoid misleading results and ensure that your measures respect both the user’s selections and your defined conditions.

FAQs

KEEPFILTERS() ensures that filters inside CALCULATE() do not override the existing filter context. Instead, it intersects both filters, leading to more accurate results.

By default, CALCULATE() replaces any existing filters on the same column if there is a conflict. This is why you may see the same value repeated across different categories in a visual.

No. It is only needed when you want to avoid filter replacement behaviour in CALCULATE(). In many cases, the default behaviour is sufficient.

Extended Learning & References    

For more in-depth learning and advanced Power BI training, enroll in  

  Here, you can access detailed courses and resources designed to enhance your Power BI skills and take your reporting capabilities to the next level.  

Leave a Reply

Your email address will not be published. Required fields are marked *