Row Context and Filter Context in Power BI

In this article you will learn how row context and filter context work in Power BI

Introduction

Power BI is a powerful tool for data analysis and visualization. Two fundamental concepts that are crucial to understand in Power BI are Row Context and Filter Context. These contexts determine how calculations are performed and how data is filtered in your reports and dashboards. This report will explain these concepts in simple terms, provide examples, and discuss real-life scenarios where they are used or might fail.

Functions

1. Row Context: Refers to the context in which a calculation is performed on a specific row of a table. It is the scenario where Power BI evaluates a formula for each row individually.

2. Filter Context: Refers to the set of filters applied to data before performing a calculation. It is the scenario where Power BI evaluates a formula based on a subset of data defined by certain criteria.

Conditions Required

  1. Row Context:
    – Each row is evaluated independently.
    – Calculations use column values from the same row.
  2.  Filter Context:
    – Filters can come from slicers, report filters, or DAX formulas.
    – Calculations use a subset of data defined by the filters.

Examples

Row Context Example

Imagine a sales table with columns for quantity and price, and we want to calculate the total revenue for each sale:

Total Revenue = Sales[Quantity] * Sales[Price]

This formula calculates the total revenue for each row independently.

Filter Context Example

Now, let’s calculate the total sales for the ‘Electronics’ category:

DAX Formula:

Total Sales Electronics = CALCULATE(SUM(Sales[Total Revenue]), Sales[Category] = ‘Electronics’)

This formula changes the filter context to only include rows where the category is ‘Electronics’.

Real Life Scenarios in Power BI Where They Are Used

Row Context Scenarios

1. Calculating Individual Sales Amount:

Sales Amount = Sales[Quantity] * Sales[Price]

2. Creating a Profit Margin Column:

Profit Margin = (Sales[Revenue] – Sales[Cost]) / Sales[Revenue]

3. Concatenating Strings:

Full Name = Sales[First Name] & ‘ ‘ & Sales[Last Name]

Filter Context Scenarios

1. Calculating Total Sales by Category:

Total Sales by Category = CALCULATE(SUM(Sales[Total Revenue]), Sales[Category] = ‘Electronics’)

2. Calculating Year-to-Date (YTD) Sales:

YTD Sales = CALCULATE(SUM(Sales[Total Revenue]), DATESYTD(Sales[Date]))

3. Filtering by Multiple Criteria:

Total Sales Product Region = CALCULATE(SUM(Sales[Total Revenue]), Sales[Product] = ‘Laptop’, Sales[Region] = ‘North America’)

4. Calculating Rolling Averages:

Rolling Average 7 Days = CALCULATE(AVERAGEX(DATESINPERIOD(Sales[Date], LASTDATE(Sales[Date]), -7, DAY), Sales[Total Revenue]))

Real Life Scenarios in Power BI Where They Will Fail

1. Incorrect Use of Row Context in Measures:
– Measures do not inherently have row context. Attempting to reference individual row values directly in a measure without using an iterator function (like SUMX or AVERAGEX) will fail.
Example: `Total Sales = Sales[Quantity] * Sales[Price]` in a measure will not work without an iterator.

2. Misapplying Filter Context in Calculated Columns:
– Calculated columns are evaluated at row level, so applying filter context using CALCULATE in calculated columns might not yield the expected results.
Example: `CALCULATE(SUM(Sales[Total Revenue]), Sales[Category] = ‘Electronics’)` in a calculated column will not apply the filter correctly.

Introduction to VertiPaq

VertiPaq is a high-performance storage engine used in Power BI, SQL Server Analysis Services (SSAS), and Excel. It is designed to handle large amounts of data efficiently by compressing and storing it in memory. VertiPaq’s in-memory columnar storage format allows for rapid query processing, making it a critical component in data analysis and business intelligence.

How Row Context and Filter Context Work Using VertiPaq

Row Context and Filter Context

Understanding VertiPaq involves grasping the concepts of Row Context and Filter Context, fundamental to how Power BI processes calculations.

Row Context:
– Refers to calculations performed on each row of a table individually.
– Each row is evaluated independently, making it efficient for calculations that need to reference values within the same row.

Filter Context:
– Involves applying filters to data before performing calculations.
– Enables calculations on a subset of data defined by specific criteria, facilitating dynamic and flexible data analysis.

How VertiPaq Enhances Performance

Row Context with VertiPaq
– VertiPaq processes each row independently, allowing for efficient storage and quick retrieval of data. By compressing each column separately, VertiPaq optimizes memory usage and speeds up query performance.

Filter Context with VertiPaq
– VertiPaq applies filters to compressed data efficiently, enabling fast aggregation and calculation on filtered subsets of data. This process reduces the amount of data that needs to be scanned and processed, leading to quicker response times for complex queries.

Scroll to Top