Skip to content

Commit 4580972

Browse files
committed
docs: Calendar cubes
1 parent 36e2ab0 commit 4580972

File tree

11 files changed

+1544
-334
lines changed

11 files changed

+1544
-334
lines changed

docs/pages/product/caching/recipes/non-additivity.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,7 @@ cube(`users`, {
165165

166166
### Decomposing into a formula with additive measures
167167

168-
Non-additive `avg` measures can be rewritten as
169-
[calculated measures](/product/data-modeling/reference/measures#calculated-measures)
168+
Non-additive `avg` measures can be rewritten as [calculated measures][ref-calculated-measures]
170169
that reference additive measures only. Then, this additive measures can be used
171170
in pre-aggregations. Please note, however, that you shouldn't include `avg_age`
172171
measure in your pre-aggregation as it renders it non-additive.
@@ -245,4 +244,5 @@ or run it with the `docker-compose up` command. You'll see the result, including
245244
queried data, in the console.
246245

247246

248-
[ref-percentile-recipe]: /product/data-modeling/recipes/percentiles
247+
[ref-percentile-recipe]: /product/data-modeling/recipes/percentiles
248+
[ref-calculated-measures]: /product/data-modeling/concepts/calculated-members#calculated-measures

docs/pages/product/data-modeling/concepts.mdx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ metrics-first approaches.
4242
_Cubes_ represent datasets in Cube and are conceptually similar to [views in
4343
SQL][wiki-view-sql]. Cubes are usually declared in separate files with one
4444
cube per file. Typically, a cube points to a single table in
45-
your database using the [`sql_table` property][ref-schema-ref-sql-table]:
45+
your [data source][ref-data-sources] using the [`sql_table` property][ref-schema-ref-sql-table]:
4646

4747
<CodeTabs>
4848

@@ -104,6 +104,9 @@ Cubes and their members can be further referenced by [views](#views).
104104
105105
Note that cubes support [extension][ref-extending-cubes],
106106
[polymorphism][ref-polymorphic-cubes], and [data blending][ref-data-blending].
107+
Custom calendars, such as retail calendars, can be implemented using [calendar
108+
cubes][ref-calendar-cubes].
109+
107110
Cubes can be defined statically and you can also build [dynamic data
108111
models][ref-dynamic-data-models].
109112
@@ -852,4 +855,6 @@ See the reference documentaton for the full list of pre-aggregation
852855
[ref-custom-calendar-recipe]: /product/data-modeling/recipes/custom-calendar
853856
[ref-cube-with-dbt]: /product/data-modeling/recipes/dbt
854857
[ref-apis-support]: /product/apis-integrations#data-modeling
855-
[ref-viz-tools]: /product/configuration/visualization-tools
858+
[ref-viz-tools]: /product/configuration/visualization-tools
859+
[ref-data-sources]: /product/configuration/data-sources
860+
[ref-calendar-cubes]: /product/data-modeling/concepts/calendar-cubes

docs/pages/product/data-modeling/concepts/_meta.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module.exports = {
22
"calculated-members": "Calculated members",
33
"multi-stage-calculations": "Multi-stage calculations",
4+
"calendar-cubes": "Calendar cubes",
45
"code-reusability-extending-cubes": "Extension",
56
"polymorphic-cubes": "Polymorphic cubes",
67
"data-blending": "Data blending",

docs/pages/product/data-modeling/concepts/calculated-members.mdx

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ into formulas that involve simpler measures. Also, calculated measures [can
1717
help][ref-decomposition-recipe] to use [non-additive][ref-non-additive] measures with
1818
pre-aggregations.
1919

20+
### Members of the same cube
21+
2022
In the following example, the `completed_ratio` measure is calculated as a division of
2123
`completed_count` by total `count`. Note that the result is also multiplied by `1.0`
2224
since [integer division in SQL][link-postgres-division] would otherwise produce an
@@ -90,6 +92,149 @@ FROM (
9092
) AS "orders"
9193
```
9294

95+
### Members of other cubes
96+
97+
If you have `first_cube` that is [joined][ref-joins] to `second_cube`, you can define a
98+
calculated measure that references measures from both `first_cube` and `second_cube`.
99+
When you query for this calculated measure, Cube will transparently generate SQL with
100+
necessary joins.
101+
102+
In the following example, the `orders.purchases_to_users_ratio` measure references the
103+
`purchases` measure from the `orders` cube and the `count` measure from the `users` cube:
104+
105+
<CodeTabs>
106+
107+
```javascript
108+
cube(`orders`, {
109+
sql: `
110+
SELECT 1 AS id, 11 AS user_id, 'processing' AS status UNION ALL
111+
SELECT 2 AS id, 11 AS user_id, 'completed' AS status UNION ALL
112+
SELECT 3 AS id, 11 AS user_id, 'completed' AS status
113+
`,
114+
115+
dimensions: {
116+
id: {
117+
sql: `id`,
118+
type: `number`,
119+
primary_key: true
120+
}
121+
},
122+
123+
measures: {
124+
purchases: {
125+
type: `count`,
126+
filters: [{
127+
sql: `${CUBE}.status = 'completed'`
128+
}]
129+
}
130+
}
131+
})
132+
133+
cube(`users`, {
134+
sql: `
135+
SELECT 11 AS id, 'Alice' AS name UNION ALL
136+
SELECT 12 AS id, 'Bob' AS name UNION ALL
137+
SELECT 13 AS id, 'Eve' AS name
138+
`,
139+
140+
joins: {
141+
orders: {
142+
sql: `${CUBE}.id = ${orders}.user_id`,
143+
relationship: `one_to_many`
144+
}
145+
},
146+
147+
dimensions: {
148+
id: {
149+
sql: `id`,
150+
type: `number`,
151+
primary_key: true
152+
}
153+
},
154+
155+
measures: {
156+
count: {
157+
type: `count`
158+
},
159+
160+
purchases_to_users_ratio: {
161+
sql: `100.0 * ${orders.purchases} / ${CUBE.count}`,
162+
type: `number`,
163+
format: `percent`
164+
}
165+
}
166+
})
167+
```
168+
169+
```yaml
170+
cubes:
171+
- name: orders
172+
sql: >
173+
SELECT 1 AS id, 11 AS user_id, 'processing' AS status UNION ALL
174+
SELECT 2 AS id, 11 AS user_id, 'completed' AS status UNION ALL
175+
SELECT 3 AS id, 11 AS user_id, 'completed' AS status
176+
177+
dimensions:
178+
- name: id
179+
sql: id
180+
type: number
181+
primary_key: true
182+
183+
measures:
184+
- name: purchases
185+
type: count
186+
filters:
187+
- sql: "{CUBE}.status = 'completed'"
188+
189+
- name: users
190+
sql: >
191+
SELECT 11 AS id, 'Alice' AS name UNION ALL
192+
SELECT 12 AS id, 'Bob' AS name UNION ALL
193+
SELECT 13 AS id, 'Eve' AS name
194+
195+
joins:
196+
- name: orders
197+
sql: "{CUBE}.id = {orders}.user_id"
198+
relationship: one_to_many
199+
200+
dimensions:
201+
- name: id
202+
sql: id
203+
type: number
204+
primary_key: true
205+
206+
measures:
207+
- name: count
208+
type: count
209+
210+
- name: purchases_to_users_ratio
211+
sql: "1.0 * {orders.purchases} / {CUBE.count}"
212+
type: number
213+
```
214+
215+
</CodeTabs>
216+
217+
If you query for `users.purchases_to_users_ratio`, Cube will generate the following SQL:
218+
219+
```sql
220+
SELECT
221+
1.0 * COUNT(
222+
CASE
223+
WHEN ("orders".status = 'completed') THEN "orders".id
224+
END
225+
) / COUNT(DISTINCT "users".id) "users__purchases_to_users_ratio"
226+
FROM (
227+
SELECT 11 AS id, 'Alice' AS name UNION ALL
228+
SELECT 12 AS id, 'Bob' AS name UNION ALL
229+
SELECT 13 AS id, 'Eve' AS name
230+
) AS "users"
231+
LEFT JOIN (
232+
SELECT 1 AS id, 11 AS user_id, 'processing' AS status UNION ALL
233+
SELECT 2 AS id, 11 AS user_id, 'completed' AS status UNION ALL
234+
SELECT 3 AS id, 11 AS user_id, 'completed' AS status
235+
) AS "orders" ON "users".id = "orders".user_id
236+
```
237+
93238
## Proxy dimensions
94239

95240
**Proxy dimensions reference dimensions from the same cube or other cubes.**

0 commit comments

Comments
 (0)