[Power BI] KEEPFILTERS in DAX

This post is to guide you meticulously use KEEPFILTERS in Power BI – a formula in Filter group.
Bài viết này mình hướng dẫn các sử dụng hàm KEEPFILTERS trong Power BI – một hàm thuộc nhóm Filter.

We have two tables (Product and Sales) connected each other through product_id columns.
Chúng ta có hai bảng (Product và Sales) được kết nối với nhau thông qua cột product_id.

Now, we want to calculate the profit as the following equation:
Bây giờ, chúng ta muốn tính lợi nhuận theo công thức sau:
Profit = Quantity*Unit Price – Unit Cost

Profits =
VAR cogs =
    CALCULATE(
        SUMX(
            Sales,
            Sales[quantity_sold]
                RELATED( ‘Product'[current_cost] )
        )
    )
VAR revenue =
    CALCULATE(
        SUMX(
            Sales,
            Sales[quantity_sold] * Sales[unit_price]
        )
    )
RETURN
    [revenue] – cogs

After creating a measure named Profits to return the profit from sales. Now we create a matrix to show the profit by each product.
Sau khi tạo measure tên Profits để tính lợi nhuận từ doanh số. Bây giờ chúng ta tạo một ma trận để hiển thị lợi nhuận theo từng sản phẩm.


In the next step, we will create a measure to compute the profit of store 3 only. We will conduct this task through two methods, one is using KEEPFILTERS and other is mentioning direct comparison.
Trong bước tiếp theo, chúng ta sẽ tạo một measure để tính lợi nhuận chỉ cho cửa hàng 3. Chúng ta sẽ thực hiện việc này thông qua hai cách, một là sử dụng hàm KEEPFILTERS, hai là so sánh trực tiếp trong hàm.

Profit of Store 5 using KEEPFILTERS:

Profits_Store5 (KEEPFILTERS) =
VAR cogs =
    CALCULATE(
        SUMX(
            Sales,
            Sales[quantity_sold]
                RELATED( ‘Product'[current_cost] )
        ),
        KEEPFILTERS( Sales[store_id] = 5 )
    )
VAR revenue =
    CALCULATE(
        SUMX(
            Sales,
            Sales[quantity_sold] * Sales[unit_price]
        ),
        KEEPFILTERS( Sales[store_id] = 5 )
    )
RETURN
    revenue – cogs

Profit of Store 5 using direct comparion:

Profits_Store5 (direct comparison) =
VAR cogs =
    CALCULATE(
        SUMX(
            Sales,
            Sales[quantity_sold]
                RELATED( ‘Product'[current_cost] )
        ),
        Sales[store_id] = 5
    )
VAR revenue =
    CALCULATE(
        SUMX(
            Sales,
            Sales[quantity_sold] * Sales[unit_price]
        ),
        Sales[store_id] = 5
    )
RETURN
    revenue – cogs

Profit of Store 5 using FILTER:

Profits_Store5 (FILTER) =
VAR cogs =
    CALCULATE(
        SUMX(
            Sales,
            Sales[quantity_sold]
                RELATED( ‘Product'[current_cost] )
        ),
        FILTER(
            Sales,
            Sales[store_id] = 5
        )
    )
VAR revenue =
    CALCULATE(
        SUMX(
            Sales,
            Sales[quantity_sold] * Sales[unit_price]
        ),
        FILTER(
            Sales,
            Sales[store_id] = 5
        )
    )
RETURN
    revenue – cogs

Now, we need to add a slicer to show store id options. Select store id 3 to view the result:
Bây giờ, cần thêm vào một bộ lọc để hiển thị các tuỳ chọn store id. Chọn store id 3 và xem kết quả:

As can be seen seamlessly, although we added KEEPFILTERS and FILTER inside the CALCULATE formula, choosing an option (not equal to 5) in the slicer, the profit column of the the matrix displayed nothing, while the profit column using direct logical statement still remained the same result, the profit of store 5, not being affected by slicer option.
Có thể thấy rằng, mặc dù chúng ta đã chèn hàm KEEPFILTERS và FILTER vào trong hàm CALCULATE, chọn store id bất kỳ (khác 5) ở slicer, cột profit không hiển thị gì cả, trong khi đó, cột profit sử dụng so sánh trực tiếp vẫn giữ nguyên kết quả – lợi nhuận của store 5 không bị ảnh hưởng bởi lựa chọn trong slicer.


Now, let’s create another matrix to show the profits by each store and see the difference between nested KEEPFILTERS, FILTERS and direct logical statement inside CALCULATE.
Tạo một ma trận mới hiển thị lợi nhuận theo cửa hàng để thấy sự khác nhau giữa hai trường hợp, dùng KEEPFILTERS, FILTER và so sánh trực tiếp để lọc.


However, FILTER function is used to modify or create a table of data by applying one or more filter conditions. While KEEPFILTERS function is used to preserve existing filters when applying new filters inside a calculation, typically in combination with the CALCULATE function.
Tuy nhiên, hàm FILTER được dùng để tuỳ chỉnh hoặc tạo một bảng mới cùng với một hoặc nhiều điều kiện lọc. Trong khi đó, KEEPFILTERS được dùng để giữ lại những bộ lọc đang dùng khi áp dụng bộ lọc mới bên trong một phép tính, thường kết hợp với hàm CALCULATE.

= FILTER(table, conditions)
= KEEPFILTERS(expression, conditions)
Profits_Store5 =
CALCULATETABLE(
    FILTER(
        Sales,
        Sales[store_id] = 5
    )
)

The above formula returns a table, copying all columns from Sales table, just keeping records of store 5.
Hàm trên trả về một bảng, sao chép tất cả các cột từ bảng Sales, chỉ giữ lại các dòng dữ liệu thuộc store 5.


To return value with multiple criterias, using IN and include the bracket of conditions behind.
Để trả về giá trị với nhiều điều kiện, sử dụng IN kèm với một tập hợp các điều kiện theo sau.

Profits_Store5&8 (KEEPFILTERS) =
VAR cogs =
    CALCULATE(
        SUMX(
            Sales,
            Sales[quantity_sold]
                RELATED( ‘Product'[current_cost] )
        ),
        KEEPFILTERS( Sales[store_id] IN { 58 } )
    )
VAR revenue =
    CALCULATE(
        SUMX(
            Sales,
            Sales[quantity_sold] * Sales[unit_price]
        ),
        KEEPFILTERS( Sales[store_id] IN { 58 } )
    )
RETURN
    revenue – cogs

Good luck!

Leave a comment