Sub topic 8
Putting it together: percent of total and percent of group
This chapter assumes you can write basic DAX measures with SUM and COUNTROWS. It also assumes you know how slicers and visual rows filter a model, and that you have read the earlier chapters on CALCULATE, ALL, REMOVEFILTERS, and ALLEXCEPT. Here you combine them into one of the most common report requirements: showing each item as a share of a larger whole.
The idea behind every share measure
A share, also called a percentage of total, is a ratio of two numbers:
- The numerator is the value for the current cell of the visual. It uses the filter context as it is.
- The denominator is the value for a larger group. It uses a modified filter context in which some filters have been cleared.
The only thing that changes between a percent of total and a percent of group is which filters the denominator clears. Everything else stays the same.
The base measure
All the examples use this measure. It sums the sales amount and responds to whatever filters the visual applies.
Total Sales = SUM ( Sales[Amount] )Suppose a matrix has one row per product, and the Product table has columns such as Product[Product Name], Product[Category], and Product[Color]. In the row for one product, Total Sales returns the sales of that product only. The product row filters the Product table, and the relationship carries that filter to Sales.
Percent of all products
To get each product's share of the grand total, the denominator must ignore every filter that comes from the Product table. REMOVEFILTERS does that inside CALCULATE.
Sales % of All Products =
DIVIDE (
[Total Sales],
CALCULATE ( [Total Sales], REMOVEFILTERS ( 'Product' ) )
)Walk through it for the row "Contoso Laptop":
- The numerator evaluates
[Total Sales]with the product filter active. It returns the laptop's sales. - The denominator runs
CALCULATE, which first removes all filters onProduct. The laptop row no longer filters anything, so[Total Sales]returns the sales of every product. DIVIDEreturns numerator divided by denominator.
DIVIDE returns blank when the denominator is zero or blank, so you do not get a division error. A plain / operator would raise an error in that case.
What the denominator still respects
REMOVEFILTERS ( 'Product' ) clears filters on the Product table only. Filters on other tables stay in place. If a slicer selects the year 2025 from a Date table, the denominator is the total of all products for 2025. This is usually what people want: the share of products within the selected period.
That behavior depends on the model. The Date filter survives because Date is a separate table from Product. If a slicer filtered a column in Product itself, such as Product[Color], the denominator would ignore it too. The percentage would then be computed against all products of every color, not only the selected colors. If you want the slicer to count, see the section on ALLSELECTED in the last chapter.
Percent of category (percent of group)
Now suppose the matrix groups products under their category, and you want each product's share of its own category. The denominator must clear the filter on the product but keep the filter on the category. ALLEXCEPT does exactly this.
Sales % of Category =
DIVIDE (
[Total Sales],
CALCULATE ( [Total Sales], ALLEXCEPT ( 'Product', 'Product'[Category] ) )
)ALLEXCEPT ( 'Product', 'Product'[Category] ) removes every filter on the Product table except the one on Category. In the row for "Contoso Laptop", the context contains a filter on Product Name and a filter on Category (because the matrix has the category above the product, and the product's row implies its category). The denominator drops the Product Name filter and keeps Category. The result is the sales of the whole Laptops category.
Here is a sample result.
| Category | Product | Total Sales | Sales % of All Products | Sales % of Category |
|---|---|---|---|---|
| Laptops | Contoso Laptop | 600 | 30% | 75% |
| Laptops | Fabrikam Laptop | 200 | 10% | 25% |
| Phones | Contoso Phone | 900 | 45% | 90% |
| Phones | Adventure Phone | 100 | 5% | 10% |
| Accessories | Contoso Mouse | 200 | 10% | 100% |
The grand total is 2,000. Laptops total 800, phones total 1,000, and accessories total 200. Each column of percentages follows from those numbers: 600 / 2,000 = 30%, and 600 / 800 = 75%.
Subtotal rows
Look at what these measures return in the category subtotal row. In the "Laptops" subtotal, the filter context holds only the category. For Sales % of Category, the denominator is the sales of Laptops, which equals the numerator, so the subtotal shows 100%. For Sales % of All Products, the denominator is 2,000, so the Laptops subtotal shows 40%. Both results are correct, and both are useful. The category subtotal in the first measure tells you how big the category is. The category subtotal in the second measure is always 100% by construction.
At the grand total row, both measures show 100%, because no filter exists to remove.
Two ways to write the same group share
You can get the same group share by clearing the product-level column and leaving everything else alone. When only one column needs to be cleared, you can pass that column to REMOVEFILTERS.
Sales % of Category (columns) =
DIVIDE (
[Total Sales],
CALCULATE ( [Total Sales], REMOVEFILTERS ( 'Product'[Product Name] ) )
)The two measures return the same numbers in the matrix above. They differ in what they promise.
| Version | What it clears | What it keeps |
|---|---|---|
| `ALLEXCEPT ( 'Product', 'Product'[Category] )` | Every filter on `Product` except `Category` | `Category` only |
| `REMOVEFILTERS ( 'Product'[Product Name] )` | The filter on `Product Name` only | Every other filter, including `Color` and `Category` |
Suppose a user adds a slicer on Product[Color] and selects "Black". With ALLEXCEPT, the color filter is cleared in the denominator, so each product is compared to the whole category regardless of color. With REMOVEFILTERS on Product Name, the color filter stays, so each product is compared to the black products in the category. Neither is wrong. They answer different questions, and you should pick the one that matches the report's meaning.
Why `ALLEXCEPT` is sensitive to the grouping
ALLEXCEPT keeps the filters that exist on the columns you list. It does not add them. If the visual does not group by Category, no filter on Category exists, so the "except" list keeps nothing and the denominator is the grand total.
You can see this by putting only Product Name in the rows, without Category. In the row for "Contoso Laptop", the filter context on Product holds a filter on Product Name only, because the matrix does not group by category. Then ALLEXCEPT ( 'Product', 'Product'[Category] ) clears that name filter and finds no category filter to keep. The measure returns the share of all products, not the share of the category.
A subtle point: a filter on the kept column from the product row
In a matrix with only the product in rows, selecting a row filters Product Name. The category is not filtered, even though each product belongs to one category. DAX does not infer a category filter from a product filter within the same table. Filters apply to the columns that are directly filtered. This is why the matrix needs the category field in the rows, or a slicer on category, for ALLEXCEPT to work as a group share.
Using `FILTER` in a share measure
Sometimes the group is defined by a condition rather than by a column value. For example, you may want each product's share of the sales from large order lines only. The numerator still uses the visual's filters. The denominator combines REMOVEFILTERS with a FILTER condition.
Large Line Sales =
CALCULATE (
[Total Sales],
FILTER ( Sales, Sales[Quantity] * Sales[Net Price] > 1000 )
)Large Line % of All Large Lines =
DIVIDE (
[Large Line Sales],
CALCULATE ( [Large Line Sales], REMOVEFILTERS ( 'Product' ) )
)The second measure reuses the first. Because Large Line Sales already contains a CALCULATE, the extra CALCULATE in the denominator only needs to clear the Product filter. You build up complex measures from small ones, and each piece stays readable.
Equivalent forms with `ALL`
Inside CALCULATE, ALL ( 'Product' ) clears the same filters as REMOVEFILTERS ( 'Product' ).
All Sales Using ALL =
CALCULATE ( [Total Sales], ALL ( 'Product' ) )Both forms give the same numbers in a share measure. Prefer REMOVEFILTERS when the only goal is to clear filters, because the name states that intent. Use ALL when you need a table back, for example COUNTROWS ( ALL ( 'Product'[Category] ) ), since REMOVEFILTERS cannot be used as a table expression.
Checking your measure
Use this short routine when a share measure looks wrong.
- Add the numerator and the denominator as separate measures and show both in the visual. A wrong percentage usually comes from a denominator you did not expect.
- Check the grand total and each subtotal row. They should be 100% where the denominator equals the numerator.
- Add and remove slicers one at a time. Note which filters change the denominator.
- Confirm that the fields in the rows match what
ALLEXCEPTneeds to keep.
Here is a debugging pair that makes the denominator visible.
Denominator All Products =
CALCULATE ( [Total Sales], REMOVEFILTERS ( 'Product' ) )Denominator Category =
CALCULATE ( [Total Sales], ALLEXCEPT ( 'Product', 'Product'[Category] ) )Place both next to Total Sales. If Denominator Category repeats the same value on every row, the category filter is missing from the visual.
Common mistakes in share measures
- Clearing filters in the numerator too. The numerator should keep the visual's filters, otherwise every row shows 100%.
- Using
REMOVEFILTERSwhere a table is expected, such as insideCOUNTROWS. It returns no table, so the formula fails. - Passing the whole
Producttable toALLorREMOVEFILTERSwhen one column would do. That clears more filters than you intended, including slicers on otherProductcolumns. - Expecting
ALLEXCEPTto keep a filter that the visual does not apply. - Dividing with
/and getting errors on empty cells. UseDIVIDE.
Exercise
A matrix has Product[Product Name] in the rows and no other Product field. A slicer on Date[Year] is set to 2025. You write Sales % of Category with ALLEXCEPT ( 'Product', 'Product'[Category] ). Each row shows its share of total 2025 sales instead of its share of the category. Why, and how do you fix it?
Show answer
ALLEXCEPT keeps only filters that already exist on the listed columns. The matrix filters Product Name but not Category, so the denominator clears the name filter and keeps no category filter. The result is the 2025 total across all products. Fix it by adding Product[Category] to the rows above Product Name, or by adding a slicer on Category. The Date filter stays in the denominator because ALLEXCEPT acts only on the Product table.
Exercise
You want each product's share of its category, but a slicer on Product[Color] is set to Black. The denominator must include only black products in the category. Which measure do you write for the denominator, and why does the ALLEXCEPT version not work?
Show answer
Use CALCULATE ( [Total Sales], REMOVEFILTERS ( 'Product'[Product Name] ) ). It clears only the product name filter, so the Color and Category filters stay. The ALLEXCEPT version with Category clears the Color filter too, so the denominator would include products of every color.