avatarArpita Ghosh

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3481

Abstract

age by Author</figcaption></figure><ul><li>Click on Load and save data.</li></ul><h1 id="0cc8">VARIABLES WITH TEXT</h1><p id="49a4">Variables can store numeric data as well as text. Using this feature, you can create a dynamic text-based measure that can be varied depending on filter selection. Let’s see one example.</p><ol><li>Create one text-based calculated measure (Sales Quantity) using variable</li><li>There are 4 variables. one for the direct measure, two for hard-coded messages and the final result variable where you are using IF clause condition and concatenate operator. Here you are using the result as a return statement.</li><li>To make the next line or new line, press Shift + Enter.</li></ol><figure id="1c73"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*JD0mqE6lj1K-pWGXHssP2w.jpeg"><figcaption>Image by Author</figcaption></figure><p id="6502">4 . Your first measure using variables has been ready for presentation.</p><p id="fbf3">5. Create one Slicer and one table to test your newly created measure.</p><p id="4859">6. For Slicer, add Order Date Hierarchy in the Field section.</p><p id="5ebc">7. For Table visual, add Order Date Hierarchy and Sales Quantity in the Values section.</p><p id="0869">8. Display report with some filter condition like 2015 as Year, Quarter as Qtr1, Month as January and observe the report result.</p><figure id="08fd"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*KabVvQzqTW5eTA4yT-3hiQ.png"><figcaption>Image by Author</figcaption></figure><h1 id="252c">VARIABLES IN CALCULATED COLUMNS</h1><p id="2b24">When you are creating variables inside calculated columns, variables automatically get access to values in other columns from the same row.</p><p id="6e05">It can be useful to display text-based data in some visuals.</p><ol><li>Create your first calculated variable based column.</li><li>Please follow the below steps to create that.</li></ol><figure id="c4de"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*KYgdJkJuyTGAZewA2QVZ7Q.png"><figcaption>Image by author</figcaption></figure><p id="937a">3. Create a Table visual to see the output.</p><figure id="7fd9"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*gjMSY0ONBbAeUofUHd1WpQ.png"><figcaption>Image by Author</figcaption></figure><h1 id="d6d8">VARIABLES IN CALCULATED MEASURES</h1><p id="8f97">To make a calculated measure faster, you can use variables.</p><p id="f6fe">However, variables in calculated measures do not relate with individual rows that mean it cannot be assigned as a column-based value. This is the key difference with variables in calculated columns.</p><p id="0458">Let us see some example.</p><ol><li>Create one calculated measure Sales > 500 with variable.</li><li>Change the number Formatting from the Whole Number to a Decimal Number with 2 decimal places.</li></ol><figure id="1cab"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*zGXBZYJcye-mZFXQKcQFMw.png"><figcaption>Image by Author</figcaption></figure><p id="56ad">3. Create your report with Customer Name, Order ID, Product ID, Sales and Sales > 500.</p><p id="bcba">4. Now analyse the report. For example, Ed Braxton placed one order for two products. For one product sales value is above 500 and another one is below 500.</p><figure id="c659"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*wQk2s6qLSyxlaA-F3BHi7g.png"><figcaption>Image by Author</figcaption></

Options

figure><h1 id="1bd1">VARIABLES IN CALCULATED TABLES</h1><p id="2079">Variables in the calculated table are useful when you are creating any summary or aggregated table from the existing table. It helps to improve the performance of the report page.</p><ol><li>Create one calculated table “Summary” using a variable.</li></ol><figure id="1703"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*M1prRj7ThBYq0YfUhr9uiA.png"><figcaption>Image by Author</figcaption></figure><p id="52a5">2. Now create two reports and compare the results whether your calculated table is working as expected or not.</p><figure id="2f7a"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*pxIGSxScCfg7qXuULFTs7g.png"><figcaption>Image by Author</figcaption></figure><h1 id="dea7">NESTED VARIABLES</h1><p id="7548">Variables can be nested and can exist within the same calculation.</p><p id="4bfd">The scope of each variable starts with a VAR statement and ends with a RETURN statement and can only be referred to by other variables which are declared at the same level or higher.</p><ol><li>Now you are going to create one calculated table using a nested variable. See the below example.</li></ol><figure id="67f8"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*weRdKPKPh8XECeisn2YXcQ.png"><figcaption>Image by Author</figcaption></figure><p id="93d1">2. It’s time to create one report using this table. You can create more detailed reports to validate your result. As well as change the value for the Level2 variable and see the difference.</p><figure id="d268"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*qdyH55_C6yet48cE9E8IIA.png"><figcaption>Image by Author</figcaption></figure><h1 id="7a25">Download</h1><p id="f2a8">Please find the code in the below location</p><p id="127b">DAX — Chapter 2 <a href="https://github.com/arpitag1/Power-BI">https://github.com/arpitag1/Power-BI</a></p><h1 id="f3e0">Video</h1> <figure id="6110"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FhHxkKfNZEr8%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DhHxkKfNZEr8&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FhHxkKfNZEr8%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" allowfullscreen="" frameborder="0" height="480" width="854"> </div> </div> </figure></iframe></div></div></figure><h1 id="c6a7">Conclusion</h1><p id="6832">In this blog, the following things have been identified</p><ol><li>What is DAX Variable</li><li>Why we need DAX Variable</li><li>Structure of Variables</li><li>Variables in Text</li><li>Variables in Calculated Column</li><li>Variables in Calculated Measure</li><li>Variables in Calculated Table</li><li>Nested Variables</li></ol><p id="2aff">In my next blog, we will learn more about DAX.</p><p id="03a3">If you have any questions related to this project, please feel free to post your comments.</p><p id="04c5">Please visit my website for other technical resources.</p><p id="5b6d"><a href="https://arpitatechcorner.com/">https://arpitatechcorner.com/</a></p><p id="09e9">Please like, comment and subscribe to my YouTube channel which you have already seen. :-) Keep Learning.</p></article></body>

How to Create Variables in DAX — DAX in Power BI — Chapter 2

Learn how to use DAX variables in Power BI to make calculations easier to understand and for improved readability, consistency and performance.

Image by Usplash

When I was studying DAX, I realized how variables are helping to improve performance, readability and reduce the complexity of any DAX calculations.

During this learning process, I put my understandings in this blog. Let’s start our journey to DAX variables.

If you have missed my previous blog about Introduction to DAX, then please have a look to refresh your idea about DAX.

What is the DAX Variable

In DAX calculation, we can use variables to make the calculations easier to understand.

When you are writing any complex or nested expression using DAX functions, variables can help to break these complex calculations into smaller, more useful sections.

Why we need DAX variable

Image by Author — Benefits of Variables

You can use variables in any form of DAX calculations and it includes calculated measures, columns and tables.

A type of variable is nothing but the calculated object type in DAX

Structure of Variable

Image by Author — Basic Structure
  1. To close a declared variable scope, there must be a RETURN statement.

VAR varA=2

RETURN varA * 2

2. You can declare multiple variables with the same layer of scope and a single RETURN statement

VAR varA=2

VAR varB=varA+2

RETURN varB*3

3. Variables can be assigned once and cannot be reassigned.

Give an error if you like below

VAR varA=2

varA=varA+2

“The RETURN keyword is used to return the value of any variable in the current scope. This can be useful when you’re debugging calculations with multiple variables. It does not have to return the last variable in the series.” — by Philip Seamark

It’s time to do some hands-on in Power BI Desktop.

Get Data

For this case study, I consider the US Superstore dataset from Kaggle.

  • Let’s start with the Get Data option under the Home tab. As this is a CSV file, select the Text/CSV option from the drop-down list
  • Select the file named US Superstore data.csv
  • After selecting the file, data will be displayed in the below format
Image by Author
  • Click on Load and save data.

VARIABLES WITH TEXT

Variables can store numeric data as well as text. Using this feature, you can create a dynamic text-based measure that can be varied depending on filter selection. Let’s see one example.

  1. Create one text-based calculated measure (Sales Quantity) using variable
  2. There are 4 variables. one for the direct measure, two for hard-coded messages and the final result variable where you are using IF clause condition and concatenate operator. Here you are using the result as a return statement.
  3. To make the next line or new line, press Shift + Enter.
Image by Author

4 . Your first measure using variables has been ready for presentation.

5. Create one Slicer and one table to test your newly created measure.

6. For Slicer, add Order Date Hierarchy in the Field section.

7. For Table visual, add Order Date Hierarchy and Sales Quantity in the Values section.

8. Display report with some filter condition like 2015 as Year, Quarter as Qtr1, Month as January and observe the report result.

Image by Author

VARIABLES IN CALCULATED COLUMNS

When you are creating variables inside calculated columns, variables automatically get access to values in other columns from the same row.

It can be useful to display text-based data in some visuals.

  1. Create your first calculated variable based column.
  2. Please follow the below steps to create that.
Image by author

3. Create a Table visual to see the output.

Image by Author

VARIABLES IN CALCULATED MEASURES

To make a calculated measure faster, you can use variables.

However, variables in calculated measures do not relate with individual rows that mean it cannot be assigned as a column-based value. This is the key difference with variables in calculated columns.

Let us see some example.

  1. Create one calculated measure Sales > 500 with variable.
  2. Change the number Formatting from the Whole Number to a Decimal Number with 2 decimal places.
Image by Author

3. Create your report with Customer Name, Order ID, Product ID, Sales and Sales > 500.

4. Now analyse the report. For example, Ed Braxton placed one order for two products. For one product sales value is above 500 and another one is below 500.

Image by Author

VARIABLES IN CALCULATED TABLES

Variables in the calculated table are useful when you are creating any summary or aggregated table from the existing table. It helps to improve the performance of the report page.

  1. Create one calculated table “Summary” using a variable.
Image by Author

2. Now create two reports and compare the results whether your calculated table is working as expected or not.

Image by Author

NESTED VARIABLES

Variables can be nested and can exist within the same calculation.

The scope of each variable starts with a VAR statement and ends with a RETURN statement and can only be referred to by other variables which are declared at the same level or higher.

  1. Now you are going to create one calculated table using a nested variable. See the below example.
Image by Author

2. It’s time to create one report using this table. You can create more detailed reports to validate your result. As well as change the value for the Level2 variable and see the difference.

Image by Author

Download

Please find the code in the below location

DAX — Chapter 2 https://github.com/arpitag1/Power-BI

Video

Conclusion

In this blog, the following things have been identified

  1. What is DAX Variable
  2. Why we need DAX Variable
  3. Structure of Variables
  4. Variables in Text
  5. Variables in Calculated Column
  6. Variables in Calculated Measure
  7. Variables in Calculated Table
  8. Nested Variables

In my next blog, we will learn more about DAX.

If you have any questions related to this project, please feel free to post your comments.

Please visit my website for other technical resources.

https://arpitatechcorner.com/

Please like, comment and subscribe to my YouTube channel which you have already seen. :-) Keep Learning.

Power Bi
Data Science
Data Analysis
Dax
Power Bi Tutorials
Recommended from ReadMedium