Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
DAX Cookbook

You're reading from   DAX Cookbook Over 120 recipes to enhance your business with analytics, reporting, and business intelligence

Arrow left icon
Product type Paperback
Published in Mar 2020
Publisher Packt
ISBN-13 9781839217074
Length 552 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Greg Deckler Greg Deckler
Author Profile Icon Greg Deckler
Greg Deckler
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Thinking in DAX 2. Dealing with Dates and Calendars FREE CHAPTER 3. Tangling with Time and Duration 4. Transforming Text and Numbers 5. Figuring Financial Rates and Revenues 6. Computing Customer KPIs 7. Evaluating Employment Measures 8. Processing Project Performance 9. Calculating Common Industry Metrics 10. Using Uncommon DAX Patterns 11. Solving Statistical and Mathematical Formulas 12. Applying Advanced DAX Patterns 13. Debugging and Optimizing DAX 14. Other Books You May Enjoy

Using variables

While DAX functions can be nearly infinitely nested, using variables can avoid doing the same calculation multiple times and also improves overall code readability. Using variables can help you break complex calculations down into smaller, more consumable pieces that can be individually verified step by step.

Getting ready

To prepare for this recipe, perform the following steps:

  1. Create a table using the following formula:
R3_Table = GENERATESERIES(1,30,1)
  1. Create a column in that table using the following formula:
Column = ROUNDUP('R3_Table'[Value]/11,0)

How to do it...

Variables are created by using the VAR statement with the following syntax:

VAR <name> = <expression>

<name> is the name of the variable. Variable names must begin with the letters a-z or A-Z. The only other supported characters within variable names are the characters 0-9. Existing table names and certain keywords are not permitted as variable names.

DAX calculations that use a VAR statement must also use a RETURN statement.

By following these principles, we can write a DAX measure as follows:

Variable DAX = 
/*
* This measure summarizes the table, Table, grouping by [Column] and summing [Column1]. This
* summarized table is then filtered to values 2 and 3 in [Column] and then sums up [Column1]
*
* Gregory J. Deckler
* gdeckler@fusionalliance.com
* 10/7/2019
*/
VAR __summarizedTable = // Summarize table by [Column], summing [Value]
SUMMARIZE(
'R03_Table',
'R03_Table'[Column],
"__Value",
SUM( 'R03_Table'[Value] )
)
VAR __filteredTable = // Filter summarized table for 2 and 3
FILTER(
__summarizedTable, // Here we use our __summarizedTable variable
[Column] = 2
||
[Column] = 3
)
VAR __sum = // Sum [__Value]
SUMX(
__filteredTable, // Here we use our __filteredTable variable
[__Value]
)
RETURN // If result is < 400, return the sum, otherwise -1
IF(
__sum < 400, // We avoid having to do the same calculation twice
__sum,
-1
)

How it works...

The code creates three variables, __summarizedTable, __filteredTable, and __sum. The __summarizedTable variable creates a table in memory using the SUMMARIZE function to return a table summarized by [Column] and by summing the [Value] column. This variable takes on the value of the table as shown:

Column

__Value

1

66

2

187

3

212

The __filteredTable variable uses the FILTER function to filter the table represented by the __summarizedTable variable to just the values 2 and 3 in the [Column] column. This variable takes on the value of the table as shown:

Column

__Value

2

187

3

212

The __sum variable uses the SUMX function to sum the [__Value] column of the two remaining rows in the table, taking on a value of 187 + 212, or 399.

The RETURN statement uses an IF statement. This IF statement checks to see whether the __sum variable is less than 400. If the variable is less than 400, the value stored in the __sum variable is returned. Otherwise, the value -1 is returned.

Variables can only be referenced within the DAX calculation in which they are created. As shown in both __filteredTable and __sum, variables can refer to previously created variables within the same DAX calculation.

There's more...

You can nest variables. For example, this recipe could be written as follows:

Nested Variable DAX = 
/*
* This measure summarizes the table, Table, grouping by [Column] and summing [Column1]. This
* summarized table is then filtered to values 2 and 3 in [Column] and then sums up [Column1]
*
* Gregory J. Deckler
* gdeckler@fusionalliance.com
* 10/7/2019
*/
VAR __sum = // Sum [__Value]
SUMX(
VAR __filteredTable =
FILTER( // Filter summarized table for 2 and 3
VAR __summarizedTable = // Summarize table by [Column], summing [Value]
SUMMARIZE(
'R03_Table',
'R03_Table'[Column],
"__Value",
SUM( 'R03_Table'[Value] )
)
RETURN __summarizedTable // Return the summarized table
,
[Column] = 2
||
[Column] = 3
)
RETURN __filteredTable // Return the filtered table
,
[__Value]
)
RETURN // If result is < 400, return the sum, otherwise -1
IF(
__sum < 400,
__sum,
-1
)

See also

For more details regarding this recipe, refer to the following links:

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image