@@ -5,7 +5,8 @@ or arbitrary SQL expressions.
55
66_ Multi-stage calculations_ enable data modeling of more sophisticated _ multi-stage measures_ .
77They 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
2526Common 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
34103Period-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
0 commit comments