Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST TOOLS MATH

Power BI DAX Variables


Share

What Are Variables?

A variable stores the result of an expression under a name.

You can then use that name in the rest of the formula.

This page uses the Poke Mart model described in DAX Introduction, with the Total Sales measure from DAX Measures.

Variables use two keywords, VAR and RETURN:

Syntax

Measure Name =
VAR name1 = expression1
VAR name2 = expression2
RETURN
    result
KeywordMeaning
VARDefines a variable: a name, an equal sign and an expression. You can have as many as you need. A variable can use the variables above it.
RETURNThe final expression. Its value is the result of the measure.


A First Example

What share of the sales comes from big orders, the orders with a quantity of 10 or more?

You need the sales of the big orders, the sales of all orders, and then a division:

Example

Big Order % =
VAR BigOrderSales = CALCULATE([Total Sales], Sales[Quantity] >= 10)
VAR AllSales = [Total Sales]
RETURN
    DIVIDE(BigOrderSales, AllSales)

Format the measure as Percentage with 2 decimal places, and put it in a matrix by Category:

CategoryTotal SalesBig Order %
Balls4,447,20061.32%
Medicine4,413,60033.85%
Status Healers556,00023.58%
Total9,416,80046.22%

The variable names tell the reader what each part is.

The RETURN line reads almost like a sentence: divide the big order sales by all sales.

The big order sales come from the same simple filter as on the FILTER page: 4,352,100 of 9,416,800 in total.

Write a Formula on Several Lines

In the formula bar, press Shift+Enter or Alt+Enter to start a new line.

Press Enter when the formula is done.

To get more room, select the arrow at the right end of the formula bar to make it bigger.

Do not press Esc to close the suggestion list while you type.

Esc cancels the whole measure, not only the list.

The expanded formula bar in Power BI Desktop showing the Big Order % measure written on five lines, with two VAR lines and a RETURN line

Rules for Variable Names

  • Use letters and digits. Do not start with a digit.
  • No spaces: write BigOrderSales, not Big Order Sales.
  • Do not use the name of a table. A variable cannot be called Sales, because the model has a Sales table.
  • Do not use DAX keywords, like VAR or RETURN.


Year-over-Year Growth with Variables

Year-over-year growth (YoY) compares sales with the same period one year earlier:

(this year - last year) / last year

Last year's sales come from a time intelligence function that you will learn on the next page, SAMEPERIODLASTYEAR.

For now, you only need to know what this expression returns:

CALCULATE([Total Sales], SAMEPERIODLASTYEAR('Date'[Date]))

It returns the sales of the same period, one year earlier.

In a visual filtered to 2025, it returns the sales of 2024: 4,393,200.

Without variables, the growth formula needs this expression twice:

DIVIDE(
    [Total Sales] - CALCULATE([Total Sales], SAMEPERIODLASTYEAR('Date'[Date])),
    CALCULATE([Total Sales], SAMEPERIODLASTYEAR('Date'[Date]))
)

It works, but it is long, and the same calculation is written twice.

With variables, each part is written once, and gets a name:

Example

Sales YoY % VAR =
VAR CurrentSales = [Total Sales]
VAR LastYearSales = CALCULATE([Total Sales], SAMEPERIODLASTYEAR('Date'[Date]))
RETURN
    DIVIDE(CurrentSales - LastYearSales, LastYearSales)

Format it as Percentage with 2 decimal places.

Make a matrix with Region in Rows, and Total Sales and Sales YoY % VAR in Values.

Add a slicer with Year.

A slicer on a number column like Year starts as a Between slider, so go to Format visual > Visual > Slicer settings > Options and set Style to Vertical list. Then select 2025:

Region (2025)Total SalesSales YoY % VAR
Hoenn1,647,30024.35%
Johto1,751,2008.60%
Kanto1,625,10011.61%
Total5,023,60014.35%

Hoenn grew the most, from 1,324,700 in 2024 to 1,647,300 in 2025. That is (1,647,300 - 1,324,700) / 1,324,700 = 24.35%.

If you select 2024 in the slicer, the measure is blank.

The data starts in 2024, so there is no last year to compare with.

The next page explains this.

On the next page, you create a separate Sales LY measure for last year's sales, and a Sales YoY % measure that uses it.

It gives the same results as Sales YoY % VAR.

A variable can also hold a measure, for example VAR LastYearSales = [Sales LY].


Why Use Variables?

BenefitWhy
ReadabilityNames like CurrentSales and LastYearSales explain each step. The RETURN line is short.
SpeedA variable is calculated only once, even if you use it several times in the formula.
Easier testingTo check one part, change the RETURN line to return that variable, for example RETURN LastYearSales. Look at the result in a visual, then change it back.


A Variable Does Not Change

A variable is calculated where it is defined, in the filter context of the visual.

After that, its value does not change.

Even CALCULATE cannot change it:

Example

Variable Trap =
VAR AllSales = [Total Sales]
RETURN
    CALCULATE(AllSales, Stores[Region] = "Kanto")

In a card, this measure shows the grand total, 9,416,800 (shortened on the card), not the Kanto sales (3,081,150).

AllSales was already calculated before CALCULATE added the Kanto filter.

To filter a calculation, put the measure itself inside CALCULATE: CALCULATE([Total Sales], Stores[Region] = "Kanto").


What You Learned

  • VAR stores the result of an expression under a name, and RETURN gives the result of the measure.
  • Variables make long formulas easier to read, test and calculate.
  • Variable names have no spaces and cannot be the name of a table.
  • A variable is calculated once, and CALCULATE cannot change its value.


Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.