cube-js/cube · error · UserError

Unsupported measure type replacement for ${sourceMeasure}: $

Error message

Unsupported measure type replacement for ${sourceMeasure}: ${aggType} => ${newMeasureType}

What it means

When a rolled-up/patched measure replaces an average-type aggregation (avg, etc.) with count_distinct_approx, Cube allows it only in specific cases; for the avg source branch, any newMeasureType not in the whitelisted set (count/count_distinct family that ignores input value) is rejected with this UserError. Averaging measures depend on their input values, so changing to an incompatible aggregation would silently produce wrong numbers.

Source

Thrown at packages/cubejs-schema-compiler/src/adapter/BaseMeasure.ts:43

      switch (aggType) {
        case 'sum':
        case 'avg':
        case 'min':
        case 'max':
          switch (newMeasureType) {
            case 'sum':
            case 'avg':
            case 'min':
            case 'max':
            case 'count_distinct':
            case 'countDistinct':
            case 'count_distinct_approx':
            case 'countDistinctApprox':
              // Can change from avg/... to count_distinct
              // Latter does not care what input value is
              // ok, do nothing
              break;
            default:
              throw new UserError(
                `Unsupported measure type replacement for ${sourceMeasure}: ${aggType} => ${newMeasureType}`
              );
          }
          break;
        case 'count_distinct':
        case 'countDistinct':
        case 'count_distinct_approx':
        case 'countDistinctApprox':
          switch (newMeasureType) {
            case 'count_distinct':
            case 'countDistinct':
            case 'count_distinct_approx':
            case 'countDistinctApprox':
              // ok, do nothing
              break;
            default:
              // Can not change from count_distinct to avg/...

View on GitHub (pinned to 7d981676b3)

Solutions

  1. Change the newMeasureType to one supported for the avg source (e.g. count, count_distinct_approx)
  2. Define a separate measure for the desired aggregation instead of patching the avg measure
  3. Remove the measure replacement from the rollup configuration so the original type is preserved
  4. If it must be summed, define the measure as type: 'sum' in the data model rather than patching

Example fix

// before (rollup patch)
measures: { 'Orders.avgTotal': { aggType: 'avg' } } // patched to newMeasureType 'sum'
// after — define a dedicated measure in schema
measures: { total: { type: 'sum', sql: 'amount' } }
Defensive patterns

Strategy: try-catch

Validate before calling

// Ensure rollup measure replacements keep compatible aggregation types
function checkMeasureReplacement(m) {
  if (m.aggType?.startsWith('avg') && m.newMeasureType &&
      !['count','count_distinct','count_distinct_approx'].includes(m.newMeasureType)) {
    throw new Error(`Cannot patch avg measure to ${m.newMeasureType}`);
  }
}

Try / catch

try {
  await cubeApi.load(query);
} catch (e) {
  if (e.message.startsWith('Unsupported measure type replacement')) {
    console.error('Rollup measure type mismatch:', e.message);
    // rebuild query without the patched measure
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling query.rollup() or building a rollup query with a measure replacement (aggType from an avg-family measure patched to a newMeasureType like 'avg'->'sum' or 'avg'->'countDistinct' variant not permitted), via the query patch API (preAggregation rollups, rollupMeasure with modified aggregation).

Common situations: Defining pre-aggregation rollups where measure types don't match; refactoring data models and reusing a rolled-up measure under a different agg; programmatic query generation replacing measure types.

Related errors


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