Power BI calculations

Writing DAX Measures for Common Business Metrics

Create reusable DAX measures by understanding filter context, safe division, iterators, time comparisons, and total behavior.

How this page is maintained

Written for learners, checked against the sources below, and reviewed every quarter. Last reviewed July 27, 2026.

Short answer

A DAX measure evaluates in the filter context supplied by the report. Build a simple base measure first, modify context explicitly with CALCULATE when required, and use DIVIDE for ratios. Test the measure at grand total, subtotal, row, and filtered states because a correct-looking detail row can still produce a wrong aggregate.

Who this is for: Power BI model authors who know basic formulas but need measures that remain correct across filters and totals.

  • Separate additive base measures from derived ratios so definitions remain reusable and auditable.
  • Reason explicitly about filter context and use CALCULATE only when the business rule requires changing it.
  • Validate totals, blanks, date coverage, and denominator behavior across representative report interactions.

Start with base measures

Create measures such as Total Sales, Units, Orders, and Customers directly from fact columns at known grain. Give each a business definition and format. Derived measures should reference these bases rather than repeating expressions. This improves consistency and lets one corrected definition flow through margin, average order value, and growth calculations.

Understand additivity. Sales may add across product and time, account balances may add across accounts but not dates, and percentages usually should not be summed or averaged from displayed rows. For a ratio, divide aggregated numerator by aggregated denominator in the current context. This naturally weights groups by their underlying volume.

Reason about filter context

A visual cell evaluates a measure under filters from rows, columns, slicers, page settings, relationships, and security. The same Total Sales measure returns different values for each product or month because context changes. Variables can name intermediate values and make business logic easier to inspect without changing that fundamental evaluation model.

CALCULATE evaluates an expression under modified filters. Use it for rules such as sales from completed orders or a comparison that removes one dimension while preserving others. Be precise about replacing, adding, or removing filters. Broadly clearing a table can erase user selections and produce a denominator unrelated to the displayed question.

Write robust ratios and comparisons

Use DIVIDE for denominators that may be zero or blank and choose an alternate result intentionally. Blank often communicates not applicable better than zero. For conversion, define converted entities and eligible entities at matching grain. DISTINCTCOUNT can be appropriate, but it should not be used to conceal duplicated rows caused by a faulty model.

Time comparisons require a complete date table, correct date relationships, and clear period logic. Prior month, prior year, rolling twelve months, and year to date answer different questions. Incomplete current periods need comparable elapsed-period logic or a visible warning. Fiscal calendars should come from the governed date dimension rather than formula shortcuts.

Test the evaluation surface

Place the measure in a matrix with relevant dimensions, subtotals, and a grand total. Test no-data combinations, one selection, multiple selections, unknown members, and cross-highlighting. Recalculate a few cells manually from source rows. A grand total is the measure evaluated at total context, not necessarily the arithmetic sum of visible derived rows.

Check performance after correctness. Iterators such as SUMX are essential when each row needs an expression, but unnecessary iteration over large tables can be expensive. Improve the model and reuse variables before inventing complex formulas. Microsoft documentation on measures and DAX basics is the current implementation reference for syntax and behavior.

Build a weighted gross margin measure

A report averages product-level margin percentages, overstating company margin because small high-margin products receive the same weight as large products.

  1. Create base measures for Net Sales and Cost from the sales fact at line grain.
  2. Define Gross Profit as Net Sales minus Cost and Gross Margin as DIVIDE of Gross Profit by Net Sales.
  3. Place product, category, and month in a matrix and compare detail values with source calculations.
  4. Verify the grand total recomputes total profit divided by total sales rather than averaging visible percentages.
  5. Test blank sales, returns, filters, and incomplete current periods before replacing the old measure.
Result: Every report slice uses a volume-weighted margin whose total follows the business formula instead of an average of averages.

DAX measure test card

Attach this card to governed measures during review.

  • Definition: business name, formula in words, grain, numerator, denominator, exclusions, and format.
  • Dependencies: base measures, fact table, dimensions, active relationships, and date role.
  • Context behavior: filters preserved, filters modified, blank rule, zero rule, and security expectation.
  • Test matrix: detail row, subtotal, grand total, no data, multi-select, unknown key, and cross-highlight.
  • Reconciliation: hand-calculated cases, source total, accepted variance, performance check, and owner approval.

Common mistakes

  • Averaging row-level percentages when the business metric requires total numerator divided by total denominator.
  • Removing all filters to calculate a benchmark and unintentionally ignoring selected region or customer scope.
  • Using a distinct count to mask a many-to-many duplication instead of correcting the model relationship.

Try one

Write the logic for a return rate measure and explain how you would verify its total across categories.

A correct response defines compatible base measures, such as returned units divided by eligible sold units, and uses safe division. It states whether returns are attributed to sale or return period and how canceled lines behave. Verification recomputes total returned units over total eligible units, checks categories and grand total, tests blanks and filters, and reconciles source transactions rather than averaging category percentages.

Sources

Learn this with a tutor

Tell LearnLive what you already know and what you need to do with write useful dax measures.

Build this course