Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
DAX Cookbook
DAX Cookbook

DAX Cookbook: Over 120 recipes to enhance your business with analytics, reporting, and business intelligence

eBook
€20.98 €29.99
Paperback
€36.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
Table of content icon View table of contents Preview book icon Preview Book

DAX Cookbook

Dealing with Dates and Calendars

It is a rare business scenario and data model that does not have something related to dates at its core. So much of the business world deals with dates. People work on certain dates and not others. Deadlines are due on specific days. Bonuses and key performance indicators are dependent upon what week, month, or quarter sales occur. This chapter is all about working with dates, weeks, months, quarters, and years.

This chapter comprises the following recipes:

  • Using time intelligence
  • Creating quarters
  • Calculating leap years
  • Determining day and working day numbers in a year
  • Determining date of the day number of a year
  • Finding week start and end dates
  • Finding working days for weeks, months, quarters, and years
  • Constructing a sequential week number
  • Computing rolling weeks
  • Replacing Excel's NETWORKDAYS function
  • Working with date intervals
  • Exploiting...

Technical requirements

Using time intelligence

Time intelligence in DAX is somewhat of a misnomer. Time intelligence actually does not deal with time in the sense of hours, minutes, seconds, and so on. Instead, time intelligence would be better off being called date intelligence or calendar intelligence, because the time intelligence functions in DAX really deal with dates. Hence, if you are looking for calculations involving time in the sense of hours, minutes, and seconds, proceed to the next chapter. Otherwise, this recipe will demonstrate how to use the time intelligence functions in DAX to perform calculations related to things such as year-over-year, month-over-month, and quarter-to-date.

Getting ready

To prepare for this recipe, perform the...

Creating quarters

You may consider it somewhat silly to have a recipe for determining the quarter of a date since quarter is included in the default hierarchy for a date. However, many organizations do not adhere to a standard calendar quarter. For example, Microsoft's fiscal calendar runs from June to May. Other organizations start and stop their quarters on specific dates of the year. This quarter calculation can be used with standard quarterly calendars as well as non-standard quarterly calendars. It is easily modified to fit just about any custom quarterly schedule you can imagine. The quarter table that we will create in this recipe has the following quarters:

  • Q1: August 15 November 14
  • Q2: November 15 February 14
  • Q3: February 15 May 14
  • Q4: May 15 – August 14
...

Calculating leap years

A leap year is defined as a year that is evenly divisible by 4, but not evenly divisible by 100 unless that year is also evenly divisible by 400. Confused? This is why this recipe exists! This recipe calculates whether or not a year is a leap year and the number of days in a year.

Getting ready

To prepare for this recipe, perform the following steps:

  1. Create a table using the following formula:
R03_Years = GENERATESERIES(1899,3000,1)

How to do it...

To implement this recipe, perform the following steps:

  1. Create the following columns in the R03_Years...

Determining day and working day numbers in a year

There are many business scenarios where you need to know the sequential day number of the year or the sequential working day number of the year. For example, February 1 would be the 32nd day of the year (since January has 31 days). This recipe provides calculations for finding this sequential day number within a year as well as the sequential working day number within a year (excludes weekends).

Getting ready

To prepare for this recipe, perform the following steps:

  1. Create a table using the following formula:
R04_Calendar = CALENDAR(DATE(2018,1,1),DATE(2022,12,31))

How to do it...

Determining date of the day number of a year

This recipe comes in handy if you know the day number of a year and wish to determine the actual date of that day.

Getting ready

To prepare for this recipe, perform the following steps:

  1. Create a table using the following formula:
R05_Calendar = CALENDAR(DATE(2018,1,1),DATE(2022,12,31))
  1. In this table, create the following column:
DayNoOfYear = ('R05_Calendar'[Date] - DATE(YEAR('R05_Calendar'[Date]), 1, 1 )) * 1 + 1

How to do it...

To implement this recipe, perform the following steps:

  1. Create a column...

Finding week start and end dates

Many organizations have the concept of week starting and week ending, especially when dealing with time tracking or reporting. This recipe provides a way to calculate the week start and end dates for any date within a calendar year.

Getting ready

To prepare for this recipe, perform the following steps:

  1. Create a table using the following formula:
R06_Calendar = CALENDAR(DATE(2018,1,1),DATE(2022,12,31))
  1. Create a column in this table using the following formula:
Weekday = FORMAT([Date],"dddd")

The Weekday column is simply for reference and checking; it is not a required part of the recipe.

...

Finding working days for weeks, months, quarters, and years

Finding the first and last days of a month in DAX is fairly straightforward. The starting day of a month is always 1 and DAX includes a handy EOMONTH function to return the last day of a month. Similarly, finding the first and last days of a year is extremely straightforward considering that years always begin on January 1 and end on December 31. Nevertheless, things become trickier when attempting to find the first and last day of a week and become much, much trickier when trying to identify the first working day of a week, month, or year.

However, since most businesses have the concept of a work week that includes work days and non-work days, it is often important to be able to identify the first and last working days of weeks, months, and years. Luckily, this recipe shows exactly how to accomplish finding the first...

Constructing a sequential week number

While perhaps not obviously useful on its own, there are many circumstances where having a sequential number across weeks, months, quarters, and years can facilitate other calculations. This recipe demonstrates how to construct a sequential week number across years.

Getting ready

To prepare for this recipe, perform the following steps:

  1. Create a table using the following formula:
R08_Calendar = CALENDAR(DATE(2018,1,1),DATE(2022,12,31))

How to do it...

To implement this recipe, perform the following steps:

  1. Create the following column...

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...

Replacing Excel's NETWORKDAYS function

Excel has a NETWORKDAYS function that calculates the number of days between two dates minus weekends and holidays. While DAX has a DATEDIFF function that calculates the number of days between two dates, the DATEDIFF function does not account for subtracting weekends and holidays. This recipe is a recreation of Excel's NETWORKDAYS function in DAX.

Getting ready

To prepare for this recipe, perform the following steps:

  1. Use an Enter Data query to create a table called R10_Table with the following columns and rows:

Created Date

Finished Date

11/17/2019

12/29/2019

12/15/2019

1/13/2020

1/12/2020

3/30/2020

  1. Ensure that both columns are set to have a data...

Working with date intervals

There are many scenarios involving dates where you have two or more sets of dates as items or transactions move through a process. It is often desirable to know how many items or transactions are in one state or another at any given time. Unfortunately, this is something that is not straightforward to present in a report given that, with just the raw data, it is difficult, if not impossible, to depict how and when items and transactions transitioned from one state to another.

This recipe presents a simple scenario where help tickets are opened on one date and then closed. This recipe demonstrates how to see how many tickets are in process (open) at different date intervals, such as by year, month, and day.

Getting ready

...

Exploiting alternatives to DAX's time intelligence

The problem with DAX's time intelligence functions, other than the fact that they are ill-named, is that, well, they really are not all that intelligent. You see, all of these functions proceed from the same basic assumption that everything works on the basis of a standard calendar year. However, this is not the case for many businesses, including Microsoft! Thus, this underlying assumption of a standard calendar year makes any DAX time intelligence functions dealing with quarters particularly useless. In addition, many of the time intelligence functions are not supported in DirectQuery mode, which, again, makes them entirely useless in such scenarios.

But fear not, as I have mentioned, the vast majority of DAX's time intelligence functions are really not all that intelligent. In fact, they are really simple shorthand...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Learn to write sophisticated DAX queries to solve business intelligence and data analytics challenges
  • Handle performance issues and optimization within the data model, DAX calculations and more
  • Solve business issues with Microsoft Excel, Power BI, and SQL Server using DAX queries

Description

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.

Who is this book for?

Business users, BI developers, data analysts, and SQL users who are looking for solutions to the challenges faced while solving analytical operations using DAX techniques and patterns will find this book useful. Basic knowledge of the DAX language and Microsoft services is mandatory.

What you will learn

  • Understand how to create common calculations for dates, time, and duration
  • Create key performance indicators (KPIs) and other business calculations
  • Develop general DAX calculations that deal with text and numbers
  • Discover new ideas and time-saving techniques for better calculations and models
  • Perform advanced DAX calculations for solving statistical measures and other mathematical formulas
  • Handle errors in DAX and learn how to debug DAX calculations
  • Understand how to optimize your data models

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Mar 18, 2020
Length: 552 pages
Edition : 1st
Language : English
ISBN-13 : 9781839215223
Category :
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning

Product Details

Publication date : Mar 18, 2020
Length: 552 pages
Edition : 1st
Language : English
ISBN-13 : 9781839215223
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 103.97
Hands-On Business Intelligence with DAX
€29.99
DAX Cookbook
€36.99
Power Query Cookbook
€36.99
Total 103.97 Stars icon

Table of Contents

14 Chapters
Thinking in DAX Chevron down icon Chevron up icon
Dealing with Dates and Calendars Chevron down icon Chevron up icon
Tangling with Time and Duration Chevron down icon Chevron up icon
Transforming Text and Numbers Chevron down icon Chevron up icon
Figuring Financial Rates and Revenues Chevron down icon Chevron up icon
Computing Customer KPIs Chevron down icon Chevron up icon
Evaluating Employment Measures Chevron down icon Chevron up icon
Processing Project Performance Chevron down icon Chevron up icon
Calculating Common Industry Metrics Chevron down icon Chevron up icon
Using Uncommon DAX Patterns Chevron down icon Chevron up icon
Solving Statistical and Mathematical Formulas Chevron down icon Chevron up icon
Applying Advanced DAX Patterns Chevron down icon Chevron up icon
Debugging and Optimizing DAX Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Most Recent
Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.8
(12 Ratings)
5 star 41.7%
4 star 16.7%
3 star 25%
2 star 8.3%
1 star 8.3%
Filter icon Filter
Most Recent

Filter reviews by




Robin Jun 08, 2024
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This is a very clear and concise set of examples that are great for learning DAX and as a reference for experts. The time and dedication that Greg puts into his work is obvious.
Feefo Verified review Feefo
M. Short Oct 05, 2022
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
Quite difficult to read compared to plenty of other tomes. Sometimes it is quite basic, yet other times it assumes a high level of knowledge that you have to go away and do some research on, in order to understand what they are driving at. Works for me, albeit a slow, hard slog. Could be a lot better and certainly not a beginners' book.
Amazon Verified review Amazon
michael mitchell Jun 27, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
The DAX COOKBOOK helped me venture into Advanced DAX concepts right from the start of the book. Based on what I read, I knew I had to improve my DAX skills from writing sample calculated columns and simple measures. I appreciated this book because, in the first chapters, the author worked mainly with calendars and calendar functions. In those chapters, he worked with scalar functions, logical functions, joins, and it also incorporated heavy utilization of variables throughout the chapters. The author incorporated work with table functions nested into filters. He also used generate series a lot throughout the book to create tables. During the reading of the book, I also watched third party videos on DAX to quantify the concepts of this book, and based on his teaching on advanced time intelligence, relationship functions, and other advanced theories all presented at the beginning of the book; I would recommend the user have advanced knowledge of DAX before reading this book.I would have appreciated a video series to accompany this book because the author used some concepts or presentations of the DAX code which was unfamiliar to me at first glance. I also believed on the cover of the book the author should've put Advanced DAX COOKBOOK because I don't believe a beginner would understand some of the complex coding languages of this book. I also had to use multiple monitors to read the dialogue of this book to understand the coding language in the above paragraph. I also wanted to disclose "Disclosure: I received a sample product from Packt to judge the content distributed by the author. I would recommend this book only to intermediate and advanced students; beginners should advance their skills before picking up this book.
Amazon Verified review Amazon
Amazon Customer Jun 19, 2021
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
The author uses the deprecated EARLY() and EARLIEST() functions quite a bit. Also, many of the samples are wildly inefficient and therefore not practical on large datasets. The solitions are frequently inelegant and or rely on creating quite a few new nested tables.The author does tackle some fairly complicated statistical analysis problems.It's an ok book if you want to see DAX examples, but be advised that some of them are not optimal and parts of the code are dated. This book is due for an update. It's not worth the $45 paperback price.
Amazon Verified review Amazon
Guy Roland Mar 22, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I discovered this book thanks to my packt subscription.It’s so useful that I’ve decided to buy the physical one. The book is well designed and the paper is very good quality.The content of the book is a treasure! Before reading it, you should have little experience with dax in my opinion.The techniques taught in this book are diverse and very useful. Even when you think to master a concept, you’ll discover new use cases!I highly recommend this very practical book.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.