08.1Writing a variable

Your formulas are getting longer, and sometimes repeat the same calculation. Variables spare you that, and make the formula easier to read. Here is what they are for, then how to write them.

08.1.1What is a variable for?

A variable gives a name to a result, so you can reuse it in the rest of the formula.

This result can be scalar or tabular. In other words, a variable can hold a single value or a virtual table.

Take the revenue with a 20% tax, that is the revenue plus 20% of the revenue. Without a variable, the same calculation is written twice:

Revenue Incl. Tax = SUM( Sales[Amount] ) + SUM( Sales[Amount] ) * 0.2
Power BI accepts this DAX foryes, Measureyes, Calculated columnno, Calculated tableno, Virtual table

With variables, each calculation is written only once, and the last line reads like a sentence:

Revenue Incl. Tax = VAR ExclTax = SUM( Sales[Amount] ) VAR Tax = ExclTax * 0.2 RETURN ExclTax + Tax
Power BI accepts this DAX foryes, Measureyes, Calculated columnno, Calculated tableno, Virtual table

Both formulas return 2,040,000, that is 1,700,000 of revenue plus 340,000 of tax.

Here is a variable that holds a virtual table:

New York Customers = VAR NewYorkCustomers = FILTER( Customers, Customers[City] = "New York" ) RETURN COUNTROWS( NewYorkCustomers )
Power BI accepts this DAX foryes, Measureyes, Calculated columnno, Calculated tableno, Virtual table

The “NewYorkCustomers” variable holds the virtual table returned by FILTER, taken from the Customers table. The query view of Power BI shows this table:

This is a reproduction of the Power BI interface for teaching purposes. It differs from the real interface.

COUNTROWS counts its rows, and the measure returns a single value, 3.

Variables bring four benefits:

  • The formula is easier to read, because a clear name says what each calculation returns.
  • Variables can improve the performance of Power BI, because a calculation written only once is carried out only once.
  • Measures are easier to fix. To change a calculation, you touch only one line.
  • Variables can be checked step by step. Write a variable that holds a number or a text after RETURN to see its result. A variable that holds a table is checked in the query view.

Variables become precious when a measure is complex and is calculated in several steps. Each step then has its own line and its own name.

Take a classic example, the Pareto analysis, or 80/20 rule. It looks for the share of customers that makes 80% of the revenue. You don’t need to understand every step today. Watch above all how each step becomes a variable. Here is one way to get there, in several steps, all within a single measure:

  • Create a virtual table that groups the sales by customer.
  • Add a column to it with the share of each customer in the revenue.
  • Sort the shares, from the largest to the smallest.
  • Add a column with the running total of the shares in this order.
  • Keep only the rows whose running total is below 80%.
  • Count these rows with COUNTROWS, then add 1 for the customer who crosses the 80% mark.
  • Divide this number by the total number of customers.
  • You then have every piece of the Pareto.

Each step becomes a variable. Four customers stay below 80%, and the fifth crosses the mark. The result is 5 customers out of 7, that is 71%.

There are many other ways to calculate a Pareto, and this method is only one example.

08.1.2VAR and RETURN

A variable is declared with VAR, followed by its name, the = sign, then its calculation.

VAR <name> = <expression>

After the variables, RETURN states the result of the formula:

Average Revenue = VAR TotalRevenue = SUM( Sales[Amount] ) VAR SalesCount = COUNTROWS( Sales ) RETURN DIVIDE( TotalRevenue, SalesCount )
Power BI accepts this DAX foryes, Measureyes, Calculated columnno, Calculated tableno, Virtual table

“TotalRevenue” is 1,700,000 and “SalesCount” is 12. RETURN divides one by the other, which gives 141,666.67.

Watch out!

Without RETURN, Power BI rejects the formula. A variable is called by its name alone, without square brackets. Write “TotalRevenue,” not [TotalRevenue].

08.1.3The name of a variable

In the name of a variable, these are not allowed:

  • A space, as in “Total sales.”
  • A digit at the start, as in “1Total.”
  • Square brackets, double quotes or apostrophes, as in [Total].
  • A hyphen or another sign, as in “Total-sales” or “Total+sales” (the underscore is allowed, as in “Total_sales”).
  • A reserved DAX word, like RETURN.
  • The name of a table, like Sales.

Avoid accented letters too. Microsoft’s documentation does not provide for them in a variable name. Write “Cafe” rather than “Café.”

08.1.4No comma between variables

A comma separates the arguments of a function. VAR is not a function, so nothing separates two variables. Each variable simply starts with VAR.

VAR TotalRevenue = SUM( Sales[Amount] ) VAR SalesCount = COUNTROWS( Sales )

08.1.5Variables in IntelliSense

Once written, a variable appears in the IntelliSense suggestions. Type its first letters to find it again.

Quiz

The questions often rely on the course tables. Keep them in view in their floating window, which you can move and enlarge.

Question 1

Power BI rejects the measure VAR TotalRevenue = SUM( Sales[Amount] ), VAR SalesCount = COUNTROWS( Sales ) RETURN DIVIDE( TotalRevenue, SalesCount ). Why?

Question 2

On a card with no filter at all, what does the measure VAR ChicagoCustomers = FILTER( Customers, Customers[City] = "Chicago" ) RETURN COUNTROWS( ChicagoCustomers ) return?

Question 3

Which of these lines does Power BI accept?

Key takeaways
  • A variable gives a name to a result. The formula is easier to read, and the same calculation is not repeated.
  • VAR name = expression declares a variable, and RETURN states the result of the formula.
  • A variable name is a single word, and it does not start with a digit.
  • Nothing separates two variables, neither a comma nor a semicolon.