The Google Data Studio Function: DATE_DIFF
Insightful data analysis often involves playing with dates - calculating periods, keeping track of intervals, and most importantly, understanding time differences. Google Data Studio provides users with a robust function called DATE_DIFF which works seamlessly to calculate the difference between two dates.
However, DATE_DIFF is restricted to compatibility mode dates only, meaning it cannot handle newer Date or Date & Time types. For such dates, Google recommends using the DATETIME_DIFF function instead.
To make the most of the DATE_DIFF function, knowing the function's syntax is important.
The standard format for DATE_DIFF is:
DATE_DIFF(X, Y)
Here, both X and Y should ideally be date fields or expressions. The result of DATE_DIFF is calculated as X - Y. Therefore, if X is later than Y, the result will be positive. If X is earlier than Y, the function will generate a negative result.
Implementing the DATE_DIFF function is a process that involves primarily two parameters - X and Y, which represent the end date and the start date, respectively.
Consider the standard
DATE_DIFF(End Date, Start Date)
format. Here, you are essentially calculating the number of days between the start date and the end date. Any date field or expression can be (effectively) swapped in for parameters X and Y.
To appreciate the utility of DATE_DIFF, consider the following examples based on sales metrics:
Suppose you execute a sales campaign from March 1, 2021, to March 31, 2021. You can calculate the campaign length using
DATE_DIFF("2021-03-31", "2021-03-01")
. The function will return 30, indicating the campaign lasted 30 days.
If a product was launched on January 1, 2021, and the date today is April 20, 2021, you can calculate how long the product has been in the market using
DATE_DIFF("2021-04-20", "2021-01-01")
. The function will return 109.
If you want to calculate the time between the sales of two batches of a product where the first batch was sold on February 10, 2021, and the second batch was sold on April 10, 2021, you would use
DATE_DIFF("2021-04-10","2021-02-10")
. The function will return 59, indicating that 59 days passed between the two sales.
The key limitation of DATE_DIFF is that it only supports compatibility mode dates. Users with more recent date types may not be able to use this function effectively.
While using DATE_DIFF, always ensure that parameter X (the end date) is later than parameter Y (the start date). Also, you should consider upgrading your date fields to the newer Date or Date & Time types to avail of full functionality.
Become a Looker Studio expert
for free
Learn all the secrets of data analysis and create beautiful and effective dashboards thanks to our 30-second video courses! Join Catchr's community on YouTube.
Get more video lessonsAll your data on Looker Studio
Build Looker Studio dashboard easily with your marketing data from all platforms and track your essential KPIs! No-code integration.
Start free trial now!Optimize your data analysis
Get free Looker Studio dashboard template among a large collection of +50 stunning template! Elevate your data visualization.
Get free templates!