Filtering Data Using Calculate
One of the most powerful features of DAX is the ability to manipulate filters. This allows for complicated time based logic, such as previous year to date. It allows for various comparisons, like a percentage of total sales.
Visuals and Pivot Tables are implicitly applying filters behind the scenes. These filters can later be modified in DAX. The way we do that is with the Calculate function. This is the corner stone of filter manipulation in DAX. If you're using DAX with any level of complexity, you're going to become very well acquainted with this function.
Why Filtering?
Filtering is the key to any analytical reporting. It's necessary to do any comparisons over dimensions. Want to know How sales in North America compare to Asia? You need filtering. Want to know How black products compare to gray products? You need filtering.
Okay, so black products are doing great this year, want to drill down to see Why? You need filtering. Want to see How this year's sales compare to last year's sales? You need filtering. Want to see What percentage of black products were of total sales? You need filtering.
- Comparison by dimensions
- Drill downs
- Period-based comparisons
- Slice-of-pie comparisons
In some of these cases the filtering is implied by the visuals or the grid layouts. In other cases, like the percentage of total, the filtering is explicitly built into the Measure.
Filtering, whether implicit or explicit, is integral to analytical reporting. This is because so much analytical reporting is about slicing and dicing. It's about making comparisons. Comparisons over categories, comparisons over time, comparisons to the whole. And all of those require manipulating filters. DAX in particular is optimized for two key things:
- Aggregations
- Filtering
Using CALCULATE
The main way to apply filters in DAX expressions is the CALCULATE function. The CALCULATE function allows us to override filters applied by the user. This is going to be one of the most common functions you call in DAX. To understand how CALCULATE works, we first need to talk about How filters are applied?
Implicit vs. Explicit Filtering
To make things easier to think about, I like to break up the types of filtering into two broad buckets
Implicit Filtering
Filtering applied by the user or the layout of the report.
This is filtering that has been applied before the DAX expression is evaluated. This is often the result of user actions where in Power BI they specifically click on a slicer visual, or in Excel perhaps click on the down arrow to filter things. Additionally, this is done implicitly by most visuals as well. Anytime you add a category to a grid or bar chart, the DAX engine is implicitly filtering based on that category.
Explicit Filtering
Filtering applied by the Measures.
THese are filters coded into the DAX expressions. Now here is the key point, explicit filtering overrides implicit filtering. So if the user via action or report setup has filtered all the products on the color black, the DAX expression can override that if it filters on the color green instead. Expect to use CALCULATE frequently when you are working with Measures because it really is such a powerful tool.
CALCULATE Function Example
Here's an example of using CALCULATE. We want to add up the total sales amount, but we want to make sure that the color filter is always green, no matter what the user has applied. The first parameter specifies the expression we want to evaluate. The next parameter expresses the filter manipulations that we want to apply.
Sum of Green Products = CALCULATE( SUM( Sales[Total] ), [Color] = "Green")
In this case we are overriding any applied filters on the color column and filtering it to Green instead. CALCULATE can support multiple sets of filtering manipulations, separated by commas.