Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [1.3.14](https://github.com/cube-js/cube/compare/v1.3.13...v1.3.14) (2025-05-13)

### Bug Fixes

- **schema-compiler:** Fix view queries for measure with single timeshift reference without time dimension ([#9565](https://github.com/cube-js/cube/issues/9565)) ([d3c28d4](https://github.com/cube-js/cube/commit/d3c28d48b470d7ee052107ed10d4bce6a324eef2))

### Features

- Rewrite joins from SQL as query-level join hints ([#9561](https://github.com/cube-js/cube/issues/9561)) ([2b2ac1c](https://github.com/cube-js/cube/commit/2b2ac1c47898f4f6bf67ebae658f90b768c63a7a))

## [1.3.13](https://github.com/cube-js/cube/compare/v1.3.12...v1.3.13) (2025-05-12)

### Features
Expand Down
13 changes: 12 additions & 1 deletion docs/pages/guides/recipes/data-modeling/period-over-period.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ _previous period_.
these `rolling_window` measures and uses them in a calculation, e.g.,
divides or subtracts them.

<WarningBox>

Tesseract, the [next-generation data modeling engine][link-tesseract],
provides a more efficient way to calculate [period-over-period changes][link-period-over-period].
Tesseract is currently in preview. Use the `CUBEJS_TESSERACT_SQL_PLANNER`
environment variable to enable it.

</WarningBox>

The following data model allows to calculate a month-over-month change of
some value. `current_month_sum` and `previous_month_sum` measures define
two rolling windows and the `month_over_month_ratio` measure divides
Expand Down Expand Up @@ -89,4 +98,6 @@ Here's the result:

[ref-rolling-window]: /reference/data-model/measures#rolling_window
[ref-calculated-measure]: /product/data-modeling/overview#4-using-calculated-measures
[ref-time-dimension-granularity]: /product/apis-integrations/rest-api/query-format#time-dimensions-format
[ref-time-dimension-granularity]: /product/apis-integrations/rest-api/query-format#time-dimensions-format
[link-tesseract]: https://cube.dev/blog/introducing-next-generation-data-modeling-engine
[link-period-over-period]: /product/data-modeling/concepts/multi-stage-calculations#prior-date
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ or arbitrary SQL expressions.

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

<WarningBox>

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

Common uses of multi-stage calculations:

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

## Rolling window

Rolling window calculations are used to calculate metrics over a moving window of time.
Use the [`rolling_window` parameter][ref-rolling-window] of a measure to define
a rolling window.

### Stages

Here's how the rolling window calculation is performed:

- **Date range.** First, the date range for the query is determined.
If there's a time dimension with a date range filter in the query, it's used.
Otherwise, the date range is determined by selecting the minimum and maximum
values for the time dimension.

<WarningBox>

Tesseract enables rolling window calculations without the date range for the time dimension.
If Tesseract is not used, the date range must be provided. Otherwise, the query would
fail with the following error: `Time series queries without dateRange aren't supported`.

</WarningBox>

- **Time windows.** Then, the series of time windows is calculated. The size of the
window is defined by the time dimension granularity and the `trailing` and
`leading` parameters.
- **Measure.** Finally, the measure is calculated for each window.

### Example

Data model:

```yaml

cubes:
- name: orders
sql: >
SELECT 1 AS id, '2025-01-01'::TIMESTAMP AS time UNION ALL
SELECT 2 AS id, '2025-01-11'::TIMESTAMP AS time UNION ALL
SELECT 3 AS id, '2025-01-21'::TIMESTAMP AS time UNION ALL
SELECT 4 AS id, '2025-01-31'::TIMESTAMP AS time UNION ALL
SELECT 5 AS id, '2025-02-01'::TIMESTAMP AS time UNION ALL
SELECT 6 AS id, '2025-02-11'::TIMESTAMP AS time UNION ALL
SELECT 7 AS id, '2025-02-21'::TIMESTAMP AS time UNION ALL
SELECT 8 AS id, '2025-03-01'::TIMESTAMP AS time UNION ALL
SELECT 9 AS id, '2025-03-11'::TIMESTAMP AS time UNION ALL
SELECT 10 AS id, '2025-03-21'::TIMESTAMP AS time UNION ALL
SELECT 11 AS id, '2025-03-31'::TIMESTAMP AS time UNION ALL
SELECT 12 AS id, '2025-04-01'::TIMESTAMP AS time

dimensions:
- name: time
sql: time
type: time

measures:
- name: rolling_count_month
sql: id
type: count
rolling_window:
trailing: unbounded
```

Query and result:

<Screenshot src="https://ucarecdn.com/40179d09-3e10-4ada-8456-1deca6a5035d/" />

## Period-to-date

Period-to-date calculations can be used to analyze data over different time periods:
Expand Down Expand Up @@ -388,4 +457,6 @@ Query and result:

[link-tesseract]: https://cube.dev/blog/introducing-next-generation-data-modeling-engine
[ref-measures]: /product/data-modeling/concepts#measures
[ref-dimensions]: /product/data-modeling/concepts#dimensions
[ref-dimensions]: /product/data-modeling/concepts#dimensions
[ref-rolling-window]: /reference/data-model/measures#rolling_window
[link-cte]: https://en.wikipedia.org/wiki/Hierarchical_and_recursive_queries_in_SQL#Common_table_expression
51 changes: 35 additions & 16 deletions docs/pages/reference/data-model/measures.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -248,28 +248,45 @@ cubes:

### `rolling_window`

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

<WarningBox>

`rolling_window` only works for a query where there's a single `timeDimension`
with a defined date range.
Rolling window calculations require the query to contain a single time dimension
with a provided date range. It is used to calculate the minimum and maximum values
for the series of time windows.

With Tesseract, the [next-generation data modeling engine][link-tesseract],
rolling window calculations don't require the date range for the time dimension.
Tesseract is currently in preview. Use the `CUBEJS_TESSERACT_SQL_PLANNER`
environment variable to enable it.

</WarningBox>

These parameters have a format defined as
`(-?\d+) (minute|hour|day|week|month|year)`. The `trailing` and `leading`
parameters can also be set to an `unbounded` value, which means infinite size
for the corresponding window part. You can define `trailing` and `leading`
parameters using negative integers.
#### `offset`

The `offset` parameter is used to specify the starting point of the time window.

You can set the window `offset` parameter to either `start` or `end`, which will
match the start or end of the window.

By default, the `offset` parameter is set to `end`.

#### `trailing` and `leading`

The `trailing` and `leading` parameters define the size of the time window.
The `trailing` parameter defines the size of the window part before the `offset` point,
and the `leading` parameter defines the size of the window part after the `offset` point.

These parameters have a format defined as `(-?\d+) (minute|hour|day|week|month|year)`.
It means that you can define these parameters using both positive and negative integers.

The `trailing` and `leading` parameters can also be set to `unbounded`,
which means _infinite size_ for the corresponding window part.

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

<CodeTabs>

Expand Down Expand Up @@ -535,4 +552,6 @@ join will be created automatically.
[ref-drilldowns]: /guides/recipes/data-exploration/drilldowns
[ref-naming]: /product/data-modeling/syntax#naming
[ref-playground]: /product/workspace/playground
[ref-apis]: /product/apis-integrations
[ref-apis]: /product/apis-integrations
[ref-rolling-window]: /product/data-modeling/concepts/multi-stage-calculations#rolling-window
[link-tesseract]: https://cube.dev/blog/introducing-next-generation-data-modeling-engine
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.3.13",
"version": "1.3.14",
"npmClient": "yarn",
"command": {
"bootstrap": {
Expand Down
6 changes: 6 additions & 0 deletions packages/cubejs-api-gateway/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [1.3.14](https://github.com/cube-js/cube/compare/v1.3.13...v1.3.14) (2025-05-13)

### Features

- Rewrite joins from SQL as query-level join hints ([#9561](https://github.com/cube-js/cube/issues/9561)) ([2b2ac1c](https://github.com/cube-js/cube/commit/2b2ac1c47898f4f6bf67ebae658f90b768c63a7a))

## [1.3.13](https://github.com/cube-js/cube/compare/v1.3.12...v1.3.13) (2025-05-12)

### Features
Expand Down
8 changes: 4 additions & 4 deletions packages/cubejs-api-gateway/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@cubejs-backend/api-gateway",
"description": "Cube.js API Gateway",
"author": "Cube Dev, Inc.",
"version": "1.3.13",
"version": "1.3.14",
"repository": {
"type": "git",
"url": "https://github.com/cube-js/cube.git",
Expand All @@ -27,8 +27,8 @@
"dist/src/*"
],
"dependencies": {
"@cubejs-backend/native": "1.3.13",
"@cubejs-backend/shared": "1.3.13",
"@cubejs-backend/native": "1.3.14",
"@cubejs-backend/shared": "1.3.14",
"@ungap/structured-clone": "^0.3.4",
"assert-never": "^1.4.0",
"body-parser": "^1.19.0",
Expand All @@ -51,7 +51,7 @@
"uuid": "^8.3.2"
},
"devDependencies": {
"@cubejs-backend/linter": "1.3.13",
"@cubejs-backend/linter": "1.3.14",
"@types/express": "^4.17.21",
"@types/jest": "^29",
"@types/jsonwebtoken": "^9.0.2",
Expand Down
4 changes: 4 additions & 0 deletions packages/cubejs-athena-driver/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [1.3.14](https://github.com/cube-js/cube/compare/v1.3.13...v1.3.14) (2025-05-13)

**Note:** Version bump only for package @cubejs-backend/athena-driver

## [1.3.13](https://github.com/cube-js/cube/compare/v1.3.12...v1.3.13) (2025-05-12)

**Note:** Version bump only for package @cubejs-backend/athena-driver
Expand Down
10 changes: 5 additions & 5 deletions packages/cubejs-athena-driver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@cubejs-backend/athena-driver",
"description": "Cube.js Athena database driver",
"author": "Cube Dev, Inc.",
"version": "1.3.13",
"version": "1.3.14",
"repository": {
"type": "git",
"url": "https://github.com/cube-js/cube.git",
Expand All @@ -29,13 +29,13 @@
"types": "dist/src/index.d.ts",
"dependencies": {
"@aws-sdk/client-athena": "^3.22.0",
"@cubejs-backend/base-driver": "1.3.13",
"@cubejs-backend/shared": "1.3.13",
"@cubejs-backend/base-driver": "1.3.14",
"@cubejs-backend/shared": "1.3.14",
"sqlstring": "^2.3.1"
},
"devDependencies": {
"@cubejs-backend/linter": "1.3.13",
"@cubejs-backend/testing-shared": "1.3.13",
"@cubejs-backend/linter": "1.3.14",
"@cubejs-backend/testing-shared": "1.3.14",
"@types/ramda": "^0.27.40",
"typescript": "~5.2.2"
},
Expand Down
4 changes: 4 additions & 0 deletions packages/cubejs-backend-cloud/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [1.3.14](https://github.com/cube-js/cube/compare/v1.3.13...v1.3.14) (2025-05-13)

**Note:** Version bump only for package @cubejs-backend/cloud

## [1.3.13](https://github.com/cube-js/cube/compare/v1.3.12...v1.3.13) (2025-05-12)

**Note:** Version bump only for package @cubejs-backend/cloud
Expand Down
6 changes: 3 additions & 3 deletions packages/cubejs-backend-cloud/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cubejs-backend/cloud",
"version": "1.3.13",
"version": "1.3.14",
"description": "Cube Cloud package",
"main": "dist/src/index.js",
"typings": "dist/src/index.d.ts",
Expand All @@ -25,15 +25,15 @@
"devDependencies": {
"@babel/core": "^7.24.5",
"@babel/preset-env": "^7.24.5",
"@cubejs-backend/linter": "1.3.13",
"@cubejs-backend/linter": "1.3.14",
"@types/fs-extra": "^9.0.8",
"@types/jest": "^29",
"jest": "^29",
"typescript": "~5.2.2"
},
"dependencies": {
"@cubejs-backend/dotenv": "^9.0.2",
"@cubejs-backend/shared": "1.3.13",
"@cubejs-backend/shared": "1.3.14",
"chokidar": "^3.5.1",
"env-var": "^6.3.0",
"form-data": "^4.0.0",
Expand Down
4 changes: 4 additions & 0 deletions packages/cubejs-backend-maven/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [1.3.14](https://github.com/cube-js/cube/compare/v1.3.13...v1.3.14) (2025-05-13)

**Note:** Version bump only for package @cubejs-backend/maven

## [1.3.13](https://github.com/cube-js/cube/compare/v1.3.12...v1.3.13) (2025-05-12)

**Note:** Version bump only for package @cubejs-backend/maven
Expand Down
6 changes: 3 additions & 3 deletions packages/cubejs-backend-maven/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@cubejs-backend/maven",
"description": "Cube.js Maven Wrapper for java dependencies downloading",
"author": "Cube Dev, Inc.",
"version": "1.3.13",
"version": "1.3.14",
"license": "Apache-2.0",
"repository": {
"type": "git",
Expand Down Expand Up @@ -31,12 +31,12 @@
"dist/src/*"
],
"dependencies": {
"@cubejs-backend/shared": "1.3.13",
"@cubejs-backend/shared": "1.3.14",
"source-map-support": "^0.5.19",
"xmlbuilder2": "^2.4.0"
},
"devDependencies": {
"@cubejs-backend/linter": "1.3.13",
"@cubejs-backend/linter": "1.3.14",
"@types/jest": "^29",
"@types/node": "^20",
"jest": "^29",
Expand Down
4 changes: 4 additions & 0 deletions packages/cubejs-backend-native/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [1.3.14](https://github.com/cube-js/cube/compare/v1.3.13...v1.3.14) (2025-05-13)

**Note:** Version bump only for package @cubejs-backend/native

## [1.3.13](https://github.com/cube-js/cube/compare/v1.3.12...v1.3.13) (2025-05-12)

### Features
Expand Down
Loading
Loading