Open In App

Row Context and Filter Context in Power BI

Last Updated : 24 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In Power BI, the Row Context and Filter Context govern how calculations and interactions are performed within the data model. Row context and Filter context are specifically useful to understand and implement Data Analysis Expressions(DAX) properly. This article covers Row Context and Filter Context in Power BI, along with examples for a better understanding of the concept.

Row Context and Filter Context in DAX

Data Analysis Expressions (DAX) is a programming language that is used throughout Microsoft Power BI for creating calculated columns, measures, and custom tables. In the example of Row Context, the DAX function, SUMX, is used to calculate the measure by iterating row by row. In the example of Filter Context, the DAX function, CALCULATE, is used.

What is Row Context in Power BI

Row Context refers to the calculations and evaluation of values for each individual row and the values for the corresponding columns with respect to the current row. For example, if we have a Financial Data table, where there are two columns namely Units Sold and Price per Unit. Then a measure called Total Price can be calculated as follows:

Total Price = (Price per Unit) * (Units Sold)

The Row Context allows the measure, Total Price, to perform calculations based on the specific values in each row, such as multiplying the Price per Unit by the Units Sold in the current row.

What is Filter Context in Power BI

Filter context refers to the set of filters applied to the data when performing calculations in Power BI. It determines which subset of data is included or excluded before the DAX calculations start, based on the active filters at any given time. As the active filter changes, calculations adjust dynamically. The filter context is applied to the entire measure, and the visibility of certain rows is restricted based on the active filters at the time.

For example, if we take into account the above measure, Total Price, and we apply a filter to show data only for a specific product, the filter context is modified to include only the data related to that product. The Total Price measure will then be computed based on the filtered data, providing the information specific to the selected product.

Difference Between Row Context and Filter Context

Thus, from the above discussions on Row Context and Filter Context, it can be inferred that, if row-by-row calculations or calculations related to one row at a time are taken into account, then it is Row Context. Whereas, if calculations on a set of rows are done, where the set of rows is not specific and can change dynamically depending on the active filters at any point in time, then Filter Context is operated.

Row Context and Filter Context in DAX Example

Now let us see an example of Row Context and Filter Context in Power BI.

Row Context in DAX Example

In the dataset, we are provided with the Number of Units Sold, the Cost Price of each unit, and the Selling Price of each unit. Hence with the given data, we can easily calculate the Cost Price, Selling Price, and Profit.

To calculate the Cost Price of each product, the following formula is used

Cost-Price.png
The formula of Cost Price of each product

To calculate the Selling Price of each product, the following formula is used

Selling-Price.png
The formula of the Selling Price of each product

To calculate the Profit of each product, the following formula is used

Profit.png
The formula of Profit of each product

Note: It is to be noted that here that SUMX is used instead to SUM as the Selling Price, Cost Price, and Profit are, calculated for each row. SUMX iterates row by row the formula inside its function whereas SUM would have calculated the aggregate of values for the whole column.

Demonstration of Row Context on the Dashboard with the help of Profit Measure

Now let us make a dashboard using Profit measure and let us see Total Profit is calculated for each row and for the whole column:

Figure 1: Profit for Countries under Region Asia and Sales Channel as Offline
Profit-Dashboard-2
Figure 2: Profit calculated for each Region and Order Priority row-wise
Profit-Dashboard
Figure 3: The value in each row specifies Profit in each region according to Item Type
Profit-Dashboard-4
Figure 4: The value in each row specifies the Yearly Profit for each Region

From the above Figures, it can be seen that Total Profit is calculated both row-wise as well as column-wise.

Filter Context in DAX Example

By using Filter Context, it can be shown how the Total Profit as a whole and also row-wise as well as column-wise changes as the active filter changes, thus revealing the dynamic nature of this context. First, the Construction of a Dashboard with filters Region as Asia and Sales Channel as Offline

Filter
Figure 1: Profit for Countries under Region Asia and Sales Channel as Offline
Filter_1
Figure 2: Profit according to Order Priority for the Asia Region and Offline Sales Channel
Filter_2-(1)
Figure 3: Profit of each Item Type for countries under Region Asia and Sales Channel Offline
Filter_3
Figure 4: Yearly Profit for Asia Region under Offline Sales Channel

From the above Figures, it can be seen that the Total Profit has also changed after the filter conditions have been applied. Since the Region chosen has been Asia, only Countries under Asia whose Sales Channel is Offline are Shown. The total Profit for each Country under Asia is shown. Profit of Asia under the Offline channel for each year can be seen. Other Regions in the Region and Year table have been excluded, thus restricting their visibility. Total Profit of 139.34 M is in respect to Asia with Offline sales.

Thus, an important point to note here that is if Row Context and Filter Context are to be used simultaneously, then first the Filter Context will be executed. Depending on the filter conditions, the Row Context will be executed after that.

CALCULATE Function in Filter Context

If CALCULATE function is used, then it will overwrite all the internal filters, and only the filter specified in the function will be executed. The external filters will be still executed. Let us understand it with an example.

Example of CALCULATE Function

Let us calculate a measure called Asia Products, where the Total Number of Units Sold in Asia will be calculated. Here the internal filter will be the Region Asia. The formula for CALCULATE is as follows:

Step 1: Calculation of Total Number of Units Sold in Asia with CALCULATE Function

calculate
Calculating the Total Number of Units Sold only in the Asia Region with the help of CALCULATE function

Step 2: Demonstration of CALCULATE Function on the Dashboard

Now let us design a dashboard with the measure calculated. 

Filter-2
Figure 1: The column Asia Products shows the Sum of Units Sold only in the Asia region

Here with the help of CALCULATE function, the Total Number of Units Sold with respect to Region Asia is calculated. As can be seen from the column Asia Products, the total number of units sold in Asia is shown for all the rows irrespective of the Region. Hence if there would have been other Region filters, it has been overwritten with Region as Asia filter.

Filter-3.jpg
Figure 2: CALCULATE function with external filters Sales Channel Offline and Order Priority H
Filter-4.jpg
Figure 3: CALCULATE function with Sales Channel Online and Order Priority M

Thus, it can be seen that the Total Number of Units Sold in Asia changes only when the external filters namely, Sales_Channel and Order_Priority change. Hence CALCULATE function overwrites the internal filter, but doesn’t have any impact on external filters.

Conclusion

Understanding and managing both Row Context and Filter Context is essential for accurate calculations and meaningful data analysis in Power BI. By leveraging these contexts effectively, one can create calculations that consider individual row values and dynamically respond to user interactions through applied filters.



Similar Reads

Power BI - Ribbon Charts and Multi-Row Card Visualization
In this article, we will learn to implement basic ribbon charts and multi-row card visualization using Power BI. This discusses some important concepts used to create the very common bar and column charts so as to make large business intelligence decisions. We will be discussing the following topics and their implementation in the Power BI desktop.
5 min read
Power BI - Add alternative Row Colors to the Table
In Microsoft Power Bi, “Alternate Row Color” is substantially used in a table to display their data more accurately. utmost of the association needs to present their data professionally. Applying alternate and unique colors to the data makes the waste more perfect, making the data view in various formats. It is a very common method to add colors to
2 min read
Power BI - Format Multi-Row Card
Multi-Row cards in Power BI help us display multiple values, corresponding to different dynamic scenarios. For example, want to know the total population of the world, the average world's economy, annual total deaths all in a single card, to achieve such tasks we use multi-row cards. Under the fields section, the first dropped column acts as the ro
6 min read
Power BI - Differences Between Microsoft Power BI and SSRS
In this article, we will cover what is SSRS and Power BI, their Features, Types, and Differences Between Microsoft Power BI and SSRS. What is SQL SSRS (Server Reporting Services)?SSRS stands for SQL Server Reporting Services. A tool developed by Microsoft is used to create visualizations and reports on data such as graphs, tables, charts, etc. It c
5 min read
Power BI : Hiding tables, columns, and fields from Power Pivot
A robust data modeling tool in Power BI called Power Pivot enables users to build data models by fusing several data sources, building relationships, and including computed columns and metrics. But occasionally you might want to exclude some tables, columns, or fields from the Power Pivot view in Power BI. This could be done to clean up the area, s
4 min read
Power BI - Differences between Power BI personal Gateway and Data Management Gateway
In Power BI, a gateway is a software component that enables secure and seamless data connectivity between Power BI and on-premises data sources. A gateway acts as a bridge between the cloud-based Power BI service and on-premises data sources, allowing you to use Power BI to access and visualize data that is stored on-premises. We may need to use a
5 min read
Connect Power BI Desktop with Power BI Service
Power BI Desktop is the system version of Power BI present in the local system, It is downloaded as a free application. The comprehensive report authoring tool for report designers is Power BI Desktop. You can connect to many data sources through the desktop and then turn the data into a data model. The addition of visuals based on the data model i
5 min read
Power BI - How to edit in Power BI App?
Power BI is a data visualization application that lets you connect, transform and find insights into the most pressing matters of your business. It helps in sourcing your data and creating visual dashboards, KPIs, and reports by editing the data as per your concerns. You can collaborate, share and integrate your data easily across products document
8 min read
Power BI - Rise of Microsoft Power BI as a Data Analytics powerhouse
Today's analytics and machine learning both greatly benefit from data science, which deals with data. Finding patterns and interesting findings is made easier with the use of data science and exploratory data analysis. Through data exploration, EDA enables us to learn about entirely new facets of data. Maximizing earnings while also boosting organi
9 min read
Power BI - Tools and Functionalities
Power BI is a Data Visualization and Business Intelligence tool by Microsoft that converts data from different data sources to create various business intelligence reports. It provides interactive visualizations using which end users can create reports and interactive dashboards by themselves. It is highly recommended to download Power-BI tool befo
5 min read
Article Tags :