Variables & DIVIDE

Power BI · Advanced DAX & Time Intelligence

Theory

Variables let you name an intermediate result inside a DAX expression. You declare them with VAR name = expression, and finish with RETURN followed by the final expression. Each variable is evaluated once, in the context where it is defined, and then behaves like a constant, which makes long measures easier to read, avoids repeating the same calculation, and often improves performance. Variables are also very handy for debugging: temporarily RETURN a variable to see its value.

DIVIDE(numerator, denominator, alternate result) performs safe division: when the denominator is zero or blank it returns blank, or the alternate result you supply, instead of an error or infinity. For example, Avg Order Value with VAR TotalSales = [Total Sales] and VAR OrderCount = DISTINCTCOUNT(Sales[OrderID]) returns DIVIDE(TotalSales, OrderCount): North ₹3,200 / 3 = ₹1,066.67, West ₹1,200 and South ₹600.

Why we use it

Readable measures are easier to trust and maintain, and safe division prevents ugly errors in visuals when a filter leaves no rows. Both are habits interviewers look for.

When to use it

Use variables in any measure that reuses a value or has more than one logical step. Use DIVIDE for ratios where the denominator can be zero or blank. The / operator is fine when the denominator is a constant or can never be zero.

How it works

  1. Measure has several steps
  2. Declare each step as a VAR
  3. Each VAR is evaluated once in the current context
  4. Is there a division by a value that may be 0 or blank? Yes: Use DIVIDE with an optional alternate result. No: Use the / operator.
  5. RETURN the final expression
  6. Readable, safe measure

Syntax

VAR <name> = <expression>
[VAR <name2> = <expression>]
RETURN <result_expression>

DIVIDE(<numerator>, <denominator>[, <alternateresult>])

Example

Avg Order Value = 
VAR TotalSales = [Total Sales]
VAR OrderCount = DISTINCTCOUNT(Sales[OrderID])
RETURN
    DIVIDE(TotalSales, OrderCount)

// Variables are evaluated where they are defined:
Sales vs North = 
VAR NorthSales = CALCULATE([Total Sales], Sales[Region] = "North")
RETURN DIVIDE([Total Sales], NorthSales)   // West: 2,400 / 3,200 = 0.75

Key points

Power BI topics

Power BI Introduction

Getting Data

Power Query (Data Transformation)

Data Modeling

DAX (Data Analysis Expressions)

Data Visualization

Dashboards & Reports

Power BI Service

Advanced Features

Projects & Case Studies

Interview & Certification

Advanced DAX & Time Intelligence

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Power BI Dashboards & DAX

Learn data visualization and interactive dashboards step-by-step. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice DAX Online 250 business scenarios on a star schema — write a measure and compare your visual with the expected one. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Power BI  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
On screen — where to click

{{ rich.ui.title }}

{{ u.text }}
  1. {{ st.n }}{{ st.text }}
{{ rich.ui.title }}
{{ u.text }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax / Where to find it
{{ current.syntax }}
Example
{{ current.example }}
What You'll Learn — click to check off
  • {{ pt.mark }} {{ pt.text }}

Share this topic