cube-js/cube · error · Error

Can't build query for time dimensions with different date ra

Error message

Can't build query for time dimensions with different date ranges

What it means

When multiple time dimensions with granularities and date ranges are present, Cube must generate one shared time series; if the unique date ranges differ, rows would be lost when joining. Cube therefore refuses and throws this Error.

Source

Thrown at packages/cubejs-schema-compiler/src/adapter/BaseQuery.js:2005

          // filter condition again and again. Different granularities don't play role here,
          // as rollingWindow.granularity is used for filtering.
          uniqDateJoinCondition,
          fromRollup,
          false
        ));
      return baseQueryFn(cumulativeMeasures, filters, false);
    }

    if (this.timeDimensions.filter(d => !d.dateRange && d.granularity).length > 0) {
      throw new UserError('Time series queries without dateRange aren\'t supported');
    }

    // We can't do meaningful query if few time dimensions with different ranges passed,
    // it won't be possible to join them together without losing some rows.
    const rangedTimeDimensions = this.timeDimensions.filter(d => d.dateRange && d.granularity);
    const uniqTimeDimensionWithRanges = R.uniqBy(d => d.dateRange, rangedTimeDimensions);
    if (uniqTimeDimensionWithRanges.length > 1) {
      throw new Error('Can\'t build query for time dimensions with different date ranges');
    }

    // We need to generate time series table for the lowest granularity among all time dimensions
    const [dateSeriesDimension, dateSeriesGranularity] = this.timeDimensions.filter(d => d.granularity)
      .reduce(([prevDim, prevGran], d) => {
        const mg = this.minGranularity(prevGran, d.resolvedGranularity());
        if (mg === d.resolvedGranularity()) {
          return [d, mg];
        }
        return [prevDim, mg];
      }, [null, null]);

    const dateSeriesSql = this.dateSeriesSql(dateSeriesDimension);

    // If the same time dimension is passed more than once, no need to build the same
    // filter condition again and again. Different granularities don't play role here,
    // as rollingWindow.granularity is used for filtering.
    const filters = this.segments

View on GitHub (pinned to 7d981676b3)

Solutions

  1. Make all granular time dimensions in the query use the exact same dateRange.
  2. Split into separate queries (one per time dimension range) and merge results in application code.
  3. Remove granularity from the extra time dimensions (turn them into plain range filters) so only one ranged/granular dimension remains.
  4. Wrap query construction and return a user-facing message asking to unify the date range.

Example fix

// before
timeDimensions: [
  { dimension: 'Signups.time', granularity: 'day', dateRange: ['2024-01-01','2024-03-01'] },
  { dimension: 'Purchases.time', granularity: 'day', dateRange: ['2024-02-01','2024-04-01'] }
]
// after
timeDimensions: [
  { dimension: 'Signups.time', granularity: 'day', dateRange: ['2024-01-01','2024-03-01'] },
  { dimension: 'Purchases.time', granularity: 'day', dateRange: ['2024-01-01','2024-03-01'] }
]
Defensive patterns

Strategy: validation

Validate before calling

function requireSameDateRanges(query) {
  const ranges = (query.timeDimensions || [])
    .filter(td => td.granularity && td.dateRange)
    .map(td => JSON.stringify(td.dateRange));
  if (new Set(ranges).size > 1) {
    throw new Error('All granular timeDimensions must share the same dateRange');
  }
}

Try / catch

try {
  return await cubeApi.load(query);
} catch (e) {
  if (/different date ranges/.test(e.message)) {
    console.error('Unify time dimension ranges or split the query');
  }
  throw e;
}

Prevention

When it happens

Trigger: A single query with two or more timeDimensions (each with granularity) whose dateRange values are not identical — R.uniqBy(d => d.dateRange) yields more than one entry.

Common situations: Comparing two events on different windows (e.g. signup range Jan–Mar, purchase range Feb–Apr) in one query; dashboard widgets merging multiple time dimensions with independently chosen ranges.

Related errors


AI-assisted analysis of cube-js/cube@7d981676b3 (2026-09-02). Data as JSON: /api/errors/f45e013dfcb09629. Report an issue: GitHub.