I have two dates, and I’d like to know how much time has elapsed between these two dates. I might want to know this result in days, months, or years.
Looker’s Table Calculations offer a simple way to compute the difference between two dates on the fly. The
diff_days family of functions takes in two dates and returns the difference as an integer.
Example 1: Difference Between Two Fields
Let’s say each of my orders has a created and returned date. I can use a table calculation to compute the number of days that elapsed between purchase and return:
Example 2: Age
Let’s say each of my users has a signup date. I can use a table calculation to find out how many months have elapsed since their signup, using the
now() table calculation:
Example 3: Time Between Events
Building off the previous example, let’s say my company was founded on November 30, 2011 and that I want to find out how old my company was when each user signed up. I can use the
date table calculation to compare the signup date against a hard-coded date:
If you’re savvy with LookML and want to build a difference of dates into the LookML model, so that it can be grouped on and selected by other users, read on with our Computing the Difference between Dates (for Developers) article.