DAX and Calculated Measures
Writing measures in DAX: filter context, CALCULATE, iterators and time intelligence, the skill that separates Power BI users from builders.
Intermediate · about 12 hours · difficulty 4/5
Learn first: Power BI
DAX (Data Analysis Expressions) is the formula language of Power BI, Excel Power Pivot and Analysis Services. It looks like Excel formulas but behaves very differently, because every measure is evaluated inside a filter context set by the visual.
The two contexts
- Filter context: the filters coming from rows, columns, slicers and relationships. A
Total Sales := SUM(Sales[Amount])measure shows a different number in every cell because each cell has a different filter context. - Row context: exists inside calculated columns and iterator functions, meaning "the current row".
CALCULATE is the key
CALCULATE(expression, filters...) evaluates an expression under modified filters. Examples:
Sales LY := CALCULATE([Total Sales], SAMEPERIODLASTYEAR('Date'[Date]))Sales All Regions := CALCULATE([Total Sales], REMOVEFILTERS(Region))for percent of total.
Other essentials
- Iterators such as
SUMX(Sales, Sales[Qty] * Sales[Price])compute row by row, then aggregate. - DIVIDE handles divide-by-zero safely.
- Variables (
VAR ... RETURN) make long measures readable and faster. - Time intelligence (TOTALYTD, DATEADD) needs a proper, marked date table.
Pitfall: creating calculated columns for things that should be measures, bloating the model and giving wrong totals. Prefer measures unless you need to slice by the value. You are ready to move on when you can write year-over-year growth, percent of total and a rolling 3-month average as measures and explain their filter context.
Key concepts
- Measures vs calculated columns
- Filter context and row context
- CALCULATE and filter modifiers
- Iterators: SUMX, AVERAGEX
- Time intelligence functions
- Variables in DAX
- Context transition: Why a measure inside an iterator behaves differently
- Performance Analyzer and DAX Studio
Mini project
In your Power BI report add measures for YoY growth %, percent of category total, rolling 3-month sales and customer count with at least two orders, each documented with a one-line description.
Check yourself
Explain in plain words why the same SUM measure returns different numbers in each cell of a matrix.
Free resources
- DAX reference (Microsoft)
- DAX overview in Power BI (Microsoft)
- DAX Guide (SQLBI)