Description
Describe the bug
If a measure references a rolling window measure and a dimension from a joined cube at the same time, incorrect SQL reference will be generated for the dimension. The generated SQL query will not execute.
To Reproduce
Consider the following data model. Check how the variance
measure is defined. It references dummy_dim.type
, a dimension from a joined cube. It also references actual_cur_month
, a "regular" measure from the same cube:
cube(`dummy_data`, {
sql: `
SELECT 10 AS value, '2024-01-01'::TIMESTAMP AS date, 'P' as type UNION ALL
SELECT 10 AS value, '2024-01-01'::TIMESTAMP AS date, 'R' as type UNION ALL
SELECT 30 AS value, '2024-02-01'::TIMESTAMP AS date, 'R' as type UNION ALL
SELECT 80 AS value, '2024-03-01'::TIMESTAMP AS date, 'R' as type
`,
joins: {
dummy_dim: {
relationship: `many_to_one`,
sql: `${CUBE}.type = ${dummy_dim.code}`,
},
},
dimensions: {
date: {
sql: `${CUBE}.date`,
type: `time`
},
type: {
sql: `${CUBE}.type`,
type: `string`,
primary_key: true
}
},
measures: {
actual_cur_month: {
sql: `${CUBE}.value`,
type: `sum`
},
actual_prev_month: {
sql: `${CUBE}.value`,
type: `sum`,
rolling_window: {
trailing: `1 month`,
offset: `start`
}
},
variance: {
sql: `case when ${dummy_dim.type} = 'BBB' then 123 else ${actual_cur_month} end`,
type: `number`
}
}
})
cube(`dummy_dim`, {
sql: `
SELECT 'AAA' AS type, 'R' as code UNION ALL
SELECT 'BBB' AS type, 'R' as code
`,
dimensions: {
type: {
sql: `${CUBE}.type`,
type: `string`,
primary_key: true,
public: true
},
code: {
sql: `${CUBE}.code`,
type: `string`
}
}
})
The following query works with this data model just fine:
{
"measures": [
"dummy_data.actual_prev_month",
"dummy_data.variance"
],
"dimensions": [
"dummy_dim.type"
],
"timeDimensions": [
{
"dimension": "dummy_data.date",
"granularity": "month",
"dateRange": "This year"
}
],
"limit": 5000
}
If we replace actual_cur_month
with actual_prev_month
(a rolling window measure) in the variance
definition, then the same query will fail with the missing FROM-clause entry for table "dummy_dim"
error.
Here's the key difference in generated SQL:

Expected behavior
Correct SQL is generated in both cases.
Version:
0.35.77
Additional context
Reported in Slack: https://cube-js.slack.com/archives/C04NYBJP7RQ/p1724664662312939