Power BI: Filter vs Row Context. ... a concept made simple. | by Peter Hui | Towards Data Science

I am what you term a “super commuter”. I used to wake up at 5 am. Leave the door at 5:30 am, catch the train and arrive at work by 8:00 am. It sounds like a nightmare. I love my job and I love it enough to do that. Then the pandemic came and it’s not something I have to do as often anymore. I am thankful of that, but what does this have to do with Power BI filter and row context?
You see, filter and row context can be seen in common situations if you know what you are looking for. I ride the train. The train is divided into different coaches and within each coach there are two different types of seats. There are the window seats and aisle seats.
I am a very visual person. I will draw out what I mean here.
Here is my attempt to draw a train in Excel. Here you can see that in this train there are 3 coaches and a controller room.
Now, what does filter and row context mean here?
Let’s pretend you are a fare inspector. You need to find a family on the train and find out how much they had spent on the fares. You know they ride in the first coach and they are all sitting at window seats.
In this scenario, you will need to go to the first coach and go to the window seats and tally up their ticket prices. This, in its simplest form, demonstrates filter context and row context.
Here the filter context would be the first coach and window seats. The row context would be the individual seats. The tally of how much they have spent would be the aggregation.
If you translate this into Power BI, this would be a measure of something like this.
A =SUMX (    FILTER ( Train, Coach == "First" && Seats == "Windows" && Family == "Smith" ),    [Ticket_Price])
Here you would tell Power BI to go to the first coach, go down the window seats, find the family group — this is the filter context. The next question is equally, if not more important.
What do you want to do with this? Power BI goes to the location but doesn’t know what to do. It needs to know what to do when it gets there! This is row context. Here with SUMX, you are telling Power BI to sum up, row by row, or in our metaphor, seat by seat, the ticket price.
Now for another example, what if I want to know, in the second coach, the total price of the window seats and aisle seats by occupancy?
You, being the fare inspector, would once again go to the second coach, walk to the window seats and aisle seats. Now what do we do? Now we need row context. What are we doing by row? We need to add up the cost of the window seats and aisle seats. This is the SUMX in Power BI. You will add up the price by window seat and add the price of the aisle seat, and move on to the next row and do the same.
Basically, that is the filter and row context. In Power BI, you are no longer looking at trains but rather looking at tables. You will need to filter tables by certain criteria and then the aggregation.
Here, when users filter, likely in a visual, they will slice by the coach numbers and style, whether it is occupied etc. This will be the filter context. Finally, to calculate the Price*Promotion would be the row context to which it applies.
Okay, now what about all these? ALLEXCEPT, ALLSELECTED and ALL? What do these terms mean?
Let’s go back to our trains. You are the fare inspector but now you have an assistant. This assistant will help you by recording your totals by group. That is the ALLEXCEPT function in Power BI.
Visually, you can see this on the train again.
As a table in Power BI, it will look something like this.
Think of these filter functions as your helpers to change the way you can tally up your data. ALLEXCEPT will help you group according to your criteria, ALL will remove any filters you have specified and ALLSELECTED will use user selections to create filters.
I won’t go through all of this, but the concept is the same, they act as assistants to your task. In Microsoft terms, they change the “context” of your filters.
I hope this helps to clarify what filter context and row context mean in Power BI. Now of course, this is just one train. This only helps to illustrate a simple concept, you may see many trains and joined on the same track etc. The concept is the same.
I will attempt to explain this in my next article :)
I hope this helps in your data journey!