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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. 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
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.