Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying DAX Cookbook
  • Table Of Contents Toc
DAX Cookbook

DAX Cookbook

By : Greg Deckler
3.6 (11)
close
close
DAX Cookbook

DAX Cookbook

3.6 (11)
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)
close
close

Computing rolling weeks

There are a number of business analysis calculations, such as forecasting, that often require an analysis of a sliding scale of the data from previous (complete) date periods. For example, it is fairly common that the last 3 complete months' worth of data is used to forecast the current month. In other words, if today's date is April 6, 2020, then the dates desired would be from January 1, 2020 to March 31, 2020. These sliding scales must be able to cross year boundaries as well as ensure that the date ranges calculated are exact. Being off by even a single day can sometimes greatly affect forecasts.

While Power BI has a DAX rolling average quick measure, this measure only works with days, months, quarters, and years. The reason is that DAX's time intelligence generally does not support weeks, and for good reason. Dealing with weeks tends to get tricky. This recipe demonstrates how to compute calculations that utilize rolling weeks but be warned; this is probably the most complex recipe in this chapter!

Getting ready

How to do it...

To implement this recipe, perform the following steps:

  1. Create the following measure:
Rolling Week Average = 
VAR __Date = MAX('R09_Sales'[Date])
VAR __Sales = ALL('R09_Sales')
VAR __WeeksBack = 4
VAR __WeeksForward = 0
VAR __RollingWeekStartDate = __Date - 7 * __WeeksBack
VAR __RollingWeekEndDate = (__Date + 7 * __WeeksForward) - 1
RETURN
AVERAGEX(
FILTER(
__Sales,
[Date] >= __RollingWeekStartDate &&
[Date] <= __RollingWeekEndDate
),
[Value]
)
  1. Create a Line Chart visualization and place the Date column from the R09_Sales table in the Axis field and the Rolling Week Average measure in the Values field.

How it works...

We begin by initializing the date we are working with, __Date, and the table where we will be averaging values, __Sales. We then set how many weeks backward and forward that we want to include in our averages for any particular day and store these in the variables __WeeksBack and __WeeksForward, respectively.

We can use these variables to calculate the start and end dates of our rolling weeks in the __RollingWeekStartDate and __RollingWeekEndDate variables. In the case of __RollingWeekStartDate, we simply need to use date subtraction to subtract the number of __WeeksBack variable by 7 (seven days in a week). For __RollingWeekEndDate, it is a similar calculation except that we are adding days and we subtract 1 in order to not include the current date.

Once we have the dates to include, we FILTER the table to only include dates within and including our __RollingWeekStartDate and __RollingWeekEndDate variables and use AVERAGEX to average the Value column.

There's more...

Now, there are a couple of issues associated with our rolling week average calculation. First, this calculation does not look very complex at all despite the warning at the beginning of this recipe! Second, and most importantly, this really is not a rolling four week average; it is more like a rolling 28 day average. Fear not. This initial calculation is simply an example to explain the concept of rolling averages. In short, the entire crux of the issue is to calculate the start and end dates for our rolling window of dates and use those to filter our table such that only the values within that date range are included in our calculation.

What we really want, however, is to include the last four full weeks of values for any particular date. To do this, we will need some help from our Finding week start and end dates recipe. We can use that recipe to adjust our calculation for __RollingWeekStartDate and __RollingWeekEndDate . To do this, create a measure from the code located in the R09_RollingWeekAverage2.txt file in the GitHub repository.

OK. You were warned that this was complex! So, let's break this calculation down. The first four lines are largely the same except, since we are only interested in full weeks, we set our __WeeksForward variable to -1 to ensure that the last full week, and not the current week, is included in the final range of dates.

The majority of the remainder of the calculation is dominated by the creation of a calendar table variable, __Calendar. This variable contains a calendar that will be used by the subsequent steps in the calculation and is central to the entire solution. The creation of this variable begins with the creation of a calendar table that contains a range of date values based on our __WeeksBack and __WeeksForward variables. To this table we then add two columns, __WeekStarting and __WeekEnding, based on our Finding week start and end dates recipe. For a full explanation of these two columns, refer to the Finding week start and end dates recipe. There are no real changes required other than those required to fit them into the formula as essentially nested VAR statements. You should observe that any formula for computing these two columns could be inserted, such as finding the first and last working days of a week.

Once we have our calendar table with our __WeekStarting and __WeekEnding columns, we can now create the dates whose __WeekStarting and __WeekEnding dates we wish to look up. These variables are __LookupDateStart and __LookupDateEnd, which are essentially the same calculation we used earlier in our original formula for Rolling Week Average when computing __RollingWeekStartDate and __RollingWeekEndDate. This time, when computing __RollingWeekStartDate and __RollingWeekEndDate, we use the __LookupDateStart and __LookupDateEnd variables to find the corresponding dates in our __Calendar table and return either the __WeekStarting or __WeekEnding dates, respectively.

Once we have our __RollingWeekStartDate and __RollingWeekEndDate values, the remainder of our formula is the same as before.

See also

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
DAX Cookbook
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon