Skip to content

Commit df53c51

Browse files
authored
docs: Custom time dimension granularities (#8679)
1 parent 3eb5032 commit df53c51

File tree

11 files changed

+575
-188
lines changed

11 files changed

+575
-188
lines changed

docs/pages/guides/_meta.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = {
2+
"recipes": "Recipes",
23
"dbt": "Using Cube with dbt",
34
"designing-metrics": "Designing Metrics",
4-
"recipes": "Recipes",
55
"style-guide": "Style guide",
66
"data-store-cost-saving-guide": "Cost saving guide"
77
}

docs/pages/guides/recipes.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ These recipes will show you the best practices of using Cube.
3232
- [Calculating average and percentiles](/guides/recipes/data-modeling/percentiles)
3333
- [Calculating nested aggregates](/guides/recipes/data-modeling/nested-aggregates)
3434
- [Calculating period-over-period changes](/guides/recipes/data-modeling/period-over-period)
35-
- [Implementing fiscal year or fiscal quarter dimensions](/guides/recipes/data-modeling/fiscal-year-quarter-dimensions)
35+
- [Implementing custom time dimension granularities](/guides/recipes/data-modeling/custom-granularity)
3636
- [Implementing Entity-Attribute-Value model](/guides/recipes/data-modeling/entity-attribute-value)
3737
- [Implementing data snapshots](/guides/recipes/data-modeling/snapshots)
3838
- [Using different data models for tenants](/guides/recipes/access-control/using-different-schemas-for-tenants)

docs/pages/guides/recipes/data-modeling/_meta.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ module.exports = {
22
"percentiles": "Calculating averages and percentiles",
33
"nested-aggregates": "Calculating nested aggregates",
44
"period-over-period": "Calculating period-over-period changes",
5+
"custom-granularity": "Implementing custom time dimension granularities",
56
"snapshots": "Implementing data snapshots",
67
"entity-attribute-value": "Implementing Entity-Attribute-Value Model (EAV)",
7-
"fiscal-year-quarter-dimensions": "Implementing fiscal year or fiscal quarter dimensions",
88
"passing-dynamic-parameters-in-a-query": "Passing dynamic parameters in a query",
99
"using-dynamic-measures": "Using dynamic measures",
1010
"dynamic-union-tables": "Using dynamic union tables",
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
# Implementing custom time dimension granularities
2+
3+
This recipe show examples of commonly used [custom
4+
granularities][ref-custom-granularities].
5+
6+
## Use case
7+
8+
Sometimes, you might need to group the result set by units of time that are
9+
different from [default granularities][ref-default-granularities] such as `week`
10+
(starting on Monday) or `year` (starting on January 1).
11+
12+
Below, we explore the following examples of custom granularities:
13+
* *Week starting on Sunday*, commonly used in the US and some other countries.
14+
* *[Fiscal year][wiki-fiscal-year]* and *fiscal quarter*, commonly used in
15+
accounting and financial reporting.
16+
17+
## Data modeling
18+
19+
Consider the following data model. `interval` and `offset` parameters are used to
20+
configure each custom granularity in `granularities`.
21+
22+
Note that custom granularities are also exposed via [proxy
23+
dimensions][ref-proxy-granularity] so that we can conveniently query them via
24+
[Playground][ref-playground] or BI tools connected via the [SQL API][ref-sql-api].
25+
We can also use them in further calculations like rendering `fiscal_quarter_label`.
26+
27+
<CodeTabs>
28+
29+
```yaml
30+
cubes:
31+
- name: custom_granularities
32+
sql: >
33+
SELECT '2024-01-15'::TIMESTAMP AS timestamp UNION ALL
34+
SELECT '2024-02-15'::TIMESTAMP AS timestamp UNION ALL
35+
SELECT '2024-03-15'::TIMESTAMP AS timestamp UNION ALL
36+
SELECT '2024-04-15'::TIMESTAMP AS timestamp UNION ALL
37+
SELECT '2024-05-15'::TIMESTAMP AS timestamp UNION ALL
38+
SELECT '2024-06-15'::TIMESTAMP AS timestamp UNION ALL
39+
SELECT '2024-07-15'::TIMESTAMP AS timestamp UNION ALL
40+
SELECT '2024-08-15'::TIMESTAMP AS timestamp UNION ALL
41+
SELECT '2024-09-15'::TIMESTAMP AS timestamp UNION ALL
42+
SELECT '2024-10-15'::TIMESTAMP AS timestamp UNION ALL
43+
SELECT '2024-11-15'::TIMESTAMP AS timestamp UNION ALL
44+
SELECT '2024-12-15'::TIMESTAMP AS timestamp
45+
46+
dimensions:
47+
- name: timestamp
48+
sql: timestamp
49+
type: time
50+
51+
granularities:
52+
- name: sunday_week
53+
interval: 1 week
54+
offset: -1 day
55+
56+
- name: fiscal_year
57+
title: Federal fiscal year in the United States
58+
interval: 1 year
59+
offset: -3 months
60+
61+
- name: fiscal_quarter
62+
title: Federal fiscal quarter in the United States
63+
interval: 1 quarter
64+
offset: -3 months
65+
66+
- name: sunday_week
67+
sql: "{timestamp.sunday_week}"
68+
type: time
69+
70+
- name: fiscal_year
71+
sql: "{timestamp.fiscal_year}"
72+
type: time
73+
74+
- name: fiscal_quarter
75+
sql: "{timestamp.fiscal_quarter}"
76+
type: time
77+
78+
- name: fiscal_quarter_label
79+
sql: >
80+
'FY' || EXTRACT(YEAR FROM {timestamp.fiscal_year}) ||
81+
'-Q' || EXTRACT(QUARTER FROM {timestamp.fiscal_quarter})
82+
type: string
83+
```
84+
85+
```javascript
86+
cube(`custom_granularities`, {
87+
sql: `
88+
SELECT '2024-01-15'::TIMESTAMP AS timestamp UNION ALL
89+
SELECT '2024-02-15'::TIMESTAMP AS timestamp UNION ALL
90+
SELECT '2024-03-15'::TIMESTAMP AS timestamp UNION ALL
91+
SELECT '2024-04-15'::TIMESTAMP AS timestamp UNION ALL
92+
SELECT '2024-05-15'::TIMESTAMP AS timestamp UNION ALL
93+
SELECT '2024-06-15'::TIMESTAMP AS timestamp UNION ALL
94+
SELECT '2024-07-15'::TIMESTAMP AS timestamp UNION ALL
95+
SELECT '2024-08-15'::TIMESTAMP AS timestamp UNION ALL
96+
SELECT '2024-09-15'::TIMESTAMP AS timestamp UNION ALL
97+
SELECT '2024-10-15'::TIMESTAMP AS timestamp UNION ALL
98+
SELECT '2024-11-15'::TIMESTAMP AS timestamp UNION ALL
99+
SELECT '2024-12-15'::TIMESTAMP AS timestamp
100+
`,
101+
102+
dimensions: {
103+
timestamp: {
104+
sql: `timestamp`,
105+
type: `time`,
106+
107+
granularities: {
108+
sunday_week: {
109+
interval: `1 week`,
110+
offset: `-1 day`
111+
},
112+
113+
fiscal_year: {
114+
title: `Federal fiscal year in the United States`,
115+
interval: `1 year`,
116+
offset: `-3 months`
117+
},
118+
119+
fiscal_quarter: {
120+
title: `Federal fiscal quarter in the United States`,
121+
interval: `1 quarter`,
122+
offset: `-3 months`
123+
}
124+
}
125+
},
126+
127+
sunday_week: {
128+
sql: `${timestamp.sunday_week}`,
129+
type: `time`
130+
},
131+
132+
fiscal_year: {
133+
sql: `${timestamp.fiscal_year}`,
134+
type: `time`
135+
},
136+
137+
fiscal_quarter: {
138+
sql: `${timestamp.fiscal_quarter}`,
139+
type: `time`
140+
},
141+
142+
fiscal_quarter_label: {
143+
sql: `
144+
'FY' || EXTRACT(YEAR FROM ${timestamp.fiscal_year}) ||
145+
'-Q' || EXTRACT(QUARTER FROM ${timestamp.fiscal_quarter})
146+
`,
147+
type: `string`
148+
}
149+
}
150+
})
151+
```
152+
153+
</CodeTabs>
154+
155+
156+
[ref-custom-granularities]: /reference/data-model/dimensions#granularities
157+
[ref-default-granularities]: /product/data-modeling/concepts#time-dimensions
158+
[wiki-fiscal-year]: https://en.wikipedia.org/wiki/Fiscal_year
159+
[ref-playground]: /product/workspace/playground
160+
[ref-sql-api]: /product/apis-integrations/sql-api
161+
[ref-proxy-granularity]: /product/data-modeling/concepts/calculated-members#time-dimension-granularity

docs/pages/guides/recipes/data-modeling/fiscal-year-quarter-dimensions.mdx

Lines changed: 0 additions & 84 deletions
This file was deleted.

docs/pages/product/apis-integrations/rest-api/query-format.mdx

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,17 @@ redirect_from:
55

66
# Query format in the REST API
77

8-
Cube Queries are plain JavaScript objects, describing an analytics query. The
9-
basic elements of a query (query members) are `measures`, `dimensions`, and
10-
`segments`.
8+
Queries to the REST API are plain JavaScript objects, describing an analytics
9+
query. The basic elements of a query (query members) are `measures`, `dimensions`,
10+
and `segments`.
1111

12-
The query member format name is `CUBE_NAME.MEMBER_NAME`, for example the
13-
dimension `email` in the Cube `Users` would have the name `Users.email`.
12+
The query member format name is `cube_name.member_name`, for example the `email`
13+
dimension in the `users` cube would have the `users.email` name.
1414

15-
In the case of dimension of type `time` granularity could be optionally added to
16-
the name, in the following format `CUBE_NAME.TIME_DIMENSION_NAME.GRANULARITY`,
17-
ex: `stories.time.month`.
18-
19-
Supported granularities: `second`, `minute`, `hour`, `day`, `week`, `month`,
20-
`quarter` and `year`.
15+
In the case of a dimension of the `time` type, a granularity could be optionally
16+
added to the name, in the following format: `cube_name.time_dimension_name.granularity_name`,
17+
e.g., `stories.time.week`. It can be one of the [default granularities][ref-default-granularities]
18+
(e.g., `year` or `week`) or a [custom granularity][ref-custom-granularities].
2119

2220
The Cube client also accepts an array of queries. By default, it will be treated
2321
as a Data Blending query type.
@@ -95,8 +93,8 @@ query][ref-ungrouped-query].
9593
If the `order` property is not specified in the query, Cube sorts results by
9694
default using the following rules:
9795

98-
- The first time dimension with granularity, ascending. If no time dimension
99-
with granularity exists...
96+
- The first time dimension with a granularity, ascending. If no time dimension
97+
with a granularity exists...
10098
- The first measure, descending. If no measure exists...
10199
- The first dimension, ascending.
102100

@@ -530,9 +528,10 @@ provides a convenient shortcut to pass a dimension and a filter as a
530528
range][ref-relative-date-range], for example, `last quarter`.
531529
- `compareDateRange`: An array of date ranges to compare measure values. See
532530
[compare date range queries][ref-compare-date-range] for details.
533-
- `granularity`: A granularity for a time dimension. It supports the following
534-
values `second`, `minute`, `hour`, `day`, `week`, `month`, `quarter`, `year`.
535-
If you pass `null` to the granularity, Cube will only perform filtering by a
531+
- `granularity`: A granularity for a time dimension. It can be one of the [default
532+
granularities][ref-default-granularities] (e.g., `year` or `week`) or a [custom
533+
granularity][ref-custom-granularities].
534+
If you don't provide a granularity, Cube will only perform filtering by a
536535
specified time dimension, without grouping.
537536

538537
```json
@@ -617,4 +616,6 @@ refer to its documentation for more examples.
617616
[ref-compare-date-range]: /product/apis-integrations/queries#compare-date-range-query
618617
[ref-total-query]: /product/apis-integrations/queries#total-query
619618
[ref-ungrouped-query]: /product/apis-integrations/queries#ungrouped-query
620-
[ref-default-order]: /product/apis-integrations/queries#order
619+
[ref-default-order]: /product/apis-integrations/queries#order
620+
[ref-default-granularities]: /product/data-modeling/concepts#time-dimensions
621+
[ref-custom-granularities]: /reference/data-model/dimensions#granularities

0 commit comments

Comments
 (0)