DAX Measures for Beginners

DAX Measures for Beginners – DataScienceWay tutorial

DAX (Data Analysis Expressions) is the formula language of Power BI, Excel’s Data Model (Power Pivot) and Analysis Services. It looks like Excel formulas, which is both helpful and misleading: the functions feel familiar, but DAX works on whole tables and columns rather than individual cells, and its results depend on the filters applied by the report. Understanding that one idea – filter context – is the key to writing measures that work.

This tutorial explains measures versus calculated columns, filter context, the essential functions (SUM, DIVIDE, CALCULATE, ALL, FILTER), variables, and the most common time-intelligence measures. It uses the sales model from Building Your First Power BI Dashboard (tables Sales, Products, Stores and Date), but the ideas apply to any star schema.

Measures vs calculated columns

There are two main places to write DAX:

Comparison table: calculated columns are computed per row on refresh, stored in the model and used for slicing; measures are computed per cell at query time, not stored, respond to slicers and are used for totals and KPIs
A quick way to decide which one to write.
  • A calculated column is computed once per row when data is refreshed, and stored in the table. Example: a price band for each product. It is evaluated in row context – it can see the values in its own row.
  • A measure is computed at query time, for whatever filters apply to each cell of a visual. Example: total revenue. It is evaluated in filter context.

Rule of thumb: if you want to slice or filter by the result, use a column; if you want to aggregate it, use a measure. Most business calculations – totals, ratios, growth, percentages – should be measures. Calculated columns increase file size and are often used where a measure would be better.

-- Calculated column on Products: a category you can slice by
Price Band = IF ( Products[UnitPrice] >= 100, "Premium", "Standard" )

-- Measure: an aggregate that responds to filters
Total Revenue = SUM ( Sales[Revenue] )

Step 1: Understand filter context

Put Total Revenue in a matrix with Region on rows. The single formula SUM ( Sales[Revenue] ) returns a different number in each row. Why? Because for the “North” row, the report filters the Stores table to Region = North; that filter flows along the relationship to Sales; and SUM only sees the North sales rows. The total row has no region filter, so it sums everything.

Table visual of a Total Revenue measure by region, East 1,070, North 2,255, South 3,610 and Total 6,935, explaining that each row's filter is applied before SUM runs
One measure, evaluated once per cell under that cell’s filters.

Every cell in every visual has its own filter context, built from rows, columns, slicers, page and report filters, and cross-filtering from other visuals. A measure is simply “this calculation, under whatever filters are active here”. Once you think this way, DAX starts to make sense.

Step 2: Basic aggregation measures

Total Revenue = SUM ( Sales[Revenue] )

Total Units = SUM ( Sales[Quantity] )

Sales Count = COUNTROWS ( Sales )

Stores Selling = DISTINCTCOUNT ( Sales[StoreID] )

Avg Sale Value = DIVIDE ( [Total Revenue], [Sales Count] )

Notice that Avg Sale Value reuses other measures. Building measures from measures keeps logic in one place: if the definition of revenue changes, you update one measure. Always use DIVIDE rather than the / operator for ratios, as it returns blank instead of an error when the denominator is zero or blank. DISTINCTCOUNT counts unique values – here, how many stores made at least one sale in the current filter context. In a model with customers, DISTINCTCOUNT ( Sales[CustomerID] ) gives the number of unique customers.

Reference measures without a table prefix ([Total Revenue]) and columns with one (Sales[Revenue]). This widely used convention makes formulas easier to read.

Step 3: CALCULATE – changing the filter context

CALCULATE is the most important function in DAX. It evaluates an expression under modified filters:

Electronics Revenue =
CALCULATE ( [Total Revenue], Products[Category] = "Electronics" )

North Revenue =
CALCULATE ( [Total Revenue], Stores[Region] = "North" )

In a matrix by store, Electronics Revenue shows each store’s electronics revenue. The filter argument replaces any existing filter on Products[Category] while keeping all other filters (such as the store on the row). If the row is already filtered to Furniture, the result shows electronics anyway, because the Category filter was overridden – a frequent source of confusion.

To add to the existing filter rather than replace it, wrap the condition in KEEPFILTERS:

Electronics Revenue (kept) =
CALCULATE ( [Total Revenue], KEEPFILTERS ( Products[Category] = "Electronics" ) )

Step 4: ALL and percentage of total

ALL removes filters from a table or column. It is how you get a grand total to divide by:

Revenue All Categories =
CALCULATE ( [Total Revenue], ALL ( Products[Category] ) )

% of Category Total =
DIVIDE ( [Total Revenue], [Revenue All Categories] )

In a visual by category, each row’s revenue is divided by the revenue of all categories, while other filters – such as a Region slicer – still apply. Format the measure as a percentage. If you want the share of the visible total after slicers, use ALLSELECTED instead of ALL.

Step 5: FILTER for complex conditions

Simple column-equals-value filters can go directly in CALCULATE. For conditions involving measures or several columns, use FILTER, which iterates over a table and keeps rows meeting a condition:

Revenue from Big Sales =
CALCULATE ( [Total Revenue], FILTER ( Sales, Sales[Revenue] >= 500 ) )

Products Over 10k =
COUNTROWS ( FILTER ( VALUES ( Products[Product] ), [Total Revenue] > 10000 ) )

FILTER over a whole large fact table can be slow. Where possible, filter a single column (for example FILTER ( ALL ( Sales[Revenue] ), Sales[Revenue] >= 500 ), or simply Sales[Revenue] >= 500 as a CALCULATE argument, which DAX expands efficiently).

Step 6: Iterators – SUMX and friends

Functions ending in X (SUMX, AVERAGEX, MAXX) evaluate an expression row by row, then aggregate. Use them when the calculation must happen per row first:

Revenue (calculated) =
SUMX ( Sales, Sales[Quantity] * RELATED ( Products[UnitPrice] ) )

RELATED fetches a value from the “one” side of a relationship – similar to a lookup. This measure computes revenue without needing a stored Revenue column. It also illustrates why “sum of products” differs from “product of sums”: multiplying total quantity by average price would give a different, wrong answer.

Step 7: Variables for clarity

VAR stores an intermediate result; RETURN gives the final value. Variables make long measures readable, avoid repeating calculations, and help debugging (temporarily return a variable to check it):

Revenue Growth % =
VAR CurrentRevenue = [Total Revenue]
VAR PreviousRevenue = CALCULATE ( [Total Revenue], DATEADD ( 'Date'[Date], -1, MONTH ) )
RETURN
    DIVIDE ( CurrentRevenue - PreviousRevenue, PreviousRevenue )

Step 8: Time intelligence

Time-intelligence functions need a proper date table: one row per day with no gaps, related to your fact table, and marked as a date table. With that in place:

Revenue YTD = TOTALYTD ( [Total Revenue], 'Date'[Date] )

Revenue Last Year = CALCULATE ( [Total Revenue], SAMEPERIODLASTYEAR ( 'Date'[Date] ) )

Revenue YoY % = DIVIDE ( [Total Revenue] - [Revenue Last Year], [Revenue Last Year] )

Revenue Rolling 3M =
CALCULATE (
    [Total Revenue],
    DATESINPERIOD ( 'Date'[Date], MAX ( 'Date'[Date] ), -3, MONTH )
)

For a UK financial year ending 31 March, pass the year-end date: TOTALYTD ( [Total Revenue], 'Date'[Date], "31/3" ). Note that our sample data covers only 2024, so “last year” measures will be blank – exactly what should happen when there is no prior data.

Step 9: Rankings

Product Rank =
RANKX ( ALL ( Products[Product] ), [Total Revenue], , DESC, DENSE )

ALL ensures each product is ranked against all products, not just itself (inside a row for one product, the filter context contains only that product, so without ALL every product would rank 1). This is the DAX counterpart of SQL’s window functions.

Common mistakes

  • Using calculated columns for aggregations that should be measures.
  • Using / instead of DIVIDE.
  • Forgetting that CALCULATE filters replace existing ones on the same column.
  • Time intelligence without a marked, continuous date table.
  • Totals that “don’t add up”. The total row is evaluated in its own filter context, not by summing the rows above. For non-additive measures (averages, distinct counts, ratios) this is correct behaviour, not a bug.
  • FILTER over entire large tables when a simple column filter would do.
  • Unformatted, uncommented long measures. Use variables, line breaks and // comments.

FAQ

Is DAX the same as Excel formulas?

Many function names are shared, but DAX operates on tables and filter context rather than cell references.

Where can I test DAX outside Power BI?

Excel’s Power Pivot uses the same language. DAX Studio, a free community tool, lets you run and time queries against a model.

What is the difference between ALL and REMOVEFILTERS?

Used as a CALCULATE modifier, REMOVEFILTERS does the same as ALL but with a clearer name. ALL can also return a table, which REMOVEFILTERS cannot.

How is this related to SQL GROUP BY?

Filter context is conceptually like the grouping in GROUP BY: each cell is an aggregate over the rows matching its filters.

Next steps

Put these measures into a report with Building Your First Power BI Dashboard, clean your sources with Power Query, and compare with the spreadsheet approach in Excel Pivot Tables.

LJ

About the author: Lucky Joseph

Lucky is a UK-based writer with a data science background who runs DataScienceWay. Every tutorial is tested before publication; if you spot something that no longer works, please get in touch.

More about DataScienceWay · Contact