⚡0 XP🔥0

DAX Fundamentals - Measures, CALCULATE and Time Intelligence

Lesson 4 of 6 ⏱ 6 min read ❓ 5-question quiz

Write DAX measures - SUM, DIVIDE, COUNTROWS, DISTINCTCOUNT; filter context and CALCULATE; ALL for percent of total; iterator functions like SUMX; variables; and time intelligence (YTD, last year, YoY growth).

What is DAX?#

DAX (Data Analysis Expressions) is the formula language of Power BI (also used in Excel Power Pivot and Analysis Services). It looks like Excel formulas, but works on tables and columns, and its results respond to filters from slicers and visuals.

Measures vs implicit sums#

Dragging sales into a visual creates an implicit "Sum of sales". Professional reports use explicit measures:

  • reusable everywhere, defined once
  • named in business language
  • can contain logic (ratios, conditions, time comparisons)
  • required for many features (calculation groups, consistent formats)

Create one: select the Sales table → Home → New measure.

Tip

Create an empty table to hold measures: Home → Enter data → name it _Measures → load. Put all measures there so users find them easily.

Your first measures (Vindhya Mart sales.csv)#

Total Sales  = SUM ( Sales[sales] )
Total Profit = SUM ( Sales[profit] )
Orders       = COUNTROWS ( Sales )
Customers    = DISTINCTCOUNT ( Sales[customer_id] )
Margin %     = DIVIDE ( [Total Profit], [Total Sales] )          -- format as Percentage
Avg Order Value = DIVIDE ( [Total Sales], [Orders] )
Expected values (no filters)
Measure Value
Total Sales 1,10,66,455.10
Total Profit 15,02,052.15
Orders 1,500
Customers 407
Margin % 13.57%
Avg Order Value 7,377.64

Filter context - the key idea#

A measure is calculated for each cell of a visual, using the filters that apply to that cell (row labels, slicers, page filters). This is the filter context.

Put [Total Sales] in a matrix with region on rows: the same measure returns a different value in each row, because each row filters Sales to one region. You never write WHERE region = ... - the visual provides it.

हिंदी में समझें

Filter context DAX का सबसे ज़रूरी विचार है: एक ही measure (Total Sales) हर cell में अलग जवाब देता है, क्योंकि हर cell पर अलग filters लगे होते हैं - जैसे matrix की row में region = "West", slicer में year = 2025।

Measure खुद "कौन-सा region" नहीं जानता - visual और slicers उसे बताते हैं। इसीलिए एक measure को हर visual में दोबारा इस्तेमाल किया जा सकता है।

CALCULATE - changing the filter context#

CALCULATE ( <expression>, <filter1>, <filter2>, ... )
Technology Sales = CALCULATE ( [Total Sales], Sales[category] = "Technology" )
UPI Sales        = CALCULATE ( [Total Sales], Sales[payment_mode] = "UPI" )
Big Orders       = CALCULATE ( [Orders], Sales[sales] >= 10000 )
West Tech Sales  = CALCULATE ( [Total Sales], Sales[region] = "West", Sales[category] = "Technology" )

Filters inside CALCULATE replace existing filters on the same column and add to others.

ALL - removing filters (percent of total)#

All Region Sales = CALCULATE ( [Total Sales], ALL ( Sales[region] ) )
Region Share %   = DIVIDE ( [Total Sales], [All Region Sales] )

Grand Total Sales = CALCULATE ( [Total Sales], ALL ( Sales ) )
Share of Total %  = DIVIDE ( [Total Sales], [Grand Total Sales] )
Function Removes filters from Typical use
ALL(table/column) the table or column % of grand total
ALLSELECTED(...) inside the visual, but keeps slicer filters % of visible total
ALLEXCEPT(table, col) all columns except the listed ones % within a group
REMOVEFILTERS(...) same as ALL inside CALCULATE (clearer name)
Check these values (no slicers)
  • Technology Sales = 77,04,700.85 (69.62% of total)
  • Big Orders (≥ 10,000) = 320
  • West Region Share % = 24.05%
  • UPI Sales share = 47.91% of sales (UPI is 46.5% of orders but a bigger share of value)

Iterators - SUMX, AVERAGEX, COUNTX#

When the calculation must happen row by row:

Gross Sales (before discount) = SUMX ( Sales, Sales[quantity] * Sales[unit_price] )
Discount Given = [Gross Sales (before discount)] - [Total Sales]
Discount %     = DIVIDE ( [Discount Given], [Gross Sales (before discount)] )

In the shop star schema (price in Products), use RELATED inside the iterator:

Revenue = SUMX ( Sales, Sales[quantity] * RELATED ( Products[price] ) * ( 1 - Sales[discount] ) )

Variables - readable, faster DAX#

Profit YoY % =
VAR CurrentProfit = [Total Profit]
VAR LastYearProfit = CALCULATE ( [Total Profit], SAMEPERIODLASTYEAR ( 'Date'[Date] ) )
RETURN
    DIVIDE ( CurrentProfit - LastYearProfit, LastYearProfit )

Time intelligence#

Requires the Date table related to Sales[order_date] and marked as a date table.

Sales YTD        = TOTALYTD ( [Total Sales], 'Date'[Date] )
Sales LY         = CALCULATE ( [Total Sales], SAMEPERIODLASTYEAR ( 'Date'[Date] ) )
Sales YoY %      = DIVIDE ( [Total Sales] - [Sales LY], [Sales LY] )
Sales MTD        = TOTALMTD ( [Total Sales], 'Date'[Date] )
Sales Prev Month = CALCULATE ( [Total Sales], DATEADD ( 'Date'[Date], -1, MONTH ) )
Sales FYTD (Apr-Mar) = TOTALYTD ( [Total Sales], 'Date'[Date], "31/3" )
Rolling 3M Sales = CALCULATE ( [Total Sales], DATESINPERIOD ( 'Date'[Date], MAX ( 'Date'[Date] ), -3, MONTH ) )
हिंदी में समझें

Time intelligence = समय के हिसाब से तुलना करने वाले functions:

  • YTD (Year-to-date) - साल की शुरुआत से अब तक की कुल बिक्री
  • SAMEPERIODLASTYEAR - पिछले साल की उसी अवधि की बिक्री
  • YoY % - पिछले साल की तुलना में कितनी बढ़त/गिरावट

ये तभी सही काम करते हैं जब model में एक पूरा Date table हो (हर दिन की एक row) और वह Sales से जुड़ा हो।

Indian financial year (अप्रैल-मार्च) के लिए TOTALYTD(..., "31/3") लिखें।

Check time intelligence (matrix with Date[Year] and Date[Month])
  • 2025 total: Sales 53,91,536.05, Sales LY 56,74,919.05, Sales YoY % -5.0%
  • Sales YTD at June 2025 = 24,97,407.25 (vs 23,66,673.20 at June 2024 - so the first half of 2025 was actually ahead of 2024; the second half dropped)
  • October 2025 = 6,43,964.05 vs October 2024 7,70,154.55 (YoY about -16.4%)

Conditional logic in DAX#

Margin Status =
SWITCH (
    TRUE (),
    [Margin %] >= 0.20, "Healthy",
    [Margin %] >= 0.10, "OK",
    "Low"
)

Order Size (calculated column) =
IF ( Sales[sales] >= 50000, "Very large", IF ( Sales[sales] >= 10000, "Large", "Regular" ) )

Ranking#

Product Rank = RANKX ( ALL ( Sales[product] ), [Total Profit], , DESC, DENSE )

Use it in a table with product, then filter "Product Rank ≤ 5" for a Top 5 visual (or use the visual's Top N filter).

Common DAX mistakes#

Mistake Fix
/ instead of DIVIDE use DIVIDE for ratios
Calculated column for totals use measures for aggregations
Time intelligence without Date table create and mark a Date table
Averaging percentages divide totals: DIVIDE(SUM(profit), SUM(sales))
Huge nested IFs use SWITCH(TRUE(), ...) and variables

Summary#

  • Measures (Total Sales = SUM(Sales[sales])) are reusable, filter-aware calculations.
  • Filter context = filters from rows, columns, slicers; CALCULATE modifies it; ALL removes it.
  • Use DIVIDE for ratios, SUMX/RELATED for row-level math, VAR for readability.
  • Time intelligence (TOTALYTD, SAMEPERIODLASTYEAR, DATEADD) needs a marked Date table.

Quick Quiz

Q1.

Why use DIVIDE([Profit], [Sales]) instead of [Profit] / [Sales]?

Q2.

What does CALCULATE do?

Q3.

Which measure returns total sales ignoring any region filter?

Q4.

When do you need SUMX instead of SUM?

Q5.

SAMEPERIODLASTYEAR requires...