const windows = [3, 6, 9, 12];
cube(`gross_sales`, {
sql: `
SELECT '2023-01-01'::TIMESTAMP AS month, 100 AS amount UNION ALL
SELECT '2023-02-01'::TIMESTAMP AS month, 110 AS amount UNION ALL
SELECT '2023-03-01'::TIMESTAMP AS month, 120 AS amount UNION ALL
SELECT '2023-04-01'::TIMESTAMP AS month, 130 AS amount UNION ALL
SELECT '2023-05-01'::TIMESTAMP AS month, 140 AS amount UNION ALL
SELECT '2023-06-01'::TIMESTAMP AS month, 150 AS amount UNION ALL
SELECT '2023-07-01'::TIMESTAMP AS month, 160 AS amount UNION ALL
SELECT '2023-08-01'::TIMESTAMP AS month, 170 AS amount UNION ALL
SELECT '2023-09-01'::TIMESTAMP AS month, 180 AS amount UNION ALL
SELECT '2023-10-01'::TIMESTAMP AS month, 190 AS amount UNION ALL
SELECT '2023-11-01'::TIMESTAMP AS month, 200 AS amount UNION ALL
SELECT '2023-12-01'::TIMESTAMP AS month, 210 AS amount UNION ALL
SELECT '2024-01-01'::TIMESTAMP AS month, 220 AS amount UNION ALL
SELECT '2024-02-01'::TIMESTAMP AS month, 230 AS amount UNION ALL
SELECT '2024-03-01'::TIMESTAMP AS month, 240 AS amount UNION ALL
SELECT '2024-04-01'::TIMESTAMP AS month, 250 AS amount UNION ALL
SELECT '2024-05-01'::TIMESTAMP AS month, 260 AS amount UNION ALL
SELECT '2024-06-01'::TIMESTAMP AS month, 270 AS amount UNION ALL
SELECT '2024-07-01'::TIMESTAMP AS month, 280 AS amount UNION ALL
SELECT '2024-08-01'::TIMESTAMP AS month, 290 AS amount UNION ALL
SELECT '2024-09-01'::TIMESTAMP AS month, 300 AS amount UNION ALL
SELECT '2024-10-01'::TIMESTAMP AS month, 310 AS amount UNION ALL
SELECT '2024-11-01'::TIMESTAMP AS month, 320 AS amount UNION ALL
SELECT '2024-12-01'::TIMESTAMP AS month, 330 AS amount
`,
dimensions: {
month: {
sql: `month`,
type: `time`,
primary_key: true
},
// The query-time parameter: the selected value chooses the window.
growth_window: {
type: `switch`,
values: windows.map(months => `${months}m`)
}
},
measures: {
gross_sales: {
sql: `amount`,
type: `sum`
},
// Trailing (current-period) totals and their prior-period counterparts,
// one pair per window.
...windows.reduce((members, months) => {
const current = `r${months}_gross_sales`;
const prior = `prev_r${months}_gross_sales`;
return {
...members,
[current]: {
sql: `amount`,
type: `sum`,
rolling_window: {
trailing: `${months} month`
}
},
[prior]: {
multi_stage: true,
sql: `${CUBE[current]}`,
type: `number`,
time_shift: [{
interval: `${months} month`,
type: `prior`
}]
}
};
}, {}),
// The members consumers query, dispatching on growth_window.
gross_sales_current: {
multi_stage: true,
case: {
switch: `${CUBE.growth_window}`,
when: windows.map(months => {
const current = `r${months}_gross_sales`;
return { value: `${months}m`, sql: `${CUBE[current]}` };
}),
else: {
sql: `${CUBE[`r${windows[0]}_gross_sales`]}`
}
},
type: `number`
},
gross_sales_prior: {
multi_stage: true,
case: {
switch: `${CUBE.growth_window}`,
when: windows.map(months => {
const prior = `prev_r${months}_gross_sales`;
return { value: `${months}m`, sql: `${CUBE[prior]}` };
}),
else: {
sql: `${CUBE[`prev_r${windows[0]}_gross_sales`]}`
}
},
type: `number`
},
gross_sales_change: {
multi_stage: true,
sql: `${gross_sales_current} - ${gross_sales_prior}`,
type: `number`
},
gross_sales_growth_percentage: {
multi_stage: true,
sql: `100.0 * (${gross_sales_current} - ${gross_sales_prior}) / NULLIF(${gross_sales_prior}, 0)`,
type: `number`
}
}
});