Time Metrics
Time Metrics is in Beta - reach out to support@honeydew.ai to enable in your account
Introduction
Modeling time based metrics is a common need of semantic layers. The Time Metric accessor is a wrapper around Honeydew metrics to allow easily defining time metrics.
Time Metrics require a Time Spine entity named date
.
The time metric allows to extend a pure aggregation with time properties - grain, offest, join field to a time spine and more.
Time Metrics
This creates a time metric from an aggregation:
Parameters:
- expression: either a Honeydew metric or aggregation expression. Must not include a GROUP BY.
- offset: based on Snowflake interval expression syntax, sets a negative interval to offset the grain. Note that if has a higher grain than the chosen
grain
(i.e. a week interval on a monthly metric) it might get truncated to the chosen grain. - grain: based on Snowflake date or time part syntax - day/week/month/quarter/year. Sets the minimal grain of a metric.
- date_field: a field that is used to join to the time spine. When combined with
grain
a date_field is truncated to the grain. - date_range_start, date_range_end: a pair of fields that are used to join to the time spine.
All parameters (except for expression) are optional. However, a time metric is valid only with join configuration (date_field
or date_range_*
) set.
Use date_field
when each event is counted once at the associated grain.
Use date_range_start
, date_range_end
when events are counted in each matching date rage - for example cumulative metrics or grain-to-date metrics.
See examples below for different use cases.
date_range_start
and date_field
fields are mutually exlcusive.
Example
Consider the following metric sum_qty
in lineitem
entity:
Time metrics connect automatically to the time spine entity date
.
However sum_qty
is not a time metric.
A query that groups it by date.year
will have a result such as
date.year | lineitem.sum_qty |
---|---|
NULL | 153,078,795 |
A regular metric does not know how to relate to a time spine, thus will receive NULL as is the value for date fields.
If a time metric named sum_qty_when_shipped
, that counts shipped quantity, is built of top of it
Then connecting it to date
will be done by joining it to the l_shipdate
column, attributing all quantity to the shipping date
date.year | lineitem.sum_qty_when_shipped |
---|---|
1992-01-01 | 19,305,356 |
1993-01-01 | 23,184,525 |
1994-01-01 | 23,189,319 |
1995-01-01 | 23,343,871 |
1996-01-01 | 23,307,638 |
1997-01-01 | 23,247,128 |
1998-01-01 | 17,500,958 |
However, a different metric sum_qty_when_received
may count quantity to be shipped by counting quantity on the date an order was received:
Comparing the two would show that shipped quantity is less than the quantity to be shipped at the first year, and then follows:
date.year | lineitem.sum_qty_when_shipped | lineitem.sum_qty_when_received |
---|---|---|
1992-01-01 | 19,305,356 | 18,326,418 |
1993-01-01 | 23,184,525 | 23,177,759 |
1994-01-01 | 23,189,319 | 23,198,628 |
1995-01-01 | 23,343,871 | 23,338,423 |
1996-01-01 | 23,307,638 | 23,294,742 |
1997-01-01 | 23,247,128 | 23,250,072 |
1998-01-01 | 17,500,958 | 18,484,509 |
May also use time metrics to look at a previous time period. For example, the following metric sum_qty_when_shipped_prev_year
date.year | lineitem.sum_qty_when_shipped | lineitem.sum_qty_when_shipped_prev_year |
---|---|---|
1992-01-01 | 19,305,356 | NULL |
1993-01-01 | 23,184,525 | 19,305,356 |
1994-01-01 | 23,189,319 | 23,184,525 |
1995-01-01 | 23,343,871 | 23,189,319 |
1996-01-01 | 23,307,638 | 23,343,871 |
1997-01-01 | 23,247,128 | 23,307,638 |
1998-01-01 | 17,500,958 | 23,247,128 |
1999-01-01 | NULL | 17,500,958 |
This can be used for YoY growth calculations (note to coalesce the null to zero).
Composing Time Metrics
Entity default date field
The sum_qty
metric in the lineitem
entity is not a time metric. When joined to a time spine it will not connect:
date.year | lineitem.sum_qty |
---|---|
NULL | 153,078,795 |
However adding a default date field in an entity would make all metrics in that entity to become time metrics over the default date field.
For example, the lineitem
entity can add as a default date field the lineitem.l_shipdate
:
Then the sum_qty
metric knows how to join on time
date.year | lineitem.sum_qty |
---|---|
1992-01-01 | 19,305,356 |
1993-01-01 | 23,184,525 |
… |
Default date field will use the grain defined in the referenced field.
Use the default time spine when all metrics on an entity are always counted on the same date field. That makes time metrics natural and automatic.
However, cases where multiple date fields are possible (such as lineitem
that has both a shipping date and a order received date) are less suitable to setting a default date field, and it is recommended to use exlicit date fields in metrics.
Combining qualifiers
It is possible to compose time metrics by combining qualifiers, i.e.
Note that a time metric is computable only when has both date_field
and grain
set.
Common Examples
Revenue on close date
A metric that sets the time spine, but not a grain.
Note this can be expanded further with a TIME_METRIC
that just adds a grain without changing the date_field
Revenue based on a different time spine
Monthly revenue
Note this can be expanded further with a TIME_METRIC that just adds a date_field without changing the grain
Monthly revenue at previous month
Monthly revenue at next month
Offset is applied by default as an interval to the past (previous) period. By reversing it can look at the future (next) period.
Same time last year
Balance on the last day of the month
Uses the MAX_BY aggregation to take the value of the last date in the group
Revenue of the 2nd week of the month
Note this is a monthly grain metric (there is a value per month that counts only on 2nd week)
MoM growth
Daily contribution
Monthly contribution to monthly total
Monthly contribution to city
Generates
City | Month | Revenue | Contribution |
---|---|---|---|
Tel Aviv | Jan | 10 | 10/60 |
Tel Aviv | Feb | 20 | 20/60 |
Tel Aviv | March | 30 | 30/60 |
London | Jan | 40 | 40/… |
Common Examples - Cumulative
Last 30 days rolling revenue
This metric uses the inteval
function to define where to accumulate from, using the expression orders.close_date - interval '30 days'
to start 30 days before the date an event date
Note this is a daily metric (each day has a different value) but uses a window to accumulate.
Last 30 days rolling revenue, yesterday
The 1 day offset
is applied to both date_range_start
and date_range_end
Month-to-date (MTD) revenue
This metric uses the DATE_TRUNC
function to define where to accumulate from, using the expression DATE_TRUNC('month', orders.close_date)
to start at beginning of a month
Year-to-date (YTD) revenue
This metric uses the DATE_TRUNC
function to define where to accumulate from, using the expression DATE_TRUNC('year', orders.close_date)
to start at beginning of a year
Monthly open deals
This metric uses a range of dates between the deal creation and the deal closing, to calculate the number of open deals during each month.
Was this page helpful?