Search Our Knowledge Base…

Search Our Knowledge Base…

DAX Functions

How to Calculate Function in Power BI: Step-by-step

How to Calculate Function in Power BI: Step-by-step

Written By: Sajagan Thirugnanam and Austin Levine

Last Updated on October 7, 2024

The Calculate Function is one of the most important and commonly used DAX functions in Power BI. I have been working with Power BI as my main visualization tool across several industries as a Data Analyst and have used the Calculate function extensively during this tenure.

This function opens up many opportunities and flexibility for data analysis and allows us to develop functional Power BI reports and dashboards.

In this article, I will demonstrate how to use the Calculate Function and how the function can be used in real-world applications.

What is Power BI Calculate Function?

Let's take a look at what the Calculate function is and how it works. 

The easiest way to envision the Calculate function is by comparing it to the Excel SUMIF function where it can sum up specific columns while filtering out rows which do not meet the matching criterion. 

An Example of How Calculate Function Works

For instance, the Calculate function lets us calculate the total profit for our business for only, let’s say, a specific city where our business operates. Similarly, it lets us calculate the total revenue for a certain product. This function opens all kinds of new opportunities in terms of analysis and mathematical operations. We will go over some scenarios where the Calculate function can come in handy but before that let’s take a look at the syntax and how it works. 

Basic Syntax of the Calculate Function

The Calculate function in Power BI has main parts to it: the expression and the filter. The expression allows us to place different aggregations such as Sum, AVG, Count etc. The filter allows us to define the column on which we are filtering the data and the filter value. 

The DAX syntax for the calculate function in Power BI is as follows: 

CALCULATE(<expression>[, <filter1> [, <filter2> [, …]]])

It is possible to add multiple filters to the filter section in the function by separating via commas. 

How to use the Calculate Function in Power BI?

To use the Calculate function, first we have to select the table in which we want to create the new measure - this new measure will appear as a dimension/metric in the table in which we create the calculation. Right click on the table and select New measure.

On the formula bar, type the measure name followed by the Calculate function. Here is a basic example of how the Calculate function can look like:

We can also format the measure using the formatting options in the Measure tools ribbon panel. The measure created will now appear in the dataset on the right side Data panel.

When to use the Calculate Function 

I tend to use the Calculate function primarily to modify or manipulate the filter context of my data to perform specific calculations or dynamically filter the data. Let us walk through an example to help simplify this. 

Suppose we have a table named “Sales” which has the following three columns: Product Type, Sales Amount and Date. Let us calculate the total sales revenue for the Electronics product type in 2023.

To do this, we have to add the filter context for the measure which will tell the function how to filter the data. The DAX function will look like this-

Electronics_Sales_2023 = 

CALCULATE(

    SUM(Sales[Sales Amount]), 

    Sales[Product Type] = "Electronics",

    YEAR(Sales[Date]) = 2023)

Here the SUM(Sales[Sales Amount]) calculates the sum of the Sales Amount column and Sales[Product Type] = "Electronics" sets the filter to include only rows where the product type is Electronics. Finally, the YEAR(Sales[Date]) = 2023 indicates that we only want to include data for the year 2023.

This way we can use the Calculate function to dynamically calculate the total sales for Electronics product type in 2023. While Excel can do the same thing with the SUMIF function, in Power BI, the best practice is to use the Calculate function to set filter context for each measure to perform such specific calculations.

Another Real World Use Case of the Calculate Function 

Let's walk through another example of how we can use the calculate function to help in our daily reporting and analysis. Here we want to calculate the overall revenue from City A where customers are above the age of 45

The DAX will look like this:  

Total Revenue

CALCULATE

(

    SUM(Sales[Revenue]),

    Customer[City] = "City A",

    Customer[Age] > 45

)

This function allows us to calculate the total revenue of customers over the age of 45 in City A. We can furthermore incorporate the divide function to calculate % growth and year-over-year change.

There are many other ways in which we can use this function to help in our reporting and analysis. For a further in-depth look at how the function works, follow this link and go through the Microsoft guidelines on how to use the Calculate function. 

Closing Remarks

The Calculate function in Power BI is a versatile tool that allows users to perform sophisticated data analysis and generate actionable insights from complex datasets. By mastering the  Calculate function, we can unlock the full potential of our data and drive informed decision-making in a dynamic business environment.

Keep in mind, there are many other ways in which we can utilize this function in addition to what we have discussed here in this blog. I hope this post has helped you understand the basics of this function and causes the spark in you to explore further!

FAQs

Does CALCULATE affect the entire report or only specific measures?

CALCULATE affects only the measures or expressions within its scope. It does not affect other measures or visuals in the report unless those measures.

Can I nest Calculate Function?

Yes, it is possible to nest the Calculate function to modify the filter context even further. However, this may result in a complex DAX function which may be difficult to read and maintain.

Can I use multiple filter arguments with the Calculate Function?

Yes this is certainly possible as I have shown in the examples above. 

Does Calculate support logical operators like AND, OR, NOT?

Yes we can use logical operators like AND, OR, NOT to create complex filter conditions within the Calculate function. These operators are useful to combine multiple filter conditions to further customize the filter context.