# Explaining Row Context In Power BI

I have found out that most people get confused in regards to understanding how DAX works in row context. This is because row context can be quite complicated and unnecessarily so. So we’re going to touch on row context throughout this article. You may watch the full video of this tutorial at the bottom of this blog.

The way I think about row context is through iterations or iterating functions. As soon as I made that distinction in my mind, it made more sense in terms of what was actually going on in the calculation engine.

As a recap, we have discussed that there are three types of contexts: the evaluation context, the filter context, and the row context.

Any DAX calculation works in a two-step process. The evaluation context always goes first and then it branches out into either filter context or row context depending on what function you write.

## Understanding Row Context

Row context is all about iterating.

I’m going to show you a couple of examples and hopefully this will help in your understanding of what row context actually does in the back end.

### Row Context VS Filter Context

In our example, the formula is being calculated via filter context because it is a simple aggregation. It sums up the entire Quantity column after all the initial filters have been put in place.

The initial filter on the Sales table is Product 1. By summing up every quantity that has been sold, we get 165.

Now I’m going to show you how to get the exact same result, but calculated in a very different way. First, I’m going to create a new measure for Quantity Sold Iteration using an iterating formula.

Examples of iterating functions include SUMX, AVERAGEX, MANYX, and COUNTX. In this case, we are going to use SUMX.

The SUMX function has asked me to put in a table, so I’m going to put in the Sales table. And then it asked me for an expression so I just put in the Quantity column.

This function is quite different to the first function, but I get exactly the same result when I drag it in.

We’re getting exactly the same results here because it is ultimately the same calculation. It was just calculated in a different way.

The Quantity Sold Iteration was calculated using row context, while the Total Quantity Sold was calculated using filter context.

### Calculating Quantity Sold Iteration Using Row Context

Let’s now go through step by step how the Quantity Sold Iteration is calculated. First, you have to remember to leave the initial valuation context, which is Product 1 in this case. And then via row context, we’re iterating through every single row in the table that we specify.

In this case, we’re specifying the Sales table and then we go and look at every single row in the Quantity column.

During the initial evaluation, Product 1 would be determined by whatever is filtered in the Product ID column. And then it counts up the rows under the Quantity column. Every time it hits a row it goes in and saves the result into memory.

After it reaches the bottom of the table, it then goes and does an evaluation of all the results that are saved into memory, and then it will do whatever calculation you ask of it, which in this case is SUMX.

### Why Use A Row Context Or An Iterating Function?

This is because inside an iterating function you can do far more complex things. You can write some very advanced logic inside.

For example, for every row in the Sales table, the quantity is multiplied by 2. You’ll see now that the same calculation process is occurring, but we’re getting different results.

This is because for every single row, we’re writing some different logic.  After going through every single row in the quantity column, we multiply the value by two, and then it gets saved into memory. At the end of that, we do a SUM.

We can also write different things here including advanced logic such as the IF or SWITCH logic.

There are heaps and heaps of examples of where it would be better to use an iterating function or row context to do a calculation rather than an aggregation function or filter context.

## Key Notes In Understanding Contexts In Power BI

To recap all the key things about context, remember that calculating in DAX works via a two-step process. The first step is always the evaluation context. It matters so much what the evaluation context is.

Once you understand what the evaluation context is for any individual result, DAX then branches out into two different ways. It can either calculate via filter context or via row context.

Choosing which context to use depends on what formula you are writing. So if you write a simple aggregation formula, it will then go and calculate it via filter context. But if you write an iterating function like SUMX, then it will calculate the formula via row context.

Once you’re getting slightly more advanced in DAX, you can actually have multiple contexts in the same function. But before you get there, you really have to understand what context is in its simplest form.

## Conclusion

Understanding the types of context is absolutely crucial as you learn DAX and want to do more advanced work. When you start writing really advanced DAX formula, you have multiple contexts working at the same time and you need to understand what each individual part of that formula is doing to achieve your desired results.

Here are some recommended links for you to learn more about the application of row context inside of Power BI:

Evaluation Context – (1.8) Ultimate Beginners Guide to DAX

Filter Context – (1.9) Ultimate Beginners Guide to DAX

Enjoy working through this one.

Sam

## Enhanced Data Visualization And Reporting Frameworks

Today, I'll talk about enhanced visualization frameworks for Power BI. This is the Analyst Hub, an...

## Automatically Find Your Top 3 Salespeople Per Region Using DAX In Power BI

There's so much you can do with DAX in Power BI, and one of these things is for you to be able to find...

## Bullet Charts: Advanced Custom Visuals for Power BI

In this tutorial, weâ€™ll discuss a custom visual called Bullet charts. They're mainly used for measuring...

## Best Power BI Reports Examples | Formula 1 Reporting Apps

For todayâ€™s tutorial, I want to take this opportunity to showcase and appreciate some of the best Power...

## Create Automated Lists Based On Ranking Calculations in Power BI

You might find a situation where across a range of different metrics you actually want to show the TopN...

## Power BI Query Parameters: Optimizing Tables

Query parameters are a really strong feature in Power BI. You may watch the full video of this tutorial...

## How To Use SWITCH True Logic In Power BI

When utilizing SWITCH / True Logic inside Power BI, you really do open up this huge expanse of...

## Custom Columns In Power BI Vs. Conditional Columns

In this blog post, weâ€™ll talk about the difference between Conditional and Custom Columns in Power BI....

## Managing Seasonality In Your Budget Analytics – Advanced Power BI

Completing budgeting analysis in Power BI is unfortunately just not that easy. What Iâ€™m going to go...

## Matrix Visualization In Power BI

I will show you formatting tricks on how to put think borders on matrix visualizations in Power BI....

## A Best Practice Implementation Strategy for Power BI

In the past, businesses were run based on what you know and who you know.  That is still true...

## Power BI Trend Analysis: Are Margins Expanding Or Contracting?

In this blog post, I will be diving into a relatively specific insight by conducting a Power BI trend...