Courses Learn Blog Contact
Get Started →
Lesson 13

Day 10: CALCULATE

The most important function in DAX: changing the filter a measure is calculated under.

Webbo3 Power BI Cohort 1 11 min read Free

Webbo3 Power BI · Cohort 1 · Day 10 of 16

Day 10: CALCULATE

The most important function in DAX: working out a measure under a filter you choose, instead of only the one the reader clicked.

By the end of today you can

  • Say what CALCULATE does in one sentence
  • Write a measure with one filter, with two filters, and with a list of values
  • Predict what a CALCULATE measure shows on each row of a table visual
  • Remove filters with ALL and REMOVEFILTERS
  • Avoid the three CALCULATE mistakes every beginner makes

On Day 8 a measure followed the filters: pick Lekki and Total Revenue showed Lekki. Today you learn to set the filter yourself, inside the measure.

Open your Day 9 file. You need the four relationships and the Total Revenue measure; every example today builds on [Total Revenue].

1. What CALCULATE Is

Definition

CALCULATE works out a measure after changing the filters. You give it the measure first, then one or more filters. It applies your filters, keeps the reader's filters on everything else, and then calculates.

Real-Life Example

You ask the accountant for "total revenue" and she adds up whatever pile is on her desk. With CALCULATE you hand her a note with the question: "total revenue, but only South West stations". She does not need you to move the piles first. The note tells her which pile to use.

How It Is Written

The first argument is what to calculate, usually a measure you already have. Every argument after it is a filter. The simplest filter is a column compared with a value: Station_Master[Region] = "South West".

What this code doesWe want revenue for the three South West stations only (Ikeja, Lekki and Bodija), whatever the reader has or has not clicked.
Measure
1
2
3
4
5
6
// Pattern: CALCULATE ( what to calculate, filter, filter, ... )
Revenue South West =
CALCULATE (
    [Total Revenue],                  // what to calculate
    Station_Master[Region] = "South West"  // the filter to apply first
)
Two cards, no slicers
₦62.1M
Revenue South West
₦62,094,472
₦145.9M
Total Revenue
₦145,854,226

South West is 43% of all revenue. The new measure did not need a slicer or a filter pane: the filter lives inside it.

Picture This

Picture the note clipped to the question: "total revenue, but only South West". Every time the measure is asked, the note goes with it.

2. The Kinds of Filter You Can Give It

Types

KindWritten likeMeans
One conditionProduct_Master[Category] = "Fuel"Only rows where that is true
Two or more conditionsfilter1, filter2Both must be true (AND)
A list of valuesStation_Master[Region] IN { "North Central", "North West" }Any of them (OR)
Remove a filterALL ( Station_Master ) or REMOVEFILTERS ( Station_Master )Ignore what the reader clicked on that table

Type 1: One Condition

A column, a comparison and a value. Text goes in double quotes, numbers do not. The comparisons are the ones you know from Excel: =, <> (not equal), >, <, >= and <=.

What this code doesWe want revenue from fuel only (petrol, diesel and kerosene), leaving out gas and engine oil.
Measure
1
2
3
4
5
6
// Only rows whose product is in the Fuel category
Fuel Revenue =
CALCULATE (
    [Total Revenue],
    Product_Master[Category] = "Fuel"
)
Card visual
₦139.3M
Fuel Revenue
₦139,263,155
95.5%
of Total Revenue

The filter is on the Products table, not on Sales. It still works because Products filters Sales through the relationship you built on Day 7. CALCULATE filters the model, and the model does the rest.

Type 2: Two Conditions (AND)

Separate the filters with commas. A row has to pass every one of them.

What this code doesWe want fuel revenue at South West stations only, two conditions that must both hold.
Measure
1
2
3
4
5
6
7
// Both must be true: South West station AND a fuel product
South West Fuel Revenue =
CALCULATE (
    [Total Revenue],
    Station_Master[Region] = "South West",   // condition 1
    Product_Master[Category] = "Fuel"        // condition 2
)
Card visual
₦59.3M
South West Fuel Revenue
₦59,291,440

Type 3: A List of Values (OR)

IN followed by a list in curly brackets means "any of these". It is the tidy way to say "North Central or North West" on one column.

What this code doesWe want revenue for the two northern regions together: Wuse in Abuja and Sabon Gari in Kano.
Measure
1
2
3
4
5
6
// Either region counts
Northern Revenue =
CALCULATE (
    [Total Revenue],
    Station_Master[Region] IN { "North Central", "North West" }
)
Card visual
₦41.0M
Northern Revenue
₦40,954,329

Type 4: Removing a Filter with ALL

ALL ( Station_Master ) used as a filter means "ignore every filter on the Station_Master table". REMOVEFILTERS ( Station_Master ) does exactly the same and says so more plainly; you will see both in other people's reports. Section 3 shows why you would want this.

Picture This

Picture four kinds of note: "only this", "only this and that", "any of these", and "ignore what they pointed at".

3. What CALCULATE Does to Each Row of a Visual

The Rule

This is the part that confuses everybody, so read it twice. A table visual filters each of its rows. CALCULATE then applies its own filters, and:

  • If CALCULATE filters the same column the row is filtering, CALCULATE replaces the row's filter with its own.
  • If CALCULATE filters a different column, both filters apply, and only rows that pass both are counted.

Real-Life Example

The accountant has a pile on her desk labelled "North Central". Your note says "only South West". Same label, so she swaps the pile. Now the pile is labelled "Wuse Filling Station" and your note still says "only South West". Different labels, so she keeps the pile and checks it against the note: Wuse is not South West, so the answer is nothing.

What this visual showsWe want a table with Station_Master[Region] on the rows and both measures side by side. Before you look, predict what Revenue South West shows on the North Central row.
Table visual: rows are Region
RegionTotal RevenueRevenue South West
South West₦62,094,472₦62,094,472
South South₦21,582,980₦62,094,472
South East₦21,222,445₦62,094,472
North Central₦20,591,110₦62,094,472
North West₦20,363,218₦62,094,472
Total₦145,854,226₦62,094,472

Every row says ₦62,094,472. The row filter was on Station_Master[Region] and so was CALCULATE's, so CALCULATE replaced it. The measure is doing exactly what it was told, and it does not answer "revenue per region". Total Revenue does that.

What this visual showsWe want the same measures with Station_Master[StationName] on the rows instead. The row filter is now on a different column from CALCULATE's filter.
Table visual: rows are Station
StationRegionTotal RevenueRevenue South West
GRA Filling StationSouth South₦21,582,980
Bodija Filling StationSouth West₦21,391,823₦21,391,823
Independence Layout Filling StationSouth East₦21,222,445
Wuse Filling StationNorth Central₦20,591,110
Ikeja Filling StationSouth West₦20,455,308₦20,455,308
Sabon Gari Filling StationNorth West₦20,363,218
Lekki Filling StationSouth West₦20,247,342₦20,247,342
Total₦145,854,226₦62,094,472

Now both filters apply. Ikeja, Lekki and Bodija are in the South West, so they pass both and show their own revenue. The other four fail CALCULATE's filter and show blank (Power BI shows an empty cell). The total is South West's total.

What this code doesWe want a measure that always shows revenue for every station, even on a row that is filtered to one station.
Measure
1
2
3
4
5
6
// Ignore every filter on the Station_Master table, then add up
All Stations Revenue =
CALCULATE (
    [Total Revenue],
    ALL ( Station_Master )
)
Table visual: rows are Station
StationTotal RevenueAll Stations Revenue
GRA Filling Station₦21,582,980₦145,854,226
Bodija Filling Station₦21,391,823₦145,854,226
Independence Layout Filling Station₦21,222,445₦145,854,226
Wuse Filling Station₦20,591,110₦145,854,226
Ikeja Filling Station₦20,455,308₦145,854,226
Sabon Gari Filling Station₦20,363,218₦145,854,226
Lekki Filling Station₦20,247,342₦145,854,226
Total₦145,854,226₦145,854,226

Every row shows the grand total, because ALL removed the row's station filter. On its own that looks useless. Divide one by the other and you get each station's share of the business, which is the first thing you build in tomorrow's lab.

Picture This

Picture the accountant swapping a pile when the label matches the note, checking the pile against the note when it does not, and pushing every pile together when the note says ALL.

4. Common Mistakes

Mistake 1: A Typo in the Value

DAX does not check that "Southwest" is a real region. It filters to rows where the region is "Southwest", finds none, and shows blank. No error, just an empty card. When a CALCULATE measure is blank, copy the value straight from the column in Table view.

Capital letters do not matter: "south west" matches "South West", because DAX compares text without caring about case. Spaces and hyphens do matter.

Mistake 2: Filtering on a Measure

A simple filter must compare a column with a value. [Total Revenue] > 1000000 is a measure, so Power BI refuses the formula and underlines it in red. Filtering by a measure needs the FILTER function, which you will not need in this course.

Mistake 3: Expecting a Per-Row Answer from a Replaced Filter

Section 3's first table. If a CALCULATE measure shows the same number on every row, check whether its filter is on the same column as the rows. That is almost always the reason.

The Quick Test

When a CALCULATE measure surprises you, put it in a table beside [Total Revenue] with the column it filters on the rows. The two columns side by side tell you which rule is happening.

5. Putting It Together

A manager asks: "How much of our fuel revenue comes from the South West, as a share of all fuel?" Three measures you now know answer it, each building on the one before.

What this code doesWe want the South West's share of all fuel revenue, built from small measures that each do one job.
Three measures
1
2
3
4
5
6
7
8
9
10
11
// Step 1: fuel revenue, all regions
Fuel Revenue =
CALCULATE ( [Total Revenue], Product_Master[Category] = "Fuel" )

// Step 2: fuel revenue, South West only
South West Fuel Revenue =
CALCULATE ( [Fuel Revenue], Station_Master[Region] = "South West" )

// Step 3: one divided by the other, safely
South West Share of Fuel =
DIVIDE ( [South West Fuel Revenue], [Fuel Revenue] )
Three cards
₦139.3M
Fuel Revenue
₦59.3M
South West Fuel Revenue
42.6%
South West Share of Fuel

Step 2 uses CALCULATE on a measure that already contains CALCULATE. That is normal: the Fuel filter comes with [Fuel Revenue], and the Region filter is added on top. Format the share as a percentage (Measure tools, Format, Percentage).

What this code doesWe want Day 8's Attendants Selling again, counting only real people: every name except Unmanned.
Measure
1
2
3
4
5
6
// Day 8's count, with the unmanned sales left out
People Selling =
CALCULATE (
    DISTINCTCOUNT ( Sales[AttendantName] ),
    Sales[AttendantName] <> "Unmanned"
)
Two cards
17
People Selling
18
Attendants Selling (Day 8)

CALCULATE works on any expression, not only [Total Revenue]. Here it wraps a DISTINCTCOUNT, and the filter uses <>, "is not equal to".

Try This

Write Unmanned Revenue: revenue from sales with no attendant, where Sales[AttendantName] is "Unmanned". Then divide it by [Total Revenue] to get its share. How much of the business runs without anyone at the pump?

Expected: the card shows ₦14,372,026, which is 9.9% of all revenue.

Do This Now

  1. Write Revenue South West and check it against the card above.
  2. Write Fuel Revenue, South West Fuel Revenue and Northern Revenue.
  3. Build both tables from section 3 and explain each one out loud to yourself.
  4. Write All Stations Revenue and put it beside Total Revenue by station.
  5. Build the three-step South West Share of Fuel and format it as a percentage.
  6. Do the Try This.

Checkpoint

Q1. In one sentence, what does CALCULATE do?

It works out a measure after applying the filters you give it, on top of the filters already there.

Q2. A CALCULATE measure filtered to Region = "South West" shows the same number on every row of a table whose rows are Region. Is it broken?

No. Its filter is on the same column as the rows, so it replaces each row's filter. It is doing what it was told.

Q3. The same measure shows blank on the Wuse Filling Station row. Why?

The row is filtered to Wuse and CALCULATE adds South West. Both apply, and Wuse is not in the South West, so no rows pass.

Q4. How do you write "Cash or Transfer" as one filter?

With IN and a list: Sales[Payment Method] IN { "Cash", "Transfer" }.

Q5. What does ALL ( Station_Master ) do inside CALCULATE?

It removes every filter on the Station_Master table, so the measure counts every station whatever the reader selected.

Tick Before You Move On

  • ☐ Revenue South West shows ₦62,094,472
  • ☐ Fuel Revenue shows ₦139,263,155
  • ☐ I can predict what a CALCULATE measure shows on each row of a table
  • ☐ I know that a typo in a value gives blank, not an error
  • ☐ South West Share of Fuel shows 42.6%

Quick Recap

CALCULATE works out a measure under filters you choose. Commas mean AND; IN with a list means OR on one column. A filter on the same column as the row replaces it; a filter on another column narrows it. ALL and REMOVEFILTERS remove filters, which is how shares of a total are built. Blank usually means a value typed wrong; the same number everywhere usually means a replaced filter.

Final Scenario

The managing director opens the report and asks why the South West column says the same thing on every region.

You do not panic. You say: "That measure is South West only, on purpose. For each region's own figure read Total Revenue beside it." Then you show her the share: the South West earns 43% of everything, and 43% of the fuel.

Knowing why a number is what it is makes you the person in the room they trust.

Next, Day 11 Lab: CALCULATE together. Share of total, year against year, and the measures a manager actually asks for. The Solo Project is released.

Learn this with a real instructor

Free lessons take you a long way on your own. The full programmes add live classes, assignments, Skill Points and a certificate — built for African students.