Calculated Fields, DAX & LOD Expressions
Create measures with DAX/LOD that respect grain and context correctly.
In plain words
A measure is a smart formula that recalculates for whatever slice of data you are looking at, and LOD expressions let you force it to calculate at a specific level instead.
Think of it like…
A spreadsheet cell versus a pivot table: a calculated column is a formula in each row of the sheet, while a measure is a formula that the pivot recalculates every time you change the layout or filters, using only the rows visible in that cell.
Where you’ve seen it
Orders: East has two orders of $100 and $300, West has one order of $50. The true average order value is (100 + 300 + 50) / 3 = $150. A naive "average of the regional averages" gives (200 + 50) / 2 = $125, because West's single order gets the same weight as East's two. The correct measure is AOV = DIVIDE([Total Sales], [Order Count]), which stays right at any grain. A percent-of-total measure Sales % of Total = DIVIDE([Total Sales], CALCULATE([Total Sales], ALL(Product))) shows Product A as 60 / 100 = 60%: ALL(Product) removes the product filter from the denominator while region slicers still apply. In Tableau, a cohort field {FIXED [Customer ID] : MIN([Order Date])} gives each customer's first purchase date no matter how the chart is sliced, so retention by first-purchase month is possible.
Words you’ll hear
- Filter context
- The set of filters (slicers, rows, columns, visual and page filters) that determine which rows a measure sees.
- Row context
- Iterating over a table row by row, as in a calculated column or iterator function.
- CALCULATE
- DAX function that evaluates an expression after modifying the filter context.
- LOD expression
- Tableau calculation fixed at a chosen level: FIXED, INCLUDE or EXCLUDE.
Visual diagram
Theory & deep dive
DAX measures, contexts and CALCULATE
A measure such as Total Sales = SUM(Sales[Amount]) has no fixed value: each cell in a visual supplies a filter context and the engine aggregates only the matching rows. A calculated column (Margin = Sales[Amount] - Sales[Cost]) is evaluated once per row at refresh using row context and stored, which costs memory and cannot react to slicers, so use measures for aggregations and columns for row-level attributes or grouping. Iterators (SUMX(Sales, Sales[Qty] * Sales[Price])) create a row context inside a measure, which is how you aggregate row-level expressions correctly.
CALCULATE(expr, filter1, ...) evaluates expr after changing the filter context: ALL(Product) removes filters on the Product table, KEEPFILTERS and REMOVEFILTERS refine that, and boolean arguments such as Sales[Channel] = "Online" add or replace filters. Time intelligence builds on it: Sales LY = CALCULATE([Total Sales], SAMEPERIODLASTYEAR('Date'[Date])) requires a proper date table. Calling a measure inside a row context triggers context transition (the current row becomes filters), which is powerful and a frequent source of surprises. Prefer DIVIDE(a, b) to a / b to handle zero or blank denominators, and use variables (VAR ... RETURN) for readable, efficient measures.
Sales % of Total = DIVIDE([Total Sales], CALCULATE([Total Sales], ALL(Product))) with Product A = 60 and B = 40 returns 60% and 40%. With a region slicer set to East, the denominator becomes East's total, because ALL(Product) clears only the product filter.
Tableau LOD expressions and granularity traps
LOD expressions state the level at which to compute regardless of the viz: {FIXED [Region] : SUM([Sales])} returns the regional total on every row of that region; {INCLUDE [Customer] : SUM([Sales])} computes at a finer level than the view (then you average it); {EXCLUDE [Product] : SUM([Sales])} removes a dimension from the view's level. A common use is first-purchase date: {FIXED [Customer ID] : MIN([Order Date])}. Order of operations matters: FIXED calculations are evaluated before dimension filters (so a plain filter does not change them) unless the filter is promoted to a context filter; table calculations run last, after the view is built.
The classic grain bugs apply in both tools. Average of averages weights groups equally instead of by size (the $125 versus $150 example); ratios of sums versus sums of ratios (conversion = total conversions ÷ total visits, not the mean of daily rates); distinct counts that can't be summed across groups; semi-additive measures like inventory or account balances that should be summed across products but not across time (use the last value in the period); and measures placed in a table with the wrong grain so totals don't match row sums. Always reconcile a measure to the source at several levels, and write down each metric's definition, numerator, denominator and grain.
East orders: 100 and 300, average 200. West order: 50. True overall average = 450 / 3 = 150. Averaging the two regional averages gives (200 + 50) / 2 = 125, understating the true figure by 17% because it ignores that East has twice as many orders.
How it works, step by step
Step 1 of 41. Define the metric
Write the numerator, denominator, grain, filters and time behaviour before coding it.
- Document
- definition
When to use it & trade-offs
What it is
DAX (Data Analysis Expressions, used in Power BI) and LOD (Level of Detail) expressions (Tableau) let you define calculations that respect grain and context. In DAX a measure is evaluated at query time in the current filter context (the slicers, rows, columns and filters around a cell), while a calculated column is computed row by row at refresh in a row context and stored. CALCULATE changes the filter context (CALCULATE([Total Sales], ALL(Product))). In Tableau, {FIXED [Customer ID] : MIN([Order Date])} computes a value at a stated granularity regardless of the view. Understanding context is the difference between numbers that match the source and numbers that merely look right.
Why it matters: Business metrics (percent of total, year-over-year, average order value, first-purchase cohort) are rarely a simple sum, and a wrong aggregation level yields totals that don't add up, averages of averages or ratios that change when someone adds a filter. Interviews for analytics and BI roles routinely probe filter context, row context, CALCULATE and LOD order of operations because these are where most real dashboard bugs live.
When to reach for it
- Percent of total, year-over-year and moving averages
- First-purchase and cohort attributes (LOD)
- Ratios that must stay correct under any filter
When to avoid or be careful
- Averaging averages across groups of different sizes
- Using calculated columns for aggregations
- Dividing with / instead of DIVIDE on possibly zero denominators
Where to put a calculation
Strengths: Correct at any grain; no storage cost.
Watch out: Requires understanding of context.
Strengths: Simple; usable in slicers and axes.
Watch out: Stored in memory; cannot respond to filters.
Strengths: Tested, versioned and reusable.
Watch out: Less flexible for interactive context changes.
Interview signals & numbers to know
When this topic comes up
- “The total doesn't equal the sum of rows”
- “Percent of total changes when I filter”
- Filter context and row context
- CALCULATE and ALL
- FIXED LOD
Benchmarks & rules of thumb
- $150 vs $125
- true average order value versus average of regional averages
- 60 / 100 = 60%
- Product A's share in the example
- FIXED → context filter
- order of operations in Tableau
Common mistakes & follow-ups
Common interview mistakes
- Using a calculated column where a measure is needed and getting static results.
- Forgetting that a FIXED LOD ignores ordinary dimension filters.
- Summing a distinct count across groups.
Likely interviewer follow-ups
- What is the difference between a measure and a calculated column?
- What does CALCULATE do?
- When would you use a FIXED LOD expression?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Calculated Fields, DAX & LOD Expressions with data science interview Q&A and hands-on practice.