Sub topic 8
Putting it together: percent of total and percent of group
This chapter assumes you can write basic measures, know that a visual supplies a filter context, and have seen CALCULATE, REMOVEFILTERS, and ALLEXCEPT on their own. Here you combine them into the most common use of these functions: a share of a larger whole.
The pattern: numerator and denominator
Every percent-of-total measure divides two values.
- The numerator is a measure evaluated in the current filter context.
- The denominator is the same measure evaluated after CALCULATE removes some filters.
Total Sales = SUM ( Sales[Amount] )
Sales % of All Products =
DIVIDE (
[Total Sales],
CALCULATE ( [Total Sales], REMOVEFILTERS ( 'Product' ) )
)DIVIDE returns blank instead of an error when the denominator is zero or blank. Format the measure as a percentage in the model.
Evaluating each row
Put Product[Category] on matrix rows. Each row has its own filter context.
| Category | Total Sales | Sales % of All Products |
|---|---|---|
| Accessories | 300 | 30% |
| Bikes | 600 | 60% |
| Clothing | 100 | 10% |
| Total | 1,000 | 100% |
On the Bikes row the filter context is Category = Bikes. The numerator is 600. In the denominator, REMOVEFILTERS clears every filter on 'Product', so it evaluates to 1,000. The result is 60%. On the Total row there are no filters, so both parts equal 1,000.
Percent of group with ALLEXCEPT
Sometimes the whole is a group, not the grand total. Put Category and Subcategory in the matrix rows and measure each subcategory against its own category.
Sales % of Category =
DIVIDE (
[Total Sales],
CALCULATE ( [Total Sales], ALLEXCEPT ( 'Product', 'Product'[Category] ) )
)ALLEXCEPT clears all filters on 'Product' except the one on Category. The denominator therefore keeps the category and drops the subcategory.
| Row | Total Sales | Sales % of Category |
|---|---|---|
| Bikes | 600 | 100% |
| Road Bikes | 400 | 66.7% |
| Mountain Bikes | 200 | 33.3% |
On the Bikes row there is no subcategory filter, so the numerator and denominator match and the result is 100%.
Both denominators only touch the 'Product' table. Filters on other tables, such as a year on the rows or a slicer, stay in place. The percentages are then calculated within each year, which is usually what you want.
Choosing the denominator's scope
The denominator defines what 100% means. Pick the function from that definition.
| 100% should equal | Denominator filter argument |
|---|---|
| All sales for all products | `REMOVEFILTERS ( 'Product' )` |
| Sales of the current category | `ALLEXCEPT ( 'Product', 'Product'[Category] )` |
ALL ( 'Product' ) inside CALCULATE gives the same result as REMOVEFILTERS ( 'Product' ). Prefer REMOVEFILTERS here because the name states that you are only clearing filters.
Checking the measure
Test a percent-of-total measure in three places:
- At the lowest level of the visual, where each value should be a sensible share.
- At the subtotals, where sibling values should add up to the subtotal's value.
- At the grand total, which should show 100% when no slicer filters
'Product'.
Exercise
Write a measure that shows each product Subcategory as a share of its Category. Then say what value the Category row shows, and why.
Show answer
Sales % of Category = DIVIDE ( [Total Sales], CALCULATE ( [Total Sales], ALLEXCEPT ( 'Product', 'Product'[Category] ) ) ). On a Category row there is no subcategory filter to remove, so the numerator and denominator are equal and the row shows 100%.
Exercise
A matrix shows Category rows and uses Sales % of All Products. A Color slicer is set to Red. Why does the Total row not show 100%?
Show answer
The numerator at the Total row is Red sales only, because the slicer filters it. The denominator uses REMOVEFILTERS ( 'Product' ), which also clears the Color filter, so it is sales for all colors. Red sales divided by all sales is below 100%.