As of Looker 3.30, it is possible to perform date operations in table calculations. This includes subtracting dates, extracting dateparts from dates, creating an absolute date, and returning the current date.
Subtracting dates
Dates can be subtracting by using of the functions starting with diff_
. You can subtract the number of seconds, minutes, hours, days, months, or years between the two dates using one of the following functions:
diff_days
diff_hours
diff_minutes
diff_months
diff_seconds
diff_years
Each follows this pattern:
For example, this can be used to determine number of days between User Created Date and First Order Date like so:
diff_days(${users.created_date}, ${users_orders_facts.first_order_date})
Extracting portions of dates
Functions starting with extract_
can be used to extract a particular datepart from a date. You can extract the seconds, minutes, hours, day, month, or year of a date using one of the following functions:
extract_days
extract_hours
extract_minutes
extract_months
extract_seconds
extract_years
For example, this could be used to highlight only orders that occurred on the first of the month like so:
extract_days(${orders.created_time}) = 1
Creating an absolute date
Dates can be created using the date
function:
And datetimes/timestamps can be created using the date_time
function:
This enables you to compare a date in your database to any particular date you have chosen. For example, if you wanted to see how many minutes each order on a particular day occurred after a sale was announced.
diff_minutes(date_time(2015,09,17,9,00,00),${orders.created_time})
Returning the current date
You can refer to now using the now
function:
This is particularly useful for calculating how long ago a particular date was. For example, if you have a First Order Date field, you can use diff_date
and now
to determine how many days it’s been since a users’ first order date
diff_days(${users_orders_facts.first_order_date}, now())