Skip to content

Commit 9079227

Browse files
committed
docs: Rolling window calculations without date range, enabled by Tesseract
1 parent d3c28d4 commit 9079227

File tree

3 files changed

+120
-19
lines changed

3 files changed

+120
-19
lines changed

docs/pages/guides/recipes/data-modeling/period-over-period.mdx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ _previous period_.
1717
these `rolling_window` measures and uses them in a calculation, e.g.,
1818
divides or subtracts them.
1919

20+
<WarningBox>
21+
22+
Tesseract, the [next-generation data modeling engine][link-tesseract],
23+
provides a more efficient way to calculate [period-over-period changes][link-period-over-period].
24+
Tesseract is currently in preview. Use the `CUBEJS_TESSERACT_SQL_PLANNER`
25+
environment variable to enable it.
26+
27+
</WarningBox>
28+
2029
The following data model allows to calculate a month-over-month change of
2130
some value. `current_month_sum` and `previous_month_sum` measures define
2231
two rolling windows and the `month_over_month_ratio` measure divides
@@ -89,4 +98,6 @@ Here's the result:
8998

9099
[ref-rolling-window]: /reference/data-model/measures#rolling_window
91100
[ref-calculated-measure]: /product/data-modeling/overview#4-using-calculated-measures
92-
[ref-time-dimension-granularity]: /product/apis-integrations/rest-api/query-format#time-dimensions-format
101+
[ref-time-dimension-granularity]: /product/apis-integrations/rest-api/query-format#time-dimensions-format
102+
[link-tesseract]: https://cube.dev/blog/introducing-next-generation-data-modeling-engine
103+
[link-period-over-period]: /product/data-modeling/concepts/multi-stage-calculations#prior-date

docs/pages/product/data-modeling/concepts/multi-stage-calculations.mdx

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ or arbitrary SQL expressions.
55

66
_Multi-stage calculations_ enable data modeling of more sophisticated _multi-stage measures_.
77
They are calculated in two or more stages and often involve manipulations on already
8-
aggregated data.
8+
aggregated data. Each stage results in one or more [common table expressions][link-cte]
9+
(CTEs) in the generated SQL query.
910

1011
<WarningBox>
1112

@@ -24,11 +25,79 @@ Please track [this issue](https://github.com/cube-js/cube/issues/8487).
2425

2526
Common uses of multi-stage calculations:
2627

28+
- [Rolling window](#rolling-window) calculations.
2729
- [Period-to-date](#period-to-date) calculations, e.g., year-to-date (YTD) analysis.
2830
- [Prior date](#prior-date) calculations, e.g., year-over-year sales growth.
2931
- [Fixed dimension](#fixed-dimension) calculations, e.g., comparing individual items to a broader dataset or calculating percent of total.
3032
- [Ranking](#ranking) calculations.
3133

34+
## Rolling window
35+
36+
Rolling window calculations are used to calculate metrics over a moving window of time.
37+
Use the [`rolling_window` parameter][ref-rolling-window] of a measure to define
38+
a rolling window.
39+
40+
### Stages
41+
42+
Here's how the rolling window calculation is performed:
43+
44+
- **Date range.** First, the date range for the query is determined.
45+
If there's a time dimension with a date range filter in the query, it's used.
46+
Otherwise, the date range is determined by selecting the minimum and maximum
47+
values for the time dimension.
48+
49+
<WarningBox>
50+
51+
Tesseract enables rolling window calculations without the date range for the time dimension.
52+
If Tesseract is not used, the date range must be provided. Otherwise, the query would
53+
fail with the following error: `Time series queries without dateRange aren't supported`.
54+
55+
</WarningBox>
56+
57+
- **Time windows.** Then, the series of time windows is calculated. The size of the
58+
window is defined by the time dimension granularity and the `trailing` and
59+
`leading` parameters.
60+
- **Measure.** Finally, the measure is calculated for each window.
61+
62+
### Example
63+
64+
Data model:
65+
66+
```yaml
67+
68+
cubes:
69+
- name: orders
70+
sql: >
71+
SELECT 1 AS id, '2025-01-01'::TIMESTAMP AS time UNION ALL
72+
SELECT 2 AS id, '2025-01-11'::TIMESTAMP AS time UNION ALL
73+
SELECT 3 AS id, '2025-01-21'::TIMESTAMP AS time UNION ALL
74+
SELECT 4 AS id, '2025-01-31'::TIMESTAMP AS time UNION ALL
75+
SELECT 5 AS id, '2025-02-01'::TIMESTAMP AS time UNION ALL
76+
SELECT 6 AS id, '2025-02-11'::TIMESTAMP AS time UNION ALL
77+
SELECT 7 AS id, '2025-02-21'::TIMESTAMP AS time UNION ALL
78+
SELECT 8 AS id, '2025-03-01'::TIMESTAMP AS time UNION ALL
79+
SELECT 9 AS id, '2025-03-11'::TIMESTAMP AS time UNION ALL
80+
SELECT 10 AS id, '2025-03-21'::TIMESTAMP AS time UNION ALL
81+
SELECT 11 AS id, '2025-03-31'::TIMESTAMP AS time UNION ALL
82+
SELECT 12 AS id, '2025-04-01'::TIMESTAMP AS time
83+
84+
dimensions:
85+
- name: time
86+
sql: time
87+
type: time
88+
89+
measures:
90+
- name: rolling_count_month
91+
sql: id
92+
type: count
93+
rolling_window:
94+
trailing: unbounded
95+
```
96+
97+
Query and result:
98+
99+
<Screenshot src="https://ucarecdn.com/40179d09-3e10-4ada-8456-1deca6a5035d/" />
100+
32101
## Period-to-date
33102
34103
Period-to-date calculations can be used to analyze data over different time periods:
@@ -388,4 +457,6 @@ Query and result:
388457
389458
[link-tesseract]: https://cube.dev/blog/introducing-next-generation-data-modeling-engine
390459
[ref-measures]: /product/data-modeling/concepts#measures
391-
[ref-dimensions]: /product/data-modeling/concepts#dimensions
460+
[ref-dimensions]: /product/data-modeling/concepts#dimensions
461+
[ref-rolling-window]: /reference/data-model/measures#rolling_window
462+
[link-cte]: https://en.wikipedia.org/wiki/Hierarchical_and_recursive_queries_in_SQL#Common_table_expression

docs/pages/reference/data-model/measures.mdx

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -248,28 +248,45 @@ cubes:
248248
249249
### `rolling_window`
250250

251-
If you want to calculate some metric within a window, for example a week or a
252-
month, you should use a `rolling_window` parameter. The `trailing` and `leading`
253-
parameters define window size.
251+
The `rolling_window` parameter is used to for [rolling window][ref-rolling-window]
252+
calculations, e.g., to calculate a metric over a moving window of time, e.g. a
253+
week or a month.
254254

255255
<WarningBox>
256256

257-
`rolling_window` only works for a query where there's a single `timeDimension`
258-
with a defined date range.
257+
Rolling window calculations require the query to contain a single time dimension
258+
with a provided date range. It is used to calculate the minimum and maximum values
259+
for the series of time windows.
260+
261+
With Tesseract, the [next-generation data modeling engine][link-tesseract],
262+
rolling window calculations don't require the date range for the time dimension.
263+
Tesseract is currently in preview. Use the `CUBEJS_TESSERACT_SQL_PLANNER`
264+
environment variable to enable it.
259265

260266
</WarningBox>
261267

262-
These parameters have a format defined as
263-
`(-?\d+) (minute|hour|day|week|month|year)`. The `trailing` and `leading`
264-
parameters can also be set to an `unbounded` value, which means infinite size
265-
for the corresponding window part. You can define `trailing` and `leading`
266-
parameters using negative integers.
268+
#### `offset`
269+
270+
The `offset` parameter is used to specify the starting point of the time window.
271+
272+
You can set the window `offset` parameter to either `start` or `end`, which will
273+
match the start or end of the window.
274+
275+
By default, the `offset` parameter is set to `end`.
276+
277+
#### `trailing` and `leading`
278+
279+
The `trailing` and `leading` parameters define the size of the time window.
280+
The `trailing` parameter defines the size of the window part before the `offset` point,
281+
and the `leading` parameter defines the size of the window part after the `offset` point.
282+
283+
These parameters have a format defined as `(-?\d+) (minute|hour|day|week|month|year)`.
284+
It means that you can define these parameters using both positive and negative integers.
285+
286+
The `trailing` and `leading` parameters can also be set to `unbounded`,
287+
which means _infinite size_ for the corresponding window part.
267288

268-
The `trailing` parameter is a window part size before the `offset` point and the
269-
`leading` parameter is after it. You can set the window `offset` parameter to
270-
either `start` or `end`, which will match the start or end of the selected date
271-
range. By default, the `leading` and `trailing` parameters are set to zero and
272-
`offset` is set to `end`.
289+
By default, the `leading` and `trailing` parameters are set to zero.
273290

274291
<CodeTabs>
275292

@@ -535,4 +552,6 @@ join will be created automatically.
535552
[ref-drilldowns]: /guides/recipes/data-exploration/drilldowns
536553
[ref-naming]: /product/data-modeling/syntax#naming
537554
[ref-playground]: /product/workspace/playground
538-
[ref-apis]: /product/apis-integrations
555+
[ref-apis]: /product/apis-integrations
556+
[ref-rolling-window]: /product/data-modeling/concepts/multi-stage-calculations#rolling-window
557+
[link-tesseract]: https://cube.dev/blog/introducing-next-generation-data-modeling-engine

0 commit comments

Comments
 (0)