Here’s a bold statement: CALCULATE is the most important function in creating DAX measures. While you might disagree with that claim, you’ll find that this function is almost always needed in your DAX expressions once you master it.
The CALCULATE syntax is simple. It lets you construct expressions easily. However, understanding when and why you must use this function can be more challenging.
In this article, we will help you master this popular Power BI CALCULATE function. It covers what this function does, how it works, when to use this function, and more!
Introducing the Popular CALCULATE function in Power BI
Are you familiar with the Excel SUMIF/SUMIFS function? Both are handy for summing up a specific column while filtering out non-matching rows. For example, you can use SUMIF to calculate the total revenue, but only for rows in the sales table where the Year column is 2023.
CALCULATE is similar to SUMIF/SUMIFS functions but more powerful.
It lets you take any aggregate function, not just limited to COUNT, SUM, or AVERAGE. You might want something like MAXIF, MINIF, or STDEVIF in Excel. With CALCULATE, you can make a version of these functions quickly.
Moreover, you can use CALCULATE in pivot tables as part of a measure. While with SUMIF, you can’t.
What is CALCULATE function in Power BI?
The Power BI CALCULATE function is categorized as a filter function in Power BI DAX. It allows you to apply filtering within a specific context.
You may be wondering… What’s the difference between using CALCULATE and a slicer in Power BI?
We’ll go into more detail about what the CALCULATE function does and why you MUST use this function in the upcoming section, helping you to better understand and grasp its purpose.
What does CALCULATE do in Power BI?
The CALCULATE function evaluates an expression in a modified filter context.
To make it easier to understand, let’s have a look at an example to show how CALCULATE can help you.
Note: We use Microsoft’s Contoso Sales Sample file for Power BI. If you want, you can download the file and open it using Power BI Desktop. We assume you have the basics of Power BI and DAX.
Let’s say you’ve created a table showing the total sales by product category as follows:
Then, for each product category, you want to show the total sales for 2011. You aim to compare this with the total sales from all the years and want this information displayed next to the Total Sales column. Moreover, you want the total sales for 2011 not to be affected by a slicer or any filter being used by the visual:
In this case, you need to apply different filter contexts for different measures in the same visual. This is where CALCULATE can help you.
Power BI DAX, CALCULATE, and data model: The relationship
As we’ve discussed before, CALCULATE is a Power BI DAX function. DAX, or Data Analysis Expressions, is a formula language used in Power BI to query the data model and perform calculations.
Through DAX, Power BI allows dynamic filtering within specific calculations. This unique filtering capability gives Power BI an edge over other BI tools, such as Tableau.
It’s also important to note that Power BI has a powerful data model, which is designed to work with multi-system linkage, allowing you to pull and analyze data from multiple systems or sources effectively.
Optionally, you can use data automation tools like Coupler.io to help you bring data into Power BI easily. This is often more reliable than the built-in methods and it offers features like automatic data updates on the schedule you want. With Coupler.io, you can pull data from 50+ apps like GA4, MailChimp, Google Ads, and many more.
Power BI CALCULATE syntax
Here is the DAX syntax for Power BI CALCULATE:
CALCULATE(<expression>[, <filter1> [, <filter2> [, ...]]])
The DAX CALCULATE function has two parameters or arguments:
<expression>is what you want to evaluate. This can be a DAX expression that is constructed using an aggregate function or a measure that defines an expression.
<filter>: These are the conditions you want to apply to your data. You can include as many filters as needed.
=CALCULATE([Total Sales],'Calendar'[Year]=2011,Geography[RegionCountryName]="United States")
More on the filter parameter:
- You can also use <, >, <=, >=, and <> operators in the
- CALCULATE accepts multiple filters that are combined in an AND logical statement. If you require an OR statement, you can use the “||” operator or the “OR()” style. Example:
=CALCULATE([Total Sales], 'Calendar'[Year]=2011 || 'Calendar'[Year]=2012)
=CALCULATE([Total Sales], OR('Calendar'[Year]=2011,'Calendar'[Year]=2012))
How to CALCULATE in Power BI: Step by step
To use CALCULATE in Power BI, first open Power BI Desktop.
Then, on the Data pane on the right, select the table where you want to add a new measure. Right-click on the table, and select New measure. In our case, we add it to the Sales table:
On the formula bar, type a measure name and specify the formula. For example, here’s a simple CALCULATE function using SUM to find the total sales for 2011:
2011 Sales = CALCULATE(SUM(Sales[SalesAmount]), 'Calendar'[Year]=2011)
If you don’t like the long syntax, you can first define a Total Sales measure with this formula:
SUM(Sales[SalesAmount]). Thus, your 2011 Sales measure would be:
2011 Sales = CALCULATE([Total Sales], 'Calendar'[Year]=2011)
Next, you can change how your measure looks in the Measure tools tab. For instance, you can display it to use currency with 2 decimal places, as shown below:
Power BI CALCULATE function in action
We’ll use the formula we just created in a visualization.
For example, we create a table displaying product categories and two measures: Total Sales and our new 2011 Sales measure, which is always filtered for 2011.
Here are three key points about the formula:
- We use a measure named [Total Sales] in the
<expression>argument. You can also use any formula that could define a measure, like
- For the
<filter>argument, we don’t put 2011 in quotes because the Year column is in a number data type. If it were text, we’d have to write =
- We only use one
<filter>in this example, but you can use as many as you need in one CALCULATE formula.
How the CALCULATE Formula in Power BI works
Now that we’ve used CALCULATE in a measure and displayed it in a table, let’s examine how this function works.
Let’s say you add a slicer that filters the table for the year 2013. The CALCULATE formula in Power BI will override this existing filter and show the sales for 2011 instead.
This is because the
<filter> arguments in CALCULATE are evaluated during the “filter” phase of measure calculation. They change the filter context given by the visual. This happens before these filters are applied to the source tables and before the calculation phase starts.
In addition, we can see that the filter in the CALCULATE formula Power BI adds a new layer to the filter context. The new filter (Year = 2011) is added on top of the existing one (ProductCategory). The result will be the intersection of these two filters, showing sales for each product category in the year 2011.
In conclusion, the CALCULATE function in Power BI helps you manipulate the context of your data calculations, giving you more control over your data analysis.
Some useful Power BI CALCULATE examples
We’ve been using the [2011 Sales] measure as a sample to demonstrate how CALCULATE works. However, it might not look very useful.
So, let’s move on to other use cases that are more applicable.
How to use CALCULATE in Power BI to compare sales based on store types
In our Consoso Sales data, we have four different store types: Catalog, Online, Reseller, and Store.
You may want to compare your Online Sales with other types. To do this, you can create two measures with the following formulas:
Online Sales = CALCULATE([Total Sales], Stores[StoreType] = "Online") Non-online Sales = CALCULATE([Total Sales], Stores[StoreType] <> "Online")
Staying on the practical path, let’s check the percentage of our total sales that are made up by online sales.
Pct Online Sales = [Online Sales] / [Total Sales]
How to use CALCULATE in Power BI to calculate % growth
In this example, we’re going to calculate the % growth of sales from the year 2011.
To do this, we need to use our existing measure that always tells us how many sales were made in 2011.
2011 Sales = CALCULATE([Total Sales], 'Calendar'[Year]=2011)
Also, a measure to get cumulative sales since 2011:
Cumulative Sales = CALCULATE([Total Sales], FILTER( ALLSELECTED('Calendar'[Year]), 'Calendar'[Year] <= MAX('Calendar'[Year])))
Then, we’ll add a measure that tells us the % growth in sales since 2011:
Pct Growth = DIVIDE([Cumulative Sales]-[2011 Sales],[2011 Sales])
When to use CALCULATE in Power BI
We’ve gone through a few examples, which should make it easier for you to understand when it’s beneficial to use the CALCULATE function in Power BI. Here are some of them:
- To change context: CALCULATE formula Power BI allows you to modify the context of a calculation. This is especially useful when you need to perform calculations that aren’t based on the existing filters or conditions in your report.
- Apply specific filters: If you want to apply specific filters to a measure, CALCULATE can do this. For example, if you want to know the total sales only for a specific region or product category.
- Percentage of total: An example is when you want to calculate the % contribution of a particular segment to the total.
- Comparison: An example is when you want to compare data across different categories, like comparing sales based on online sales vs non-online sales.
- Time-based calculations: We’ve seen an example of using CALCULATE formula in Power BI to get cumulative sales. You can also use this function for other time-based calculations, such as Year-To-Date (YTD) or Month-To-Date (MTD) totals.
We hope that this tutorial has helped you in learning Power BI CALCULATE, especially regarding why and when you should use this function.
Finally, if you need help with building effective dashboards in Power BI, Coupler.io is always ready to help. As a data analysis consulting service, we offer practical solutions to your needs. We can also guide you on how to best apply functions like CALCULATE.
To learn more about Power BI functions, check our article on SUMX Power BI.
Streamline your data analytics & reporting with Coupler.io!
Coupler.io is an all-in-one data analytics and automation platform designed to close the gap between getting data and using its full potential. Gather, transform, understand, and act on data to make better decisions and drive your business forward!
- Save hours of your time on data analytics by integrating business applications with data warehouses, data visualization tools, or spreadsheets. Enjoy 200+ available integrations!
- Preview, transform, and filter your data before sending it to the destination. Get excited about how easy data analytics can be.
- Access data that is always up to date by enabling refreshing data on a schedule as often as every 15 minutes.
- Visualize your data by loading it to BI tools or exporting it directly to Looker Studio. Making data-driven decisions has never been easier.
- Easily track and improve your business metrics by creating live dashboards on your own or with the help of our experts.
Try Coupler.io today at no cost with a 14-day free trial (no credit card required), and join 700,000+ happy users to accelerate growth with data-driven decisions.Start 14-day free trial