Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.

Reply
Anonymous
Not applicable

Variables? Why?

Hi all - do you ever find yourself using Variables in DAX when it's not 100% neccesary just so the DAX looks nice? 

 

I'm noticing that using variables makes the code look so much cleaner

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Thanks for the reply, sir. Quick question about #5 Helps in creating virtual table which can be used in optimizing the cod"

When do you see yourself creating virtual tables, why do you do it, and how does it help you. I've seen so many videos of folks doing this, but I've never had to use it myself. I'd usually create customized tables in Power Query - though I know a lot of people create these virtual tables with DAX. 

Can't you still aggregate the same way without virtual tables?

View solution in original post

2 REPLIES 2
AntrikshSharma
Super User
Super User

@Anonymous Yup, I use variables even if the code without variables is still shorter and readable enough. However I have my own reasons to use variables everytime.

 

1. Readability

2. I know that execution happens only once

3. I know that execution happens only where I have declared the variable

4. I can reuse the value again and again

5. Helps in creating virtual table which can be used in optimizing the code

6. Helps in switching between Strict and Eager evaluation which can also be done with IF.EAGER but Variables improve readability

7. Query plan generated with Variables is much more readable and easy to understand, example below, just by looking at the Logical Query Plan I can decipher where a variable is declared and where it is used.

 

AntrikshSharma_0-1632423673628.png

 

Anonymous
Not applicable

Thanks for the reply, sir. Quick question about #5 Helps in creating virtual table which can be used in optimizing the cod"

When do you see yourself creating virtual tables, why do you do it, and how does it help you. I've seen so many videos of folks doing this, but I've never had to use it myself. I'd usually create customized tables in Power Query - though I know a lot of people create these virtual tables with DAX. 

Can't you still aggregate the same way without virtual tables?

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.