Configuration
Calendar cubes are cubes where thecalendar parameter
is set to true. This indicates that the cube is a calendar cube and allow the use of
custom time shifts and granularities.
Joins
Calendar cubes are only useful when they are joined with other cubes in the data model.- The calendar cube’s join dimension must be of type
timeand also be theprimary_key. - The other cube’s join dimension must also be of type
time.
Overriding time shifts
Calendar cubes can be used to override the default time shift behavior of time-shift measures. It can help implement custom time shifts or reuse common time shifts across multiple cubes. By default, a time shift likeprior + 1 month will add INTERVAL '1 month' to the
time dimension value in the generated SQL. However, with custom calendars, a more nuanced
approach is often needed, such as mapping each date to another pre-calculated date from
the calendar table.
In the following example, the custom_calendar cube defines a custom time shift for
prior + 1 month that uses the month_ago column from the calendar table. It also
defines a custom time shift my_favorite_time_shift of type prior + the 42 days
interval.
sales.total_sales_prior_month and sales.total_sales_few_days_ago measures are
queried together with the calendar.date time dimension, the generate SQL will use the
custom time shifts defined in the custom_calendar cube: one with the month_ago
column and another with INTERVAL '42 days'.
Overriding granularities
Calendar cubes can be used to override the default granularities of time dimensions. By default, SQL functions likeDATE_TRUNC are used to calculate default granularities,
such as day, month, or year. However, custom calendars often have different
definitions for these periods, e.g., a retail calendar might use 4-5-4 week patterns.
Calendar cubes allow you to define custom SQL expressions for each granularity.
In the following example, the fiscal_calendar cube overrides the default month
granularity to the to a pre-calculated mid_month column:
sales.revenue by custom_calendar.date with monthly granularity, the
mid_month column will be used instead of the standard DATE_TRUNC('month', date)
expression in the generated SQL.
Naming a granularity defined with sql
A granularity defined with sql must be named after a default granularity: second,
minute, hour, day, week, month, quarter, or year. Names are matched
case-insensitively. The sql parameter changes what an existing unit of time means; it
cannot introduce a new one. A granularity under a name of your own, such as fiscal_week,
does not compile:
interval is what places a granularity in the day → week →
month → quarter → year hierarchy, so Cube knows what to roll it up from and what to
decompose it into. A default granularity brings that position with it; a name of your own
does not, so it has to state its interval.
The two styles serve different purposes:
Use
interval to add a granularity, for example a fiscal week of a regular seven days.
See the custom granularity recipe for fiscal_week,
fiscal_quarter, and fiscal_year defined that way. Use sql when a period varies in
length and cannot be derived arithmetically, such as the months and quarters of a 4-5-4
retail calendar. The custom calendar recipe models a 4-5-4
calendar in full.
A pre-aggregation over an overridden granularity must declare
that granularity. A rollup on the
month granularity above serves queries at month,
and it is built from the mid_month column rather than DATE_TRUNC. A rollup at another
granularity will not serve those queries correctly.