Variables with VAR

Variables make long measures easy to follow. Each variable holds a named intermediate result, and RETURN gives back the result. That helps with reading, with performance and with finding errors.

In short: VAR name = expression, as often as you like, then RETURN result. A variable is calculated once, in the filter context where it is written, and is a fixed value from then on.

Before this: CALCULATE.

Try it now

Task Create the measure "Revenue per Unit" with two variables: Revenue for [Total Sales], Units for the sum of Sales[Quantity], and return DIVIDE(Revenue, Units).

Sales 120 rows, 7 columns
OrderIDOrderDateShipDateProductIDRegionIDQuantityAmount
13 Oct 20266 Oct 20261221798
29 Nov 202519 Nov 20252351145
320 Sep 202627 Sep 2026544596
416 Jun 20252 Jul 20252151145
526 Mar 202631 Mar 20261343596
64 Dec 202510 Dec 2025545745
78 Jul 202526 Jul 2025614196
820 Feb 20251 Mar 202532359.7
99 Dec 202524 Dec 202562298
1012 Aug 202625 Aug 2026212458
117 Sep 202612 Sep 2026542298
122 Mar 202614 Mar 2026623147

Showing the first 12 of 120 rows.

Measures: [Total Sales]

Product 6 rows, 4 columns
ProductIDProductCategoryPrice
1LaptopHardware899
2MonitorHardware229
3MouseAccessories19.9
4KeyboardAccessories39.9
5Office suiteSoftware149
6AntivirusSoftware49
Region 4 rows, 2 columns
RegionIDRegion
1North
2South
3West
4East
Date 730 rows, 4 columns
DateYearMonthMonthName
1 Jan 202520251Jan
2 Jan 202520251Jan
3 Jan 202520251Jan
4 Jan 202520251Jan
5 Jan 202520251Jan
6 Jan 202520251Jan
7 Jan 202520251Jan
8 Jan 202520251Jan
9 Jan 202520251Jan
10 Jan 202520251Jan
11 Jan 202520251Jan
12 Jan 202520251Jan

Showing the first 12 of 730 rows.

Structure

Revenue per Unit =
VAR Revenue = [Total Sales]
VAR Units = SUM(Sales[Quantity])
RETURN DIVIDE(Revenue, Units)

Line breaks make the formula readable; in the Power BI formula bar you add them with Shift and Enter. Variable names consist only of the letters a to z, digits and the underscore, with no spaces and no accented letters. The measure name before the equals sign, on the other hand, may contain spaces and any characters. Two variables in the same block must not share a name.

Why variables?

  • Readability: every step has a name.
  • Speed: if a part appears several times, Power BI calculates it only once with a variable.
  • Debugging: RETURN Units instead of the result shows in the matrix what the variable holds in each cell.

When the value is fixed

A variable is calculated where it is written, with the cell's filters. If a CALCULATE changes the filters later, the variable stays the same. CALCULATE(Revenue, Product[Category] = "Hardware") with a variable Revenue therefore does not return hardware sales but the same value as Revenue. The filter then belongs inside the variable itself.

Find the error

Task Find the error: the measure should return the average quantity per order, and both variables are already there. Power BI reports an error, though.

Sales 120 rows, 7 columns
OrderIDOrderDateShipDateProductIDRegionIDQuantityAmount
13 Oct 20266 Oct 20261221798
29 Nov 202519 Nov 20252351145
320 Sep 202627 Sep 2026544596
416 Jun 20252 Jul 20252151145
526 Mar 202631 Mar 20261343596
64 Dec 202510 Dec 2025545745
78 Jul 202526 Jul 2025614196
820 Feb 20251 Mar 202532359.7
99 Dec 202524 Dec 202562298
1012 Aug 202625 Aug 2026212458
117 Sep 202612 Sep 2026542298
122 Mar 202614 Mar 2026623147

Showing the first 12 of 120 rows.

Measures: [Total Sales]

Product 6 rows, 4 columns
ProductIDProductCategoryPrice
1LaptopHardware899
2MonitorHardware229
3MouseAccessories19.9
4KeyboardAccessories39.9
5Office suiteSoftware149
6AntivirusSoftware49
Region 4 rows, 2 columns
RegionIDRegion
1North
2South
3West
4East
Date 730 rows, 4 columns
DateYearMonthMonthName
1 Jan 202520251Jan
2 Jan 202520251Jan
3 Jan 202520251Jan
4 Jan 202520251Jan
5 Jan 202520251Jan
6 Jan 202520251Jan
7 Jan 202520251Jan
8 Jan 202520251Jan
9 Jan 202520251Jan
10 Jan 202520251Jan
11 Jan 202520251Jan
12 Jan 202520251Jan

Showing the first 12 of 730 rows.

This exercise is part of Premium.

Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.

See Premium Already Premium? Log in

The first exercise in this lesson is free.

In real Power BI

  1. Create a new measure and, after the equals sign, start a new line with Shift and Enter.
  2. Put each variable on its own line and RETURN on the last one.
  3. For testing, temporarily point RETURN at one variable and look at the matrix.

Common mistakes

Forgetting RETURN

Without RETURN, Power BI reports a syntax error at the end of the formula.

Using a variable before it is defined

A variable only exists from its own line onwards. Referring to the second VAR inside the first one gives an error that the name cannot be resolved.

FAQ

Can a variable hold a table?

Yes, for example VAR Hardware = FILTER(Product, Product[Category] = "Hardware"). You work with that in level 5.

Do calculated columns have variables too?

Yes, with the same structure. They are then calculated per row.

Up next: KPI measures project

Four measures for a KPI page, using everything from level 4.

Go to the project

Write to us

Choose language

Select your preferred language