Using dates in table calculations (3.30+)

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:

f54c558e950ea94c2cc0924b8a04cacea6a9e399.png

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})

8af50712524c9c29d63d9837fee4f68afc1fc706.png

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

80e48be516448d457a920839abd413272c64650a.png

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

f2fc4d27bfddbca673a003071cba809c7a555d70.png

Creating an absolute date

Dates can be created using the date function:

216f682a52c57383d10b0fae6a94f0453b84d86c.png

And datetimes/timestamps can be created using the date_time function:

bc1dbd49fcc504d293d6c6147f6c9e9f5c5a1cc1.png

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})

b80a1edb9e18310477f440404b2bac7293c0619d.png

Returning the current date

You can refer to now using the now function:

ac44005e44e861a87036b6ebed310add646bc73d.png

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())

d982b2432f83b0b048ffa8e6bf208111b6ad9f59.png

3 21 48.5K
21 REPLIES 21
Top Labels in this Space