MSBA-261 · Lesson 5 of 12 · 21 min

DAX basics and evaluation context

▶ Video coming soon A video version of this lesson is on the way.

Kwabena could already write simple measures, but he wanted to understand why they gave the numbers they did. When a measure behaved strangely, he wanted to reason it through instead of trying variations until something worked. Signe, who wrote DAX daily for the college's Dataverse reporting, offered to run two Tuesday sessions on the language itself. "Learn evaluation context properly," she said, "and DAX stops being mysterious." This lesson covers DAX syntax, the main kinds of functions, variables, and the two contexts that every DAX expression is evaluated in.

DAX syntax in a few minutes

A DAX measure has a name, an equals sign and an expression. References follow strict rules: a column is written with its table name and the column name in square brackets, such as Registration[Credits], and a measure is written with its name in square brackets, such as [Net Tuition]. Table names that contain spaces or special characters go in single quotes, such as 'Course Section'[Section Code].

Writing columns with their table names and measures without them is a convention, not a requirement of the language, but it is a good one. It lets anyone reading a formula tell at a glance whether [Credits] is a column, which behaves one way, or a measure, which behaves another. Kwabena adopted it for every formula in the model.

ElementWritten asExample
Column referenceTable[Column]Registration[Credits]
Measure reference[Measure][Net Tuition]
Table with spaces'Table Name''Course Section'
Text valueDouble quotes"Lakeshore"
Comparison and logicComparison operators, AND (&&), OR and INCampus[Campus Name] IN { "Downtown", "Ridgeview" }
Comment// or -- or /* */// excludes withdrawals

DAX looks a little like Excel formulas, and many function names are the same, such as SUM, IF, ROUND and DATE. The resemblance is useful but misleading in one way: Excel formulas work on cells, and DAX works on tables and columns. A DAX formula never refers to a single cell; it works with whatever rows are visible in the current context.

Kinds of functions

DAX has hundreds of functions, but most measures use a small number from a few families.

FamilyExamplesWhat they do
AggregationSUM, AVERAGE, MIN, MAX, COUNT, COUNTROWS, DISTINCTCOUNTSummarize a column or table in the current context
IteratorsSUMX, AVERAGEX, COUNTX, MAXX, RANKXEvaluate an expression row by row over a table, then aggregate
Filter and contextCALCULATE, FILTER, ALL, ALLEXCEPT, REMOVEFILTERS, KEEPFILTERSRead and change filter context
Time intelligenceTOTALYTD, DATESYTD, SAMEPERIODLASTYEAR, DATEADDShift or extend date filters
LogicalIF, SWITCH, AND, OR, NOT, COALESCEChoose between results
InformationISBLANK, HASONEVALUE, ISINSCOPE, SELECTEDVALUETest the current context or a value
TableVALUES, DISTINCT, SUMMARIZECOLUMNS, ADDCOLUMNS, TOPNReturn tables for use inside other functions
StatisticalMEDIAN, PERCENTILE.INC, STDEV.S, VAR.SBasic statistics on a column or expression

The PL-300 study guide singles out a few kinds of measure: single aggregation measures, CALCULATE, time intelligence, basic statistical functions and semi-additive measures. This lesson covers the foundations, the next three lessons build on them, and statistical and semi-additive measures appear in the lessons on iterators and time intelligence.

Blanks and DIVIDE

DAX has a special value, BLANK, that represents no data. SUM over no rows returns blank, not zero, and visuals usually hide rows whose measures are all blank. That is helpful: a matrix of programs by campus does not show the many combinations with no enrolment. It also means that adding zero to a measure, a common trick to "show zeros", can suddenly make a visual display every possible combination of rows. Use it sparingly and on purpose.

The DIVIDE function divides two values and returns blank, or an alternative result you supply, when the denominator is zero or blank. It is safer than the slash operator, which returns an error or infinity, and it reads clearly. Kwabena used DIVIDE for every ratio in the model.

Variables

Variables, defined with VAR and returned with RETURN, store an intermediate value inside a formula. They make DAX easier to read, easier to debug and often faster, because a variable is evaluated once even if it is used several times.

Variables name each step of a calculation. A variable is evaluated once, in the context where it is defined.
Retention Rate =
VAR StartedFall =
    CALCULATE ( DISTINCTCOUNT ( Registration[StudentKey] ), 'Date'[Term] = "Fall 2025" )
VAR ReturnedFall =
    CALCULATE (
        DISTINCTCOUNT ( Registration[StudentKey] ),
        'Date'[Term] = "Fall 2026",
        Registration[Was Enrolled Fall 2025] = TRUE ()
    )
RETURN
    DIVIDE ( ReturnedFall, StartedFall )

A detail that catches people out: a variable is evaluated where it is defined, not where it is used. If a variable holds the result of a measure and is later used inside CALCULATE with different filters, it keeps its original value. Variables are constants once assigned.

Evaluation context

Every DAX expression is evaluated in a context that decides which data it sees. There are two kinds, and understanding them is the most important step in learning DAX.

Two panels. Filter context panel: a matrix with campuses on rows and terms on columns; one cell, Lakeshore and Fall 2026, is highlighted with a label saying its filters are Campus equals Lakeshore and Term equals Fall 2026. Row context panel: a list of Registration rows with an arrow moving down them one at a time, labelled current row.
Filter context is the set of filters applied to a calculation. Row context is the current row while a formula steps through a table.

Filter context

Filter context is the set of filters in effect when a measure is evaluated. In a matrix with campuses on rows and terms on columns, the cell for Lakeshore and Fall 2026 is evaluated with two filters: Campus Name equals Lakeshore and Term equals Fall 2026. The filters come from many places at once: the visual's rows, columns and axes, slicers, the filter pane at visual, page and report level, cross-filtering from other visuals, drill-through and row-level security. They combine, so a slicer on Program and the matrix cell together filter by campus, term and program.

The measure itself does not know where the filters came from. Net Tuition is simply SUM ( Registration[Net Tuition Amount] ), and the engine applies the filters to the model's tables, lets them travel along relationships to Registration, and adds up the rows that remain. The total row of the matrix is evaluated with fewer filters: the row total for Lakeshore has only the campus filter, and the grand total has none from the matrix at all. That is why measures total correctly without any special code.

Row context

Row context exists when DAX is stepping through a table one row at a time. It arises in two places: in a calculated column, where the expression is evaluated once for each row of the table, and inside iterator functions such as SUMX and FILTER, which step through the table they are given. In a row context, a column reference returns that column's value in the current row.

Row context does not filter anything by itself. This is the most important and least intuitive rule in DAX. Inside a calculated column on Program, the expression SUM ( Registration[Credits] ) returns the total credits for the whole college on every row, not the credits for that row's program, because the row context on Program does not filter Registration.

Row context alone does not filter related tables. CALCULATE turns the current row into a filter.
// Calculated columns on the Program table
Credits Wrong = SUM ( Registration[Credits] )                 // same college-wide total on every row
Credits Right = CALCULATE ( SUM ( Registration[Credits] ) )   // credits for this program only

Context transition

The second formula works because of context transition. When CALCULATE is evaluated inside a row context, it turns the current row into an equivalent filter context: every column of the current row becomes a filter. On the row for the Practical Nursing program, CALCULATE creates a filter Program = Practical Nursing, which travels along the relationship to Registration, and SUM then adds only that program's credits.

Context transition also happens automatically whenever a measure is referenced inside a row context, because the engine wraps every measure reference in an implicit CALCULATE. That is why a calculated column on Program that simply says [Total Credits] returns the right value for each program. It is also why measures referenced inside iterators can be slow on large tables: each row triggers a context transition.

Kwabena summarized the rule on a sticky note for his monitor: "Row context: which row am I on? Filter context: which rows can I see? CALCULATE turns the first into the second."

Reading a DAX formula

When a measure gives an unexpected result, Signe's method is to ask three questions in order. What is the filter context of the cell in question, including slicers and page filters? Does the formula create any row contexts, through iterators or calculated columns? Where does CALCULATE, or a measure reference, change the filter context? Answering those three questions explains nearly every surprising number.

Tools help. DAX query view in Power BI Desktop runs a query with explicit filters so you can see a measure's result in a known context. Performance Analyzer shows the DAX query each visual sends, which you can copy into DAX query view to examine. And breaking a formula into variables lets you return each variable in turn to see what it contains.

Worked problem

Explaining a surprising total

A table visual lists programs with a measure Avg Credits = AVERAGE ( Registration[Credits] ). Each program shows a sensible average of about 3, and the total row shows 2.9. A dean asks why the total is not the average of the program averages. What should Kwabena explain?

Show solution
  1. Each program row is evaluated in a filter context of that program, so AVERAGE runs over that program's enrolment rows.
  2. The total row has no program filter, so AVERAGE runs over all enrolment rows in the college.
  3. Programs with many registrations and lower average credits pull the college-wide average down.
  4. An average of averages would weight every program equally regardless of size, which is usually not what the dean means.

Answer: The total is the average over all registrations, evaluated in the total row's own filter context. It is correct; an average of program averages would ignore program size.

A worked tour of one cell

Signe's second session took one number from the deans' overview and traced it from end to end. The number was Net Tuition in the cell for the Practical Nursing program, with a page slicer set to Fall 2026 and a report-level filter that excluded internal test registrations.

First, she listed the filters in the cell's filter context: Program Name equals Practical Nursing, from the matrix row; Term equals Fall 2026, from the slicer; Is Test equals False, from the report filter; and, for any reader in the Campus Coordinators role, a security filter on campus. Second, she followed each filter to the fact table. The Program filter travelled from the Program dimension to Registration along the ProgramKey relationship. The Term filter travelled from Date to Registration along the active term start relationship. The Is Test filter was on a column of Registration itself. Third, she applied the measure. Net Tuition is SUM ( Registration[Net Tuition Amount] ), so the engine added the net tuition of the Registration rows that survived every filter.

Then she changed the measure to show context transition at work. She added a calculated column on Program called Program Tuition All Terms, defined as CALCULATE ( SUM ( Registration[Net Tuition Amount] ) ). On the Practical Nursing row, context transition turned the row into a filter on that program, so the column showed the program's tuition for all terms. It ignored the Fall 2026 slicer completely, because calculated columns are computed at refresh, before any slicer exists. That single example, she said, explained the most common beginner question: "why does my calculated column not change when I click the slicer?"

Worked problem

Predicting the result of a formula

A calculated column on Campus is defined as COUNTROWS ( Registration ). Another is defined as [Registrations], where Registrations = COUNTROWS ( Registration ). What does each show on the Lakeshore row?

Show solution
  1. The first column has a row context on Campus but no CALCULATE, so COUNTROWS ( Registration ) sees all Registration rows.
  2. It shows the college-wide registration count on every campus row, including Lakeshore.
  3. The second column references a measure, which is wrapped in an implicit CALCULATE.
  4. Context transition turns the Lakeshore row into a filter, so it shows Lakeshore's registrations across all terms.

Answer: The first shows the college total on every row; the second shows each campus's own total, because the measure reference triggers context transition.

Formatting DAX so others can read it

Kwabena's measures were read by Signe, by Rupert's reviewers and, eventually, by whoever inherited the model. He followed three habits that make DAX readable: one function argument per line for anything longer than a single line, consistent indentation, and variables with names that describe their meaning, such as PriorYearRegistrations rather than x. Tools such as the DAX Formatter website, or the formatting command in DAX query view, apply a standard layout automatically. Readable formulas are reviewed more carefully, and reviewed formulas have fewer mistakes.

Common mistakes

  • MistakeExpecting a calculated column's row context to filter related tables.

    InsteadUse CALCULATE or a measure reference to trigger context transition.

  • MistakeWriting column references without table names and measure references with them.

    InsteadUse Table[Column] for columns and [Measure] for measures so readers can tell them apart.

  • MistakeUsing the slash operator for ratios.

    InsteadUse DIVIDE, which handles zero and blank denominators.

  • MistakeAssuming a variable re-evaluates when used inside CALCULATE.

    InsteadRemember that variables are evaluated once, where they are defined.

Summary and key terms

  • DAX references columns as Table[Column] and measures as [Measure]; quote table names with spaces.
  • Most measures use aggregation, iterator, filter, time intelligence, logical and information functions.
  • BLANK represents no data; DIVIDE avoids division errors; adding zero can expose unwanted rows.
  • Variables make formulas readable and efficient and are evaluated once where defined.
  • Filter context is the set of filters on a calculation, from visuals, slicers, filters and security.
  • Row context is the current row in a calculated column or iterator, and it does not filter by itself.
  • CALCULATE, and every measure reference, performs context transition from row context to filter context.
Filter context
The filters in effect when a DAX expression is evaluated.
Row context
The current row while DAX steps through a table.
Context transition
The conversion of a row context into an equivalent filter context by CALCULATE.
BLANK
The DAX value that represents no data.
DIVIDE
A function that divides safely, returning blank or an alternative for zero denominators.
Variable
A named value defined with VAR and evaluated once inside a formula.

Practice problems

Work each problem on paper first, then reveal the answer to check.

  1. In a matrix with campuses on rows, what is the filter context of the grand total cell?

    Show answer

    No campus filter from the matrix; only slicers, filter pane filters and any security filters apply.

  2. Why does SUM ( Registration[Credits] ) in a calculated column on Program return the same value on every row?

    Show answer

    The row context on Program does not filter Registration; there is no context transition without CALCULATE.

  3. Name two places where row context arises.

    Show answer

    Calculated columns and iterator functions such as SUMX or FILTER.

Lesson quiz · 5 questions

Score 60% or more to complete this lesson.

Take the quiz