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".
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 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
| Kind | Written like | Means |
|---|---|---|
| One condition | Product_Master[Category] = "Fuel" | Only rows where that is true |
| Two or more conditions | filter1, filter2 | Both must be true (AND) |
| A list of values | Station_Master[Region] IN { "North Central", "North West" } | Any of them (OR) |
| Remove a filter | ALL ( 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 <=.
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.
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.
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 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.
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.
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.
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 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.
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.
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).
CALCULATE works on any expression, not only [Total Revenue]. Here it wraps a DISTINCTCOUNT, and the filter uses <>, "is not equal to".
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
- Write Revenue South West and check it against the card above.
- Write Fuel Revenue, South West Fuel Revenue and Northern Revenue.
- Build both tables from section 3 and explain each one out loud to yourself.
- Write All Stations Revenue and put it beside Total Revenue by station.
- Build the three-step South West Share of Fuel and format it as a percentage.
- 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.