Skip to main content

DAX & Measures

4. DAX — Data Analysis Expressions

DAX is Power BI's formula language. Understanding DAX is the difference between an average and an excellent Power BI user.

4.1 Calculated Columns vs Measures

FeatureCalculated ColumnMeasure
When calculatedAt data refresh (stored in table)At query time (on the fly)
MemoryUses memory (stored per row)Minimal memory
ContextRow context — access to current row valuesFilter context — aggregates based on current filters
Use whenYou need a value for each ROWYou need an AGGREGATION that changes with filters
ExampleProfit = [Revenue] - [Cost]Total Revenue = SUM(Sales[Revenue])

🧠 Simple rule: If you need it per row (like calculating profit for each order) = Calculated Column. If you need it as a total/average that changes with slicers = Measure.

4.2 Essential DAX Functions

Basic Aggregation

Total Revenue = SUM(Sales[Revenue])
Total Orders = COUNTROWS(Sales)
Avg Order Value = DIVIDE(SUM(Sales[Revenue]), COUNTROWS(Sales), 0)
Unique Customers = DISTINCTCOUNT(Sales[CustomerID])

🧠 Always use DIVIDE() instead of / — DIVIDE handles division by zero gracefully.

CALCULATE — The Most Powerful Function

CALCULATE allows you to modify the filter context of a calculation. It's the most important DAX function.

Syntax: CALCULATE(expression, filter1, filter2, ...)

// Total Revenue (responds to all report filters)
Total Revenue = SUM(Sales[Revenue])

// Revenue for Electronics ONLY (ignores category slicer)
Electronics Revenue =
CALCULATE(
SUM(Sales[Revenue]),
Products[Category] = "Electronics"
)

// Revenue for 2024 ONLY
Revenue 2024 =
CALCULATE(
SUM(Sales[Revenue]),
DateTable[Year] = 2024
)

// % of Total (powerful comparison)
Revenue % of Total =
DIVIDE(
SUM(Sales[Revenue]),
CALCULATE(SUM(Sales[Revenue]), ALL(Products)),
0
)

🧠 CALCULATE = "is number ko calculate karo, WITH THESE extra conditions." All other DAX builds on this function.

Time Intelligence (Very Frequently Asked)

These functions require a proper Date Table in your model.

// Year-to-Date Revenue
YTD Revenue = TOTALYTD(SUM(Sales[Revenue]), DateTable[Date])

// Previous Year Revenue (for comparison)
PY Revenue = CALCULATE(SUM(Sales[Revenue]), SAMEPERIODLASTYEAR(DateTable[Date]))

// Year-over-Year Growth
YoY Growth % =
VAR CurrentYear = SUM(Sales[Revenue])
VAR PreviousYear = [PY Revenue]
RETURN
DIVIDE(CurrentYear - PreviousYear, PreviousYear, 0) * 100

// Month-to-Date
MTD Revenue = TOTALMTD(SUM(Sales[Revenue]), DateTable[Date])

// Running Total (Cumulative)
Running Total =
CALCULATE(
SUM(Sales[Revenue]),
FILTER(
ALL(DateTable),
DateTable[Date] <= MAX(DateTable[Date])
)
)

🧠 Time intelligence = interview mein zaroor aayega. YTD, PY, YoY Growth — yeh teen ratt lo.

Iterator Functions (Row-by-Row Calculations)

FunctionWhat It Does
SUMX(table, expression)Calculates expression for each row, then sums
AVERAGEX(table, expression)Calculates expression for each row, then averages
MAXX(table, expression)Calculates expression for each row, returns max
// Weighted Average Price (can't do this with simple SUM/AVG)
Weighted Avg Price =
SUMX(
Sales,
Sales[Quantity] * Sales[UnitPrice]
) / SUM(Sales[Quantity])

4.3 ALL, ALLEXCEPT, REMOVEFILTERS

FunctionWhat It DoesUse Case
ALL(table/column)Removes all filtersCalculating % of grand total
ALLEXCEPT(table, column)Removes all filters EXCEPT specified column% of category total
REMOVEFILTERS(column)Same as ALL but clearer intentModern replacement for ALL
// % contribution of each product to its category total
Category Contribution % =
DIVIDE(
SUM(Sales[Revenue]),
CALCULATE(
SUM(Sales[Revenue]),
ALLEXCEPT(Products, Products[Category])
),
0
)