Book Image

DAX Cookbook

By : Greg Deckler
Book Image

DAX Cookbook

By: Greg Deckler

Overview of this book

DAX provides an extra edge by extracting key information from the data that is already present in your model. Filled with examples of practical, real-world calculations geared toward business metrics and key performance indicators, this cookbook features solutions that you can apply for your own business analysis needs. You'll learn to write various DAX expressions and functions to understand how DAX queries work. The book also covers sections on dates, time, and duration to help you deal with working days, time zones, and shifts. You'll then discover how to manipulate text and numbers to create dynamic titles and ranks, and deal with measure totals. Later, you'll explore common business metrics for finance, customers, employees, and projects. The book will also show you how to implement common industry metrics such as days of supply, mean time between failure, order cycle time and overall equipment effectiveness. In the concluding chapters, you'll learn to apply statistical formulas for covariance, kurtosis, and skewness. Finally, you'll explore advanced DAX patterns for interpolation, inverse aggregators, inverse slicers, and even forecasting with a deseasonalized correlation coefficient. By the end of this book, you'll have the skills you need to use DAX's functionality and flexibility in business intelligence and data analytics.
Table of Contents (15 chapters)

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:

  • MAXX
  • MINX
  • 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: