EN VI

DAX Filter Context: Why Your Share of Total Shows 100%

By ··8 min read

Short answer

Filter context is the set of filters that apply to a measure in one cell of a visual, such as the customer on the row or a slicer. A share of total shows 100% in every row because the numerator and the denominator both sit inside the same row filter. CALCULATE changes the filter context, and CALCULATE with ALL removes the row filter so the denominator becomes the grand total.

We have already met measures such as Total Sales, and they seem to work without effort. The surprise comes when we divide one measure by another. On a table with thousands of orders, a "share of total" column can show 100% in every row. It looks like a bug in Power BI, but it is the formula doing exactly what we asked. To see why, we need one idea first, which is filter context.

What filter context means

A measure is not calculated once. Power BI calculates it again for every cell of a visual, and each cell has its own filters. The customer on the row is a filter. A slicer is a filter. A selected month is a filter. Together they are called the filter context. The measure SUM(Orders[total_amount]) only adds the orders that survive those filters.

Here is the sales table by customer. We can calculate every number by hand from the six orders.

CustomerTotal Sales
Ruby Hoang27,000,000
Ethan Vu13,000,000
Kevin Do20,000,000
Sophie Ly18,000,000
Total78,000,000

For the row Ruby Hoang, the filter context holds one filter, which is the customer Ruby Hoang. For the Total row, it holds no filter at all. That is why the same measure gives 27,000,000 in one place and 78,000,000 in another.

The trap: a share of total that is always 100%

To get each customer's share, we divide the customer's sales by the total sales. The first attempt below looks natural, because the denominator is the sum of the whole orders column.

// WRONG: the denominator is filtered by the same customer
Share Wrong = DIVIDE([Total Sales], SUM(Orders[total_amount]))

In the row of Ruby Hoang, SUM(Orders[total_amount]) is also filtered to Ruby Hoang, so it returns 27,000,000. The measure computes 27,000,000 divided by 27,000,000, which is 100%. The same happens in every row. The formula never looks outside its own row.

CALCULATE changes the filter context

To change the filters of a measure, we use CALCULATE. Its first argument is the measure, and the next arguments are the new filters. ALL(Customers) removes every filter that comes from the Customers table. Because the customer filter reaches Orders through the relationship, removing it from Customers also removes it from the orders.

// RIGHT: the denominator ignores the customer filter
Share of Total =
DIVIDE(
    [Total Sales],
    CALCULATE([Total Sales], ALL(Customers))
)
CustomerTotal SalesShare WrongShare of Total
Ruby Hoang27,000,000100%34.6%
Ethan Vu13,000,000100%16.7%
Kevin Do20,000,000100%25.6%
Sophie Ly18,000,000100%23.1%
Total78,000,000100%100.0%

The check is simple. 27 divided by 78 is 34.6%, and the four shares add up to 100%. If your own numbers do not add up, the denominator is still being filtered.

CALCULATE with a column filter

The same function can add a filter instead of removing one. The measure below counts only the orders above 10 million.

Big Orders Sales = CALCULATE([Total Sales], Orders[total_amount] > 10000000)

Four orders pass the filter, which are 15, 12, 20 and 18 million. So Ruby Hoang shows 27,000,000, Kevin Do shows 20,000,000, Sophie Ly shows 18,000,000 and the total is 65,000,000. Ethan Vu has two small orders of 8 and 5 million, so his value is blank. Blank is not zero, and a reader may need that said out loud.

When a slicer is involved

ALL(Customers) removes the customer filter completely, so the denominator stays at 78,000,000 even if a slicer picks two customers. That answers "share of the whole company". If we want "share of the customers selected", we use ALLSELECTED(Customers) instead. It removes the filter from the rows but keeps the selection made outside the visual, such as a slicer. Which one is right depends on the question, so state it in the title of the visual.

A check you can run every time

  1. Calculate one cell by hand from the source table.
  2. Add up the percentages. A share of total must reach 100% across all the rows together. A single row only holds a part of it.
  3. Put the grand total next to the rows and ask which filters each cell has.

Frequently asked questions

What is filter context in DAX?

Filter context is the set of filters that apply to a measure in one cell of a visual. Row and column fields, slicers and page filters all add to it.

What does CALCULATE do in DAX?

CALCULATE evaluates a measure after changing the filter context. You can add filters, replace them or remove them with functions such as ALL.

What is the difference between ALL and ALLSELECTED?

ALL removes the filters on a table or column. ALLSELECTED removes the filters coming from the rows of a visual but keeps the selection made outside it, such as a slicer.

References

Keep learning, step by step.

Each lesson in Learn has a business context, a runnable example and a result check, so you practise this habit on real questions.