How to Use CALCULATE in Power BI
Great Uses for CALCULATE in Power BI
Calculate is one of the most versatile functions in Power BI. When you begin using anything from simple filters, time intelligence functions or even advanced formulas, often the CALCULATE formulas are leveraged to produce the desired outcome.
Let ’s use CALCULATE to filer a column in a table. CALCULATE can be used for single filter conditions or multiple filter conditions. Let’s explore the function’s syntax. If you are familiar with Tableau this equivalent would be the level of detail functions.
How to Use Calculate
Quick Rules for CALCULATE.
The filter section of calculate can only be:
- a number
- a date
In the formula, there are two basic arguments. The expression and the filter. An expression can be anything from an aggregation or calculated measure. For these purposes, we are going to use the SUM and Average functions with our CALCULATE function. However, please remember you are not limited to this aggregation.
CALCULATE(SUM(Table[Column],Filter1) CALCULATE(SUM(Table[Column],Filter1) CALCULATE(AVERAGE(Website[Sessions]),Website[type]="Organic Search")
The benefit of using CALCULATE to filter is using your newly created calculated measures add a layer of context and complexity to your visualizations. For example, overlaying the amount of website organic search visits to the total am your visuals. This can be achieved with the intricate filter on your visualization pane, however, it a be done rather quickly using these CALCULATE measures. Usually, when your data is a column field, you are restricted to the stacked graphs and filter to isolate the dimension of choice. The CALCULATE function can help circumvent this.
Use Calculate with Two or More filters.
Sometimes your data may need more to be filtered by more than one conditions to provide you with the segment that you are interested in. Imagine you have the challenge of fiding out the number of sessions for your organic search visits from females. This can be achieved by adding an additional filter to your CALCULATE function.
To use CALCULATE with multiple filters you can just simply add another filter condition.:
CALCULATE(AVERAGE(Website[Sessions]),Website[type]="Organic Search", Website[gender]=’female’)
You can have as many conditions as you want by adding more conditional filters. We will explore other conditions using some of the time intelligence function in Power BI.
Use Calculate to with Time Intelligence Functions
Calculate is a great function to compare with time intelligence functions such as SAMEPERIODLASTYEAR, PREVIOUS MONTH, PREVIOUSDAY.
For me, this is one of the best uses of CALCULATE to evaluate time series data. Let’s look at the example below which will compare prices this year versus last year.
** A lot of time intelligence will calculate will not function if you don’t have continuous dates in your table.
The formula we are using is below:
You can also achieve the same result using the YEAR function :
You can even go one further and compare this year price vs last year for just organic by adding an additional filter. Here is This year vs Last Year sum of total website sessions.
How CALCULATE works with ALL and FILTER,
It’s very important to understand how CALCULATE filters the data that you are adding to the function. So we will export how the functions ALL and FILTER can change the results of your data.
These functions will help you achieve different objectives. A lot of the time when dealing with table functions, simply using SUM will not give you the total of all dimensions. It will give you the total or calculation by dimension. ALL allows you to take the total or calculation across all dimensions. In other words, a true total.
The benefit of the ALL function is that it will allow you to use this in calculations to get percentages across different dimensions.
ALL Calculate = CALCULATE(SUM(Website[Sessions]),ALL(Website[ Channels))
Filter with Calculate allows you to isolate the calculation to that one dimension. In this example, we just want the total for the single dimension that we specified.
CALCULATE(SUM(Website[Sessions]),FILTER(Website,Website[ Channels]="Organic Search"))
Benefits of Using FILTER with CALCULATE
- Can you measure in the Filter section
- Create OR statements
You cannot use measures in the filter section of CALCULTATE
Imagine if you wanted to get the sum of session where you have more than 4 channels in one day.
First you create a measure
Count of Channels = COUNT('Web Site Data',[Default Channels)
Now if you want to use this to see how many sessions occurred with default channels more than 4.
Session with More than 4 Channels = CALCULATE(SUM(Website[Sessions]),[Count of Channels ]>4))
You will get an error:
A function ‘CALCULATE’ has been used in a True/False expression that is used as a table filter expression. This is not allowed.
To overcome this error, you can use FILTER
CALCULATE(SUM(Website[Sessions]),FILTER(Website,[Count of Channels ]>4))
Using OR with CALCULATE
When you add two filters to the CALCULATE Function, this is equavalient to using AND. So if you wanted to get the Direct or Referral Channels. You will need to use the || to create OR in CALCULATE and FILTER
Direct OR Refferal Sessions = CALCULATE(SUM('All Web Site Data'[Sessions]), FILTER('All Web Site Data','All Web Site Data'[Default Channel Grouping]="Direct" || 'All Web Site Data'[Default Channel Grouping]="Referral"))
When you want to use the OR condition, Use FITLER with CALCULATE.
Check out THE MOST USEFUL DAX FUNCTIONS
More Time Intelligence Functions with CALCULATE
You can learn how to compare year over year and month to date functions easily. Check out the videos below to learn how to utilize time intelligence function with CALCULATE.
USE CALCULATE to Total MTD, YTD, QTD.
Check out the article on Total MTD, QTD, YTD
Use CALCUATE for Year over Year and Month over Month Comparison