DAX HANDBOOK
6.3 Row 2 Filter context transition

If you wish to follow along, you can find PBIX/Excel files at the bottom of the article. 

Authors

Krešimir Ledinski

Krešimir Ledinski

Microsoft certified expert in the field of Business Intelligence. His biggest passions are DAX, M, and data modeling.

Kristian Radoš

Kristian Radoš

Experienced data analyst. Advanced in SQL, PowerApps and M language.

Explanation

Context transition is one of the most powerful features of CALCULATE function. Whenever we use CALCULATE inside of a row context (iterative functions like SUMX, FILTER, or in a calculated column), CALCULATE transforms row context to filter context.

What is the difference?

In row context, values from the current row of the iteration can only see their own row, and cannot be used as filters for other rows or tables. When we transform row 2 filter context, we are making all the values of each row become filters that can travel through the model and filter it. This feature is much easier to explain through the following example:

TopQTYWrong = MAXX(VALUES(FactSales[SalesOrderNumber]),SUM(FactSales[OrderQuantity]))
TopQTYCorrect = MAXX(VALUES(FactSales[SalesOrderNumber]),CALCULATE(SUM(FactSales[OrderQuantity])))

We want to calculate the top selling order for each City in the data model. Each order might consist of more lines of products, so we need to iterate through each order, sum its values and then return the order which has the highest OrderQuantity. We will focus on Albany city.

The FactSales table, when filtered to Albany city, looks like this:

We can see that there are 4 orders in this city, each consisting of multiple lines of data. First, let’s explain the wrong calculation which returns the same figure as the [TotalQTY] measure.

No Context Transition

TopQTYWrong =
MAXX(
    VALUES( FactSales[SalesOrderNumber] ),
    SUM( FactSales[OrderQuantity] )
)

  • MAXX function is adding a virtual X column to the table created with VALUES function, and that table holds 4 SalesOrderNumbers, as shown in the picture below:
  • The X part of the MAXX calculation is the expression SUM( FactSales[OrderQuantity] ).
    The iteration starts for the first row (SO53451). The calculation for the first row is SUM( FactSales[OrderQuantity] ). Since we are inside of a row context of MAXX iteration, and we know that in row context there is no automatic filter propagation or filter context, the only filter applied to the FactSales table is the original filter context one, which is DimCustomer[City]=”Albany”. The fact that this is an iteration of the SalesOrderNumber SO53451 is irrelevant (due to no filter propagation in row context).
  • As a result for the first row of the MAXX iteration, the X part returns value 21, which is the OrderQuantity of all the orders combined. This happens for every row of the MAXX iteration, and for every row, the X part returns the same, total OrderQuantity.

After the X iteration finishes, the MAX simply takes the MAX of the X column, which is 21.
Now let’s check what happens in the correct version of the calculation.

With Context Transition

TopQTYCorrect =
MAXX(
    VALUES( FactSales[SalesOrderNumber] ),
    CALCULATE(
        SUM( FactSales[OrderQuantity] )
    )
)


The calculation is the same as the wrong one, except for the CALCULATE part.

  1. The 1st part of the calculation is the same, adding the virtual VALUES table with a column consisting of order numbers.
  2. The difference is in the X part of the MAXX function.

Prior to calculation, there is only one filter coming from the filter context, which is DimCustomer[City] =”Albany”. The X iteration starts for the first row in the table (SO53451). For the first row, it needs to perform the X calculation, which has CALCULATE function wrapped around the SUM part. When used in a row context, CALCULATE performs one extremely powerful feature called row 2 filter context transformation.

This means that every value from the current row of the iteration is transformed to a filter, which automatically travels through the model and filters it based on the conditions of that particular row. For the first row to the iteration that means that the value SO53451 becomes a filter for the FactSales[SalesOrderNumber] column, and is added to the original filter context. The evaluation context for the first row of the iteration now consists of 2 filters:

DimCustomer[City] =”Albany”
FactSales[SalesOrderNumber]=” SO53451”


The FactSales table in the first iteration of X looks like this:

After the CALCULATE performed its context transition, the required argument of the CALCULATE function is evaluated (SUM( FactSales[OrderQuantity] )), which returns value 3 for the first row of iteration.

The same transformation happens for every other row of the iteration, and we get correct X values for different sales orders.

After the X iteration finishes, the MAX takes the MAX of the X column, which is now a correct quantity of 8.

Referencing Measures

If we observe the wrong calculation in our previous example, we can see that as an iteration it is using the calculation SUM(FactSales[OrderQuantity]).

TopQTYWrong =
MAXX(
    VALUES( FactSales[SalesOrderNumber] ),
    SUM( FactSales[OrderQuantity] )
)

If we replace the SUM(FactSales[OrderQuantity]) function with a Measure [TotalQTY], The calculation starts to show correct figures.

TopQTYWrong = MAXX(VALUES(FactSales[SalesOrderNumber]),SUM(FactSales[OrderQuantity]))
TopQTY_Measure = MAXX(VALUES(FactSales[SalesOrderNumber]),[TotalQTY])
TotalQTY = SUM(FactSales[OrderQuantity])

The reason why referencing a measure returns the correct result is the fact that whenever you reference a Measure in other calculations, it always comes encapsulated in CALCULATE. The [TopQTY_Measure] could be read as follows:

TopQTY_Measure = MAXX(VALUES(FactSales[SalesOrderNumber]),CALCULATE([TotalQTY]))

CALCULATE will force the context transition and therefore the measure will return the correct figures.

Returning the Order Number

One thing is to return the quantity of the best order, but what if we need to return the exact order number/s of the best performing order? In that case, we need to start writing a bit more complex DAX code.

In the following example we will start combining multiple DAX concepts to provide the correct answer.

To produce the TopQTYName measure we used the following DAX code:

TopQTYName =
CONCATENATEX(
    TOPN(
        1,
        ADDCOLUMNS(
            VALUES( FactSales[SalesOrderNumber] ),
            "TotalQTY", CALCULATE( SUM( FactSales[OrderQuantity] ) )
        ),
        [TotalQTY], DESC
    ),
    FactSales[SalesOrderNumber],
    "; "
)

We will explain the formula through its order of evaluation referencing row numbers in sequence. We will focus only on the cell in a visual having “Albany” filter.

Evaluation Order

Row 6 – VALUES function creates a virtual table of a single column which holds all SalesOrderNumbers of the Albany city. We can represent this table with DAX studio:

Rows 5 & 7 – ADDCOLUMNS function adds a virtual column “TotalQTY” to VALUES table. For each row of the VALUES table, we perform CALCULATE( SUM( FactSales[OrderQuantity] ) calculation that does context transition and sums quantity on the SalesOrderNumber granularity.

Row 3 – TOPN function accepts that virtual table and then returns the top 1 row from it based on the TotalQTY virtual column.

Row 2 – CONCATENATEX takes the result of the TOPN function, then iterates through it and concatenates all SalesOrderNumbers with the highest TotalQTY. Under the “Albany” original filter, there are 2 SalesOrders with the same QTY, and they are both being concatenated and shown on the visual.

SUMMARIZE with Context Transition

If we need to make an iteration over a virtual table that consists of more columns and which needs to accept filter context, the best way would be to use SUMMARIZE.

With SUMMARIZE you can create virtual tables containing multiple columns, then iterate through them while performing context transitions.

Materials

We wish to create the best possible content!

If you are a novice looking for a better explanation of any element of the topic, feel free to comment on the part you didn't quite understand!

If you are an expert in DAX and believe certain topic lacks important internals, your comments are more than welcomed!

COMMENTS

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

OUR SERVICES

Prefer live training or consultations?

Table of Content

Table of Content

GET LATEST BI NEWS TO YOUR INBOX

Receive the latest updates on all business analyst news across all platforms.

By subscribing you are agreeing to our Privacy Policy.

Related blog posts

New Power BI training held for client Bugatti Rimac

In the past 2 weeks, we held a new Power BI training for the client Bugatti Rimac for a total duration of 24 hours. The training was held again at the client’s on Campus, but this time for another department, the Project Logistics team. The participants of the training with...

Read more

Nova održana Power BI edukacija za klijenta Bugatti Rimac

U protekla 2 tjedna održali smo novu Power BI edukaciju za klijenta Bugatti Rimac u sveukupom trajanju od 24 sata. Edukacija je ponovno održana kod klijenta na Kampusu, ali ovaj put za drugi odjel, tim Projektne Logistike. Polaznici edukacije uz pomoć Power BI sustava žele automatizirati svakodnevne zadatke i stvoriti...

Read more

Held PowerApps training for Žito d.d.

Last week we went to Osijek to hold a PowerApps training for Žito d.d. The training took place at the client’s office, lasting 2 days, with a total duration of 8 hours. The participants of the training have been using PowerApps in their business for some time, but they wanted...

Read more

Održana PowerApps edukacija za Žito d.d.

Prošli tjedan svratili smo do Osijeka kako bismo održali PowerApps edukaciju za Žito d.d. Edukacija se održala u 2 dana kod klijenta u uredu, u sveukupnom trajanju od 8 sati. Polaznici edukacije već neko vrijeme koriste PowerApps u svom poslovanju, no željeli su usavršiti svoje vještine u samostalnoj izradi aplikacija,...

Read more
0
Would love your thoughts, please comment.x
()
x
Scroll to Top