Skip to content
Jessica Hill By Jessica Hill Software Engineer I
CALCULATE in DAX

What is CALCULATE?

CALCULATE (and CALCULATETABLE) is the only function in DAX that has the power of changing and updating the filter context.

The filter context active in each cell of a report is generated by:

  • Row Selection.
  • Column Selection.
  • Slicer Selection.
  • Report Filters.

CALCULATE can be used to alter the filter context coming from the report through columns, rows, slicers etc. Inside CALCULATE, you can change and update this filter context and then compute values on top of it.

Discover your Power BI Maturity Score by taking our FREE 5 minute quiz.

How does CALCULATE work?

The syntax of CALCULATE is very simple: it is just CALCULATE, followed by an expression and then a set of filters. The filters are evaluated, and their result can change the filter context. Finally, once all the filters have been evaluated and the new filter context has been created, then CALCULATE will evaluate the expression in the new filter context.

When we use the following expression in our Power BI report to compute the sales amount of red products:

Red Sales =
SUMX (
    FILTER (
        Sales,
        RELATED ( 'Product'[Colour] ) = "Red"
    ),
    Sales[Quantity] * Sales[Net Price]
)

The inner FILTER function is returning the 'Sales' table but filtered so that only the rows where the 'Product' colour is 'Red' are returned. SUMX then iterates over this filtered table to calculate the Sales[Quantity] * Sales[Net Price] for each row in the table.

Power BI Weekly is a collation of the week's top news and articles from the Power BI ecosystem, all presented to you in one, handy newsletter!

When applied to the table visual in the report we only see the value for 'Red Sales', in the 'Red' row of the report. This is because the 'Red Sales' measure takes into account the external filter context applied in each row of the report.

In the first row, the filter context is 'Azure', and the second row the filter context is 'Black' and so forth. As we are filtering the dataset using RELATED ( 'Product'[Colour] ) = "Red" in our expression, the resulting dataset for these rows is empty and the cell is blank. The value for 'Red Sales' is only seen when 'Red' is already selected in the external filter context.

Red Sales by colour.

When we use the following expression:

Red Sales Calc = 
CALCULATE (
    SUMX (
        Sales,
        Sales[Quantity] * Sales[Net Price]
    ),
    'Product'[Colour] = "Red"
)

When this measure is applied to the table visual in the report, we can see that CALCULATE always computes the same value for each row. This is because in every row, the external filter context of 'Colour' has been replaced by 'Product'[Colour] = "Red" supplied to CALCULATE. So the value returned is always the sales amount of red products.

Red Sales using calculate.

CALCULATE places a filter on the whole model. When you put a filter on a table, this filter propagates through to other tables and is applied to the whole model. So the filter 'Product'[Colour] = "Red" is applied on the 'Colour' column of the 'Product' table and this propagates automatically. Then we are iterating and computing values with SUMX on the 'Sales' table.

Conclusion

So, to summarise CALCULATE computes an expression in a new filter context which is modified by what you provide as a filter. It is the only function in the whole DAX language that has the power of changing the filter context.

FAQs

What is the CALCULATE function in DAX? CALCULATE is the only function in DAX that has the power of changing and updating the filter context. It computes an expression in a new filter context which is modified by what you provide as a filter.
How does CALCULATE differ from using FILTER with SUMX? When using FILTER with SUMX, the external filter context from the report still applies, so filtering for red products only shows values where red is already selected. CALCULATE replaces the external filter context entirely, so the specified filter applies regardless of what is selected in the report.
What is the syntax of the CALCULATE function? The syntax is CALCULATE followed by an expression and then a set of filters. The filters are evaluated first and can change the filter context, then CALCULATE evaluates the expression in the new filter context.
How does CALCULATE propagate filters through the data model? CALCULATE places a filter on the whole model. When you put a filter on a table, this filter propagates through to other tables automatically. For example, filtering the Product table by colour will propagate to the Sales table through their relationship.
What generates the filter context in a Power BI report? The filter context active in each cell of a report is generated by row selection, column selection, slicer selection, and report filters.

Jessica Hill

Software Engineer I

Jessica Hill

Jessica comes from a Biosciences background, having gained a 1st Class Bachelor of Science in Biology from The University of Manchester.

During the lockdown Jessica used the opportunity to explore her interest in technology by taking two Code First Girls online courses; Introduction to Web Development and Python Programming. This led Jessica to look for job opportunities in the technology sector and she joined endjin's 2021 apprenticeship cohort, which had over 200 applicants.

Since joining endjin, Jessica has developed deep expertise in Power BI, DAX, and the Azure data platform. She is a Microsoft Certified Power BI Data Analyst Associate (PL-300) and works across data engineering and analytics projects using technologies such as Microsoft Fabric, Azure Synapse Analytics, Azure Databricks, and Power BI.

Jessica has written extensively about her areas of expertise, with blog posts covering topics from DAX deep dives and Power BI performance optimisation to Azure notebooks and C# pattern matching.