Chapter 14: Working with Dates, Embedding Content, and Extracting Data
14.6. DATETIME_DIFF() in Looker Studio
In this lesson, we'll dive into the DATETIME_DIFF()
function in Looker Studio, which can help you determine the difference between two dates. At the beginning of this chapter, we cover Date Function in Looker studio and in our previous example, we calculated the first rebill date based on a fixed interval by using DATETIME_ADD()
function. However, there are cases where you need a more dynamic and flexible method, such as calculating the next billing date based on the difference between the current date and the original paid membership date.
Calculating the Next Billing Date
To calculate the next billing date dynamically, use the DATETIME_DIFF()
function as follows:
1 + DATETIME_DIFF(CURRENT_DATE(), paid_member_date, 'month')
This formula calculates the difference between the current date and the date when a user became a paid member (paid_member_date
), expressing the result in months. We then add 1 to the result.
Integrating DATETIME_ADD() and DATETIME_DIFF()
Now, combine this expression with the DATETIME_ADD()
function:
DATETIME_ADD(paid_member_date, INTERVAL (1 + DATETIME_DIFF(CURRENT_DATE(), paid_member_date, 'month')) month)
This formula calculates the closest billing date after the current date. Note that this method might not work precisely when the billing date is the same as the current date. In such cases, additional logic might be required to determine the exact time of billing.
Use Case: Operational Reports
Using DATETIME_DIFF()
in conjunction with DATETIME_ADD()
is a practical solution for operational reports. For instance, this kind of report could be used to call customers and remind them about their upcoming billing dates, offering both flexibility and adaptability.
Remember to leverage Looker Studio functions like DATETIME_ADD()
and DATETIME_DIFF()
to create more dynamic and flexible date calculations that cater to specific business needs. These functions will help you generate better insights, resulting in more efficient and effective decision-making processes.
In the next lesson, we will learn how to use Extract and Date functions in Looker studio to convert timestamp or datetime fields to date format.
📩 Receive my weekly Looker Studio tips
🖇 Connect with me on LinkedIn