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
DAX Cookbook
DAX Cookbook

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

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

What do you get with a Packt Subscription?

Free for first 7 days. €18.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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 : 9781839217074
Category :
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. €18.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

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

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
Banner background image

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

Top Reviews
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
Top Reviews

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
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
Mr. D. M. Johnston Jun 30, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is simply outstanding, it’s really blown me away. So many books, concerning DAX, focus on the theory without really applying it to ‘real world scenarios’. This books flips that on its head, gives you real world scenarios and walks you through the DAX like a proper cookbook. Working with Ranking, creating a Net Promoter Score, calculating Employee Turnover Rate, aggregating duration and much more. Love it!
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
J. Lorenzo Feb 12, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
The section in 'Computing gross margin, revenue, and cost' makes this cookbook the best in its class. I highly recommend this book, especially for those writing Power BI reports with some experience in Excel. A great book to assist in the transition process from Excel to Power BI.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.