Ravindra BagaleCourses & study guides मराठी Track your progress

13. DAX: Data Analysis Expressions

13.9 FILTER Function in Power BI DAX with Example

FILTER function in Power BI DAX with example — in 30 seconds

What: FILTER returns only the rows of a table that pass a test. The test can use a measure.

Why: "Loyal customers" means customers with 2 or more orders. That test needs a measure, so it needs FILTER.

Tiny example: Ruhi (3 orders) and Zoya (2) pass. Salman (1) fails. Loyal sales = ₹1,400.

First look at the data (before → after):

FILTER function in Power BI DAX with example: before and after tables, fictional Blinkit Maharashtra data

Same before / after tables as text

Before — Customers and their orders

Customer Total Orders Total Sales (₹)
Ruhi 3 900
Salman 1 200
Zoya 2 500

About this data: This table uses columns: Customer, Total Orders, Total Sales (₹). This table uses columns: Measure, Value. Values shown are the lesson examples already on this page (not a new dataset).

Formula / click path

Loyal Customers = COUNTROWS(FILTER(VALUES(Customer[Customer]), [Total Orders] >= 2))
Loyal Sales = CALCULATE([Total Sales], FILTER(VALUES(Customer[Customer]), [Total Orders] >= 2))

After — Results

Measure Value
Loyal Customers 2
Loyal Sales ₹1,400

Do it step by step:

  1. Create [Total Orders] first (13.5).
  2. New measure: Loyal Customers (formula above).
  3. New measure: Loyal Sales.
  4. Put both on cards. Check 2 and ₹1,400.
  5. Rule: filter a small column table (VALUES), not the whole Orders table.

FILTER(<table>, <condition>) returns a table containing fakt the rows where the condition is true. It is an iterator. Use it when the condition involves a measure or complex logic across columns.

Loyal Customers =
COUNTROWS(
    FILTER(VALUES(Customer[Customer ID]), [Total Orders] >= 10)
)

Sales from Loyal Customers =
CALCULATE(
    [Total Sales],
    FILTER(VALUES(Customer[Customer ID]), [Total Orders] >= 10)
)

Slow Stores =
COUNTROWS(
    FILTER(VALUES(DarkStore[Store ID]), [Avg Delivery Time (mins)] > 15)
)

Do not FILTER a whole table when a column filter is enough

CALCULATE([Total Sales], FILTER(Orders, Orders[Discount] > 0)) works but iterates (एकेक ओळ फिरून हिशोब करणे) the entire fact table and can be slow. The simple form CALCULATE([Total Sales], Orders[Discount] > 0) filters fakt one column and is faster. Filter columns, not tables.

Ravindra Bagale's Tip

Friends, many students forget that FILTER returns a table, and try to use it on its own as a value in a measure. Wrap it in CALCULATE, COUNTROWS or an iterator. For conditions on a measure, filter a small column list: FILTER(VALUES(Customer[Customer ID]), [Total Sales] > 2000). This matters for both exams and interviews.

Loading DAX practice tasks...

  1. Batches with many rejects (Easy): Write High Reject Batches = number of Batches rows where UnitsRejected is more than 5.
  2. Night shift output (Easy): Write Night Units = UnitsMade added only for Night shift rows, using SUMX over a FILTER.
  3. Big batches (Easy): Write Big Batch Units = units made in batches of 400 or more, using CALCULATE with FILTER.
  4. Reject rate above 2% (Medium): Write Poor Batches = number of batches where UnitsRejected ÷ UnitsMade is above 0.02.
  5. Filter the dimension table (Medium): Write Germany Units = units made in plants where Country is "Germany", using FILTER on Plants.
  6. Good piston cost (Medium): Write Good Piston Cost = for Piston rows, add (UnitsMade − UnitsRejected) × CostPerUnit.
  7. Two tests with && (Medium): Write Day Disc Rejects = UnitsRejected added for Day shift AND Part "Brake Disc".
  8. Zero-defect batches by country (Hard): Row per Plants Country. Write Zero Defect = number of batches with 0 rejects.
  9. Plants above 2,000 units (Hard): Write Strong Plants = number of plants whose total UnitsMade is more than 2000.
  10. FILTER on ALL ignores the slicer (Hard): Slicer: Shift = Night. Write Full Runs All = number of batches in the whole table with UnitsMade = 500.

Ravi Sir's Cards for this lesson

मराठी cards