0% found this document useful (1 vote)
81 views11 pages

Filter Context Row Context

This document introduces the filter context in DAX, which is the second evaluation context after the row context. The filter context filters columns in the data model and affects the results of DAX formulas by restricting which rows are visible. In Power BI, each cell in a matrix visual has a different filter context based on any filters applied. The filter context operates differently than the row context by changing how data is aggregated rather than accessing values in a single row.

Uploaded by

arslan akram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
81 views11 pages

Filter Context Row Context

This document introduces the filter context in DAX, which is the second evaluation context after the row context. The filter context filters columns in the data model and affects the results of DAX formulas by restricting which rows are visible. In Power BI, each cell in a matrix visual has a different filter context based on any filters applied. The filter context operates differently than the row context by changing how data is aggregated rather than accessing values in a single row.

Uploaded by

arslan akram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

0 

DAX 101: Filter context in DAX


Understanding the di erence between a row context and a lter context is the rst and
most important concept to learn to use DAX correctly. This article introduces the lter
context.

APR 11, 2022 Marco Russo & Alberto Ferrari DAX   FILTER CONTEXT  

This article is part of a series of articles about the basics of DAX. In a previous article, we
introduced the rst evaluation context in DAX: the row context. If you are not familiar with
the row context, we strongly suggest that you start by reading that article rst. Here, we
build upon your knowledge of the row context to introduce the second evaluation context:
the lter context.

Be mindful that the most relevant information you need to master about evaluation contexts
is the di erence between the row context and the lter context. You cannot appreciate any
di erence until you know exactly what the two contexts are. This is the reason why
approaching the lter context without any existing knowledge about the row context would
be only partially useful.

After this introduction, it is time to dive into the details of the lter context. The lter context
is nothing but a lter currently active on the data model. Filters are almost always present
during the calculation of a DAX formula, and they a ect the results.

For example, the following measure seems to compute Sales Amount over the entire Sales
table, because there are no speci c lters set inside the expression:

Measure in the Sales table

1 Sales Amount :=
2 SUMX (
3     Sales,
4     Sales[Quantity] * Sales[Net Price]
5 )

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
COPY CONVENTIONS #1

And yet, depending on the lter context the formula will compute di erent values. If a user
slices by Product[Brand], the formula computes Sales Amount for di erent brands, as you
can see in the following matrix.

Despite the behavior being intuitive, we need to understand it a little deeper. At the grand
total, the formula computes the sales amount for all the rows. In the other rows of the
matrix, it computes di erent values. Although the engine is computing the same formula, it
produces di erent values in di erent cells. The di erence among the cells is the lter
context. The matrix creates a lter context that is di erent for each cell. Consequently, the
engine computes the same formula under di erent lters, therefore producing di erent
results.

For example, the highlighted cell is showing the sales amount for Contoso, because the
measure is being computed inside a lter context that shows only the value Contoso for the
Product[Brand] column.

A very common mistake made by newbies is to think that each row in a matrix is evaluated
in a row context, because they equate a row in a matrix with the row context. This is not the
case. A row context exists within an active iteration. No iteration, no row context. Therefore,
the individual rows of the matrix are not computed inside a row context. Each cell of the
matrix is evaluated in a lter context that happens to lter a single value for the Brand
column. If you carefully think about that, despite each cell in the matrix being a single row,
its value is the aggregation of several products – namely all the products from the same
brand. The lter context in each cell is not ltering one particular row from Product. The
lter context is ltering one individual value for the Product[Brand] column. Hence, the lter
actually results in multiple rows being visible in the Product table.

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
The lter context can lter multiple columns at once. If you add the year on the columns of
the matrix, the scenario becomes a bit more complex.

The red box is highlighting the value we just saw in the previous gure. In the red box
(Contoso, Total), there are no lters on the year, therefore the lter context is ltering only
Contoso. In the green box (Contoso, 2018), however, there are two columns being ltered:
one for the Contoso brand and one for the year 2018. Again, same formula, di erent lter
contexts, hence di erent results.

The lter context lters columns. The lter context is created by Power BI to re ect any lter
that is active on the current cell. Filters can be activated by the current visual, or by using
the lter pane, by cross- ltering visuals, slicers or by any other available means. No matter
where they originated from, all these lters are combined into a single lter context that is
active in the cell of the visual. Moreover, every cell has a di erent lter context. If two cells
had the same lter context, they would show the same value.

One way to think about the di erence between the row context and the lter context is to
focus on their use. A row context is needed whenever you operate on values in a single row
of a table. The lter context, on the other hand, changes the way data is aggregated and/or
scanned.

Power BI does not create the row context of each cell by running a separate query for each
cell. Instead, it uses the SUMMARIZECOLUMNS function that performs a grouping by some
columns and then it computes expressions in the lter context resulting from the group-by
columns. The following query is a simpli cation of what the previous matrix is showing (it is
missing the subtotals). If you are a DAX newbie, the formula itself is likely to be
overwhelming. So focus only on the separation of lter context vs. row context, explained
after the code:

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Query

1 EVALUATE
2 SUMMARIZECOLUMNS (
3     'Product'[Brand],                             -- Groups by Brand
4     'Date'[Year],                                 -- Groups by Year
5     "Sales Amount",                               -- Adds a new colum
6         SUMX (                                    -- Starts an iterat
7             Sales,                                -- Iterates sales o
8             Sales[Quantity] * Sales[Net Price]    -- Multiplication,
9         )                                         -- After SUMX there
10 )

COPY CONVENTIONS #2

SUMMARIZECOLUMNS retrieves all the values of Product[Brand] and Date[Year]. For each
combination of brand and year, it creates a lter context with that combination and then
evaluates the expression for Sales Amount. When the SUMX function starts, the lter context
is already active, therefore SUMX scans only the rows belonging to the given combination. It
then computes the multiplication of Sales[Quantity] times Sales[Net Price] row by row.

The two contexts (row and lter) operate in di erent parts of the expression, achieving
di erent goals. At the risk of being pedantic, let us repeat this important detail: the row
context is not the same as the lter context. The row context exists only in calculated
columns or during an iteration, whereas the lter context is set for the entire formula,
restricting the rows visible in the model.

The lter context is created automatically by Power BI as part of the evaluation of DAX
queries to populate the visuals. Developers have the option of creating their lter context
too, by leveraging CALCULATE. For example, to compute the value of Sales Amount for
only the customers living in Europe, you can use the following expression:

Measure in the Sales table

1 Europe Sales :=
2 CALCULATE (
3     [Sales Amount],
4     Customer[Continent] = "Europe"
5 )

COPY CONVENTIONS #3

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
CALCULATE changes the lter context under which Sales Amount is being computed, by
setting a lter on the Customer[Continent] column for it to be Europe. Nonetheless, talking
about CALCULATE would require much more time and e ort. Here, the purpose of the
article is to introduce the lter context and highlight some di erences between the row and
the lter contexts.

There is another important di erence between the row context and the lter context. The
row context iterates a table. Therefore, during the iteration you can access the value of
each column of only the iterated table. Columns in di erent tables are not a ected by the
presence of a row context, even if there are relationships linking the tables together.

The lter context, on the other hand, operates at the model level. When you place a lter on
a column, the lter a ects the column, the table that the column belongs to and all the
tables that can be reached through relationships. By default, the lter is propagated from
the one-side of a relationship to the many-side. Nonetheless, by activating bidirectional
relationships, you can propagate the lter context both ways: from the one-side to the
many-side and from the many-side to the one-side.

The direction of propagation of the lter context is re ected in the arrow found in the
middle of any relationship in the Power BI diagram view.

By default, the propagation occurs from the one-side towards the many-side, meaning that
a lter on the Date table is propagated to the Sales table, and the same applies from
Product towards Sales. But then a lter on Sales is propagated neither to Product nor to
Date.

You have the option of changing how the lter context propagates by activating
bidirectional relationships. If the relationship between Product and Sales were a
bidirectional relationship, then a lter on Sales would propagate to Product. Word of advice,
bidirectional relationships are a powerful and dangerous tool. Do not use bidirectional
relationships unless you have a very clear understanding of data modeling concepts and

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
DAX. If you are interested, you can nd more information about the perils of bidirectional
relationship here: Bidirectional relationships and ambiguity in DAX – SQLBI.

Conclusions
There would be a lot more to talk about when it comes to the row context and the lter
context. These concepts are the foundation of the entire DAX language, which is why we
created a suite of books, courses, and videos about these important concepts. In this
article, we merely scratched the surface. If you want to learn more, then start by watching
the free Introducing DAX video course. Once you have digested that content, proceed with
one of our in-person classroom courses and/or with watching the Mastering DAX online
video course.

Keep in mind that reaching a solid understanding of both the row context and the lter
context is of paramount importance if you plan on becoming a good DAX developer.

Articles in the DAX 101 series


Computing running totals in DAX

Counting working days in DAX

Summing values for the total

Year-to-date ltering weekdays in DAX

Creating a simple date table in DAX

Automatic time intelligence in Power BI

Using CONCATENATEX in measures

Previous year up to a certain date

Sorting months in scal calendars

Using USERELATIONSHIP in DAX

Mark as Date table

Row context in DAX

Filter context in DAX

Introducing CALCULATE in DAX

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Understanding context transition in DAX

Using KEEPFILTERS in DAX

Variables in DAX

Using RELATED and RELATEDTABLE in DAX

Introducing ALLSELECTED in DAX

Introducing RANKX in DAX

Download
Insert your email address and press Download for access to the les used in this article.

Your email address DOWNLOAD

Keep me informed about BI news and upcoming articles with a bi-weekly


newsletter (uncheck if you prefer to proceed without signing up for the newsletter)

Send me SQLBI promotions (only 1 or 2 emails per year)

By downloading the le(s) you are agreeing to our Privacy Policy and accepting our
use of cookies.

PUBLISHED APR 11, 2022

WRITTEN BY

Marco Russo & Alberto Ferrari

Marco Russo and Alberto Ferrari are the founders of SQLBI, where they regularly publish articles
about Microsoft Power BI, DAX, Power Pivot, and SQL Server Analysis Services.

Marco and Alberto have worked with Analysis Services, Power BI and Power Pivot since the rst
versions, becoming established experts. They already wrote 10 books on these technologies and
provide consultancy and mentoring. They are also regular speakers at major international BI
conferences, including Microsoft Ignite, Data Insight Summit, PASS Summit, and SQLBits. And of
course, they are quali ed trainers, with more than 250 classes taught so far.

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
 NOV 13, 2022

Row context in DAX


Understanding the di erence between row context and lter context is the rst and most important
concept to learn to use DAX correctly. This article introduces the row context, and is part of a series of
articles about evaluation contexts in…  READ MORE 

 JAN 10, 2023

Debugging DAX measures in Power BI


This article describes di erent techniques to debug a DAX measure that returns an incorrect result, with
and without external tools.  READ MORE 

 DEC 22, 2022

Customizing date and time intelligence templates in Bravo for Power BI


This article explains how to create a Bravo for Power BI template to customize a Date table and the
related Time Intelligence measures created by the tool.  READ MORE 

 DEC 11, 2022

Solving errors in CALCULATE lter arguments


The lter arguments in CALCULATE can be written as logical conditions with certain restrictions. This
article explains the more common errors in these conditions and how to solve them.  READ MORE 

 NOV 22, 2022

Welcome to SQLBI+

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
SQLBI+ is our new subscription service for advanced content that supports professional model authors
who create semantic models for Power BI and Analysis Services.  READ MORE 

53:04

 JAN 17, 2023

Debugging DAX measures in Power BI


WATCH NOW 

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
09:10

 JAN 11, 2023

Variations on like for like comparison – Unplugged #45


WATCH NOW 

05:32

 DEC 30, 2022

What happened in DAX in 2022


WATCH NOW 

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Comments

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy