Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
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

Implementing iterators

In DAX, iterators are functions that evaluate an expression for every row of a table and then aggregate the result. These functions are called iterators because the functions iterate over each row of a table. Within DAX, iterator functions end with an X character and include the following functions:

  • AVERAGEX
  • COUNTAX
  • COUNTX
  • GEOMEANX
  • MAXX
  • MEDIANX
  • MINX
  • PRODUCTX
  • STDEVX.P
  • STDEVX.S
  • SUMX
  • VARX.P
  • VARX.S

Each of these iterator functions performs exactly the same calculation as their non-X equivalent aggregation functions, except that the X functions perform their aggregation over a table specified as the first parameter of the function.

All of these iterator functions have the following general form:

<function>(<table>, <expression>)

Here, <function> is the name of the iterator function. Each iterator function takes a table as its first parameter as well as a DAX expression as its second parameter. The expression is evaluated for each row of the table and then the aggregation function aggregates the results of each of those evaluations.

Getting ready

To prepare for this recipe, perform the following steps:

  1. Create a table using the following formula:
R08_Table = GENERATESERIES(DATE(2020,1,1),DATE(2022,12,31))
  1. Create a column in that table using the following formula:
Year = [Value].[Year]
  1. Create a second column in that table using the following formula:
MonthNo = FORMAT([Value].[MonthNo],"00")
  1. Create a third column in that table using the following formula:
Weeknum = FORMAT(WEEKNUM([Value],1),"00")

How to do it...

To implement this recipe, perform the following steps:

  1. Create the following three measures:
Count = COUNTX('R08_Table',[Value])
Max = MAXX(R08_Table,[Year] & [MonthNo] & [Weeknum])
Min = MINX(R08_Table,[Year] & [MonthNo] & [Weeknum])
  1. Place each of these measures in its own Card visualization and note the values returned by each:
  • Count: 1096
  • Max: 20221253
  • Min: 20200101

How it works...

For the Count measure, the first parameter is again our table, R08_Table, which has a row for every day of the years 2020, 2021, and 2022. For each row, the expression simply evaluates to the value of the date in our Value column. The iterator then simply counts how many values have been returned as its aggregation, in this case, 1,096 days. That is 366 values for 2020, a leap year, and then 365 for both 2021 and 2022.

For the Max measure, the first parameter is again our table, R08_Table, which has a row for every day of the years 2020, 2021, and 2022. For each row in the table, the expression concatenates the Year, MonthNo, and Weeknum columns using the ampersand (&) concatenation operator. Once all of the values are calculated, the MAXX function then performs the aggregation step to return the maximum value calculated, in this case, 20221253, the last week of December in the year 2022.

The Min measure works in an identical fashion to the Max measure, except that the MINX function returns the minimum value calculated, in this case, 20200101, the first week of January in the year 2020.

There's more...

The first parameter to iterate functions does not have to be simply a table reference; it can actually be any DAX expression that returns a table. To demonstrate this, create the following measure:

Product = PRODUCTX(DISTINCT('R08_Table'[MonthNo]), [MonthNo]+0)

Place this measure in a Card visualization and note that the value displayed is 479M, or 479 million.

In this measure, we use the DISTINCT function to return only the unique values from the MonthNo column of our table, R08_Table. This returns the numbers 1-12. As our expression, we add 0 to the MonthNo column for each row in order to convert the expression to a numeric value from text. The PRODUCTX function then multiplies each of these unique values together. If you calculate the factorial of 12 (12!) on a calculator, the value is indeed 479,001,600, or roughly 479 million.

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 €18.99/month. Cancel anytime