Sub topic 7
ALLEXCEPT: Clearing All Filters on a Table Except Chosen Columns
This chapter assumes you can write basic measures with SUM and CALCULATE. It also assumes you know how REMOVEFILTERS clears filters inside CALCULATE. Here you will learn how to clear filters on a table while keeping the ones you name.
What ALLEXCEPT does
ALLEXCEPT removes every filter on a table except the filters on the columns you list. Its syntax is:
ALLEXCEPT ( <table>, <column> [, <column> ...] )The first argument is a table. Every other argument is a column of that same table. Filters on the listed columns survive. Filters on all other columns of that table are cleared.
Inside CALCULATE, it works as a filter modifier. Filters on other tables are not touched.
A worked example
Suppose a matrix shows Category and Subcategory from the Product table. You want each row to show its share of its category, not its share of the grand total.
Sales % of Category =
DIVIDE (
[Total Sales],
CALCULATE ( [Total Sales], ALLEXCEPT ( 'Product', 'Product'[Category] ) )
)The numerator keeps the current filters. The denominator clears all Product filters except Category. Consider the row for the subcategory "Laptops" under "Computers":
- The filter context contains
Category = ComputersandSubcategory = Laptops. ALLEXCEPTdrops theSubcategoryfilter and keeps theCategoryfilter.- The denominator is the sales of all Computers.
- The result is Laptop sales divided by Computer sales.
On the "Computers" category row, there is no subcategory filter, so the result is 100%.
ALLEXCEPT versus listing columns to clear
You can get the same result by naming the columns to clear instead of the columns to keep:
Sales % of Category (ALL columns) =
DIVIDE (
[Total Sales],
CALCULATE (
[Total Sales],
REMOVEFILTERS ( 'Product'[Subcategory], 'Product'[Color] )
)
)The two approaches behave differently when the report changes:
| Approach | Clears | If you add a new column to a visual |
|---|---|---|
| `ALLEXCEPT ( 'Product', 'Product'[Category] )` | Every `Product` column except `Category` | The new column is cleared too |
| `REMOVEFILTERS ( 'Product'[Subcategory], 'Product'[Color] )` | Only the columns you list | The new column still filters the denominator |
Use ALLEXCEPT when the rule is "the group is defined by these columns and nothing else." Use a column list when you want to clear one or two specific columns.
Keeping more than one column
You can list several columns. This measure keeps both Category and Color:
Sales % of Category and Color =
DIVIDE (
[Total Sales],
CALCULATE (
[Total Sales],
ALLEXCEPT ( 'Product', 'Product'[Category], 'Product'[Color] )
)
)The denominator is the sales of the current category and color combination. Other Product columns, such as Subcategory, are cleared.
Common mistakes
- Slicers on other columns are cleared too. If a report has a slicer on
Product[Brand]and your measure usesALLEXCEPT ( 'Product', 'Product'[Category] ), the denominator ignores the brand slicer. Your percentages may then not add up to 100% within the slicer selection. To keep the slicer, listProduct[Brand]as another kept column. - A column from another table does not work. Every column must belong to the table named in the first argument. To keep a filter from a
Datetable, you need a different pattern, such asVALUES. - The kept column must actually be in the filter context. If
Categoryis not in the visual or a slicer, there is nothing to keep, and the result equals the all-products total. - It clears filters, it does not add them. A visual that groups by a column in a different table is not changed by
ALLEXCEPTonProduct.
Summary
ALLEXCEPT is the shortest way to say "ignore everything on this table except these columns." It suits percent-of-group measures where the group is fixed. It is less suitable when new columns may appear in the report and should still filter the denominator.
Exercise
A matrix shows Category rows and Color columns from the Product table. You want each cell to show its share of the sales for its Color across all categories. Which ALLEXCEPT call do you put in the denominator, and why?
Show answer
Use CALCULATE ( [Total Sales], ALLEXCEPT ( 'Product', 'Product'[Color] ) ). It keeps the filter on Color and clears the filter on Category and every other Product column. The denominator is then the sales of the current color across all categories.
Exercise
A report has a slicer on Product[Brand]. Your measure uses ALLEXCEPT ( 'Product', 'Product'[Category] ) in the denominator, and the category percentages no longer add up to 100% when one brand is selected. What causes this, and how do you fix it?
Show answer
ALLEXCEPT clears the Brand filter, so the denominator includes all brands while the numerator includes only the selected brand. Add Product[Brand] to the kept columns: ALLEXCEPT ( 'Product', 'Product'[Category], 'Product'[Brand] ).