Skip to main content

Variables in DAX

Variables!!!! doesn't it sounds familiar? It is an integral part of almost every computer programming tool. Also, it plays a significant role in DAX. Let's start from square one if I introduce Var in the DAX statement that means introducing a variable. Generally, you can use variables with different combinations in DAX but there is a mandatory syntax to pass at the end of it i.e. Return where you define what value that DAX should return if the defined condition matches and the alternate result to it.

So let's clear out the ambiguity I will show how the variable works by using them in DAX under different conditions. I am considering Sample Superstore data to test the VAR. The use case is getting the West region Sales. For that, we are defining the Total Sales or Overall Sales by using the Sum of Sales and we will showcase all different regions and their respective sales.


Now, I am looking for West Region Sales. You can achieve the same by using the Calculate and filters (How to use Calculate and Filters?) but let's try to achieve it by Var. We will define a variable called "WestSales" for that.


Now, I am looking to get the percentage of sales for the west region by dividing the west sales by the overall sales. We can do this using basic calculation and dividing but let's explore how can we do the same using Var. 


As you can see we defined two variables SalesTotal and West Sales also. Let's take all the sub-categories in the matrix and let's see how much percentage of sales is contributed by West.

Yes, that's how variable functions in DAX but if you can achieve the same result without Var then why should we opt for this? Yes, you can achieve the same without Var but let's consider a scenario where you have to create lots of measures with the same fields. Generally, it occurs when you are trying to create YoY, MoM, and so on.

In such case you have to use the same field such as Sales or it can be Profit then it will be a cumbersome and repetitive task to write the same DAX again and again with a slight change. To avoid that you can use Var and define your basic DAX such as Sales last year. You can use the same variable over and over which makes the length of DAX much shorter but also improves the performance of your report. You can use the Variable to define a table or you can introduce a new calculated column using it


Pro Tip- The most important thing to keep in mind is when we are using the VAR it provides a fixed value that is not affected by a filter context.

Using VAR always proves to be beneficial as it reduces the repetitive tasks of creating the same measure over and over but you have to be very clear and decisive when you are defining the naming convention of your variables. 


Thanks for Reading  Let's connect on  LinkedIn. For more such blogs do follow us.


Comments

Popular posts from this blog

Ultimate Beginners Guide to DAX Studio

There are zillions of external tools available with Power BI but DAX Studio is one of the most commonly used tools to work with DAX queries. It is a perfect tool to optimize the DAX and the data model. In this blog let's shed some light on the basic functionalities that can take your report to the next level. ARE YOU READY?  To start you will need the latest version of the DAX Studio. You can download it from their website . Don't worry you don't have to pay for the license. Fortunately, DAX Studio is a free tool As a BI Developer, I am using DAX Studio regularly. Based on my experience I use it for several purposes but in this blog, I will highlight the most common ones. Extracting a dump of all the measures used in your PBIX. Why do we need to do this? It can be used for documentation purposes also sometimes we try to reuse the DAX and such a dump comes in handy in this scenario. How to achieve it? Open the DAX Studio it is located under the external tools once you open t

Append v/s Merge in Power BI

Let's discuss another problem of the week. As a Power BI user, there are times when you want to combine queries. What are the ways to do so? In most cases, you can attain it by using either append or merge and both serve different purposes. Let's understand what do these terms mean in Power BI and how they are functionally different from each other.  It is quite common to get data from various sources and you need to combine those data depending on a particular column which is common in both tables so that you can add extra information or column to your big table. In such cases, we use merge queries. How to perform merge queries? For instance, I am considering Sample Superstore data and we will merge the returns table to the order table. You will find both merge and append in the home tab in extreme right in the power query editor. ProTip - You will find two options when you click on the drop-down in merge which are merge queries and merge queries as new. When you use merge que

Use Relationship in DAX

Data modeling is an essential part of creating perfect visuals. While creating complex data models there can be a case where you can find an inactive relationship represented by dotted lines and it occurs because you already have an active relationship between the two tables. But as a developer, you need to use both the relationship. How can it be done? You can use "Use Relationship" in such cases. Use relationship can be added to your DAX and act as a modifier or enhancer for calculation. It activates the inactive relation. But make sure you have an inactive relationship in place before using the use relationship function. Let's see how it works on Sample Superstore data. In my fact table I have two dates- Order date and Ship date. I am making the two relations between my date table and fact table. The relation between the sample superstore (date) to date table (date) is active while the relation between the sample superstore (ship date) to date table (date) is inactive