Mastering Date Calculations with DATEADD and DATEDIFF Functions in DAX

Andrew Hubbard
Microsoft Power BI
Published in
3 min readAug 15, 2023

Introduction

In the realm of data analysis and business intelligence, mastering date calculations is a crucial skill. Whether you’re dealing with financial data, sales trends, or project timelines, understanding how to manipulate and analyse dates can unlock powerful insights. This is where the DATEADD and DATEDIFF functions in DAX (Data Analysis Expressions) come into play. These functions are essential tools that enable you to perform date-based calculations efficiently and accurately within your Power BI reports and other DAX-supported environments. In this article, we will delve into the inner workings of DATEADD and DATEDIFF, exploring their syntax, use cases, and providing illustrative examples to showcase their real-world applications.

Understanding DATEADD Function

The DATEADD function in DAX adds a specified number of units (days, months, quarters, or years) to a date. Its syntax is:

DATEADD(start_date, number_of_units, unit_type)

Here, start_date is the base date you want to start from, number_of_units is the quantity of units you want to add, and unit_type determines the type of unit to add (e.g., “day”, “month”, “quarter”, or “year”). Let’s explore some example uses:

Example 1: Calculating Future Dates

Suppose you want to project a delivery date by adding 15 days to the order date. Using the DATEADD function, you can achieve this with ease:

ProjectedDeliveryDate = DATEADD(OrderTable[OrderDate], 15, DAY)

Example 2: Rolling Averages

Calculating rolling averages is a common scenario in business analytics. To compute a 3-month rolling average of sales, you can use DATEADD like this:

RollingAverage = AVERAGEX(
FILTER(SalesTable, SalesTable[Date] <= EARLIER(SalesTable[Date])),
SalesTable[Amount]
)

Understanding DATEDIFF Function

The DATEDIFF function calculates the difference between two dates in terms of the specified unit type. Its syntax is:

DATEDIFF(start_date, end_date, unit_type)

In this function, start_date and end_date represent the two dates you want to compare, and unit_type determines the unit of measurement for the difference (e.g., “day”, “month”, “quarter”, or “year”). Here are some practical examples:

Example 1: Age Calculation

To find the age of customers, you can use DATEDIFF to calculate the difference in years between their birthdate and the current date:

CustomerAge = DATEDIFF(CustomerTable[BirthDate], TODAY(), YEAR)

Example 2: Project Duration

For project management, calculating the duration of a project in months can be essential. Consider this example:

ProjectDurationInMonths = DATEDIFF(ProjectTable[StartDate], 
ProjectTable[EndDate], MONTH)

Conclusion

In the world of data analysis, understanding and manipulating dates is a fundamental skill. We use the DATEADD and DATEDIFF functions in DAX serve as indispensable tools for performing precise date-based calculations. With DATEADD, you can effortlessly project future dates, create rolling averages, and more. Meanwhile, DATEDIFF empowers you to calculate age, project duration, and other date differences accurately. These functions equip you to tackle complex date-related challenges and extract valuable insights from your data. As you explore their capabilities and experiment with real-world scenarios, you’ll find that these functions are powerful assets in your analytical toolkit.

Don’t forget to subscribe to our:

👉 Power BI Publication

👉 Power BI Newsletter

--

--

Andrew Hubbard
Microsoft Power BI

A Data analyst using Microsoft Power BI to create visualisations. With a keen interest in mental well being.