To use a MAX function with a filter in DAX, you must wrap the MAX function inside a CALCULATE function or use the MAXX function combined with a FILTER table. Because the standard MAX function only accepts a column reference and cannot take a filtered table as an argument, CALCULATE is used to modify the filter context before the maximum value is determined.
Understanding the DAX Filter Context
Before implementing a filtered maximum, it is essential to understand how DAX handles data. In Power BI and Analysis Services, the "filter context" refers to any filtering applied to the data model via slicers, report filters, or the coordinates of a visual (such as a row in a table). A standard measure like MAX(Sales[Amount]) respects the existing filter context. However, when you need to find a maximum value based on a condition that is not currently selected by the user—or one that overrides the current selection—you must explicitly define a new context.
The primary challenge for beginners is that MAX() is an aggregation function, not an iterator. It looks at a column and returns the largest value. It cannot "see" a logical condition (like Sales[Region] = "North") unless that condition is applied to the underlying data table before the MAX function executes. This is why the combination of CALCULATE and MAX is the industry standard for this requirement.
Using CALCULATE with MAX
The most common way to achieve a "Max with Filter" is by using the CALCULATE function. CALCULATE acts as a modifier; it evaluates the first expression in a context that is modified by the filters specified in the subsequent arguments.
For example, if you want to find the maximum sale amount specifically for the "Electronics" category, regardless of what other categories might be in the current view, the syntax would look like this:
MaxElectronicsSale = CALCULATE(MAX(Sales[Amount]), Sales[Category] = "Electronics")
In this scenario, CALCULATE first filters the Sales table to only include rows where the Category is "Electronics." Once that filtered subset of data is created, the MAX function scans the Amount column within that subset and returns the highest value. If no rows match the criteria, the result is a blank.
Implementing MAXX for Complex Filtering
While CALCULATE is efficient for simple column filters, there are times when you need to filter a table based on a complex expression or a measure. In these cases, the MAXX function is the correct tool. MAXX is an iterator, meaning it goes through a table row-by-row, evaluates an expression, and then finds the maximum of those results.
The syntax for MAXX is MAXX(. To add a filter, you nest the FILTER function inside the first argument of MAXX. A practical example would be finding the maximum sale amount for transactions that exceeded a certain threshold or met a multi-condition logic:, <expression>)
MaxHighValueSale = MAXX(FILTER(Sales, Sales[Amount] > 1000 && Sales[Status] = "Completed"), Sales[Amount])
Here is the step-by-step process of how this formula executes:
- The FILTER function scans the entire Sales table.
- It keeps only the rows where the Amount is greater than 1000 AND the Status is "Completed."
- This filtered table is passed to MAXX.
- MAXX iterates through the remaining rows and identifies the single largest value in the Amount column.
Comparing MAX vs. MAXX with Filters
Choosing between CALCULATE(MAX()) and MAXX(FILTER()) often comes down to performance and the nature of the filter. Understanding the difference prevents common performance bottlenecks in large datasets.
CALCULATE(MAX()) is generally faster for simple filters. This is because Power BI can optimize the filter using the Storage Engine, which handles column-based filtering very efficiently. When you use a simple boolean filter (e.g., Column = "Value"), DAX internally converts this into a CALCULATE function using ALL and FILTER, but it does so in a way that is highly optimized.
MAXX(FILTER()) is necessary when the filter expression is too complex for a simple boolean. For instance, if you need to compare two different columns in the same row (e.g., Sales[Actual] > Sales[Budget]), a simple CALCULATE filter will not work. You must use the FILTER function to perform this row-by-row comparison before passing the result to MAXX.
Common Problems and Troubleshooting
One of the most frequent issues users encounter is the "Unexpected Result" where the MAX value seems to ignore the slicers on the page. This usually happens when the ALL function is used incorrectly within the filter. If you use CALCULATE(MAX(Sales[Amount]), ALL(Sales)), you are explicitly telling DAX to ignore all filters on the Sales table, which will return the absolute maximum of the entire dataset regardless of the user's selection.
Another common error is attempting to use a measure inside a FILTER function without understanding context transition. When a measure is called inside FILTER, DAX performs a context transition, turning the row context into a filter context. This can lead to unexpected results or significant performance slowdowns if the table being iterated is very large.
To fix these issues, check the following:
- Verify if you want to override existing slicers (use ALL) or add to them (use KEEPFILTERS).
- Ensure the column being filtered is in the same table as the value being maximized, or that a proper relationship exists between the tables.
- Check for blank values; if the filtered set is empty, MAX will return BLANK, which might be interpreted as 0 in some visuals.
Advanced Filtering Scenarios
In professional reporting, you often need the maximum value from a related table. For example, finding the maximum order date for a specific customer. This requires the RELATEDTABLE function or the CALCULATE function across a relationship.
If you are working from the "Customer" table and want to find the max date from the "Orders" table, you would use:
LastOrderDate = CALCULATE(MAX(Orders[OrderDate]), RELATEDTABLE(Orders))
This ensures that for every customer row, the filter context is shifted to the Orders table, filtered for that specific customer, and then the maximum date is extracted. This is a critical pattern for creating "Last Seen" or "Most Recent Activity" reports.
Frequently Asked Questions
Can I use MAX with multiple filters?
Yes, you can add multiple filter arguments to the CALCULATE function. Each argument acts as an "AND" condition. For example, CALCULATE(MAX(Sales[Amount]), Sales[Region] = "West", Sales[Year] = 2023) will find the maximum sale that occurred in the West region during the year 2023.
Why is my MAXX function slowing down my report?
MAXX is an iterator. If you use it on a table with millions of rows and a complex FILTER expression, DAX must evaluate that expression for every single row. To improve performance, try to use CALCULATE with simple filters or ensure your filter reduces the table size as much as possible before the iteration occurs.
What is the difference between MAX and MAXX?
MAX is an aggregation function that takes a single column as an argument and returns the largest value. MAXX is an iterator function that takes a table and an expression; it evaluates the expression for each row of the table and then returns the maximum of those evaluated results.
How do I find the max value while ignoring one specific slicer?
You can use the ALLEXCEPT function within CALCULATE. For example, CALCULATE(MAX(Sales[Amount]), ALLEXCEPT(Sales, Sales[Category])) will ignore all filters on the Sales table except for the Category filter. This allows you to compare a specific item's value against the maximum value of its entire category.
Does MAX return a date or a number?
The MAX function returns the data type of the column it is referencing. If you point it at a numeric column, it returns the highest number. If you point it at a date column, it returns the most recent date.