Skip to main content
You can use the measures parameter within cubes to define measures. Each measure is an aggregation over a certain column in your database table. Any measure should have the following parameters: name, sql, and type.

Parameters

name

The name parameter serves as the identifier of a measure. It must be unique among all measures, dimensions, and segments within a cube and follow the naming conventions.

title

You can use the title parameter to change a measure’s displayed name. By default, Cube will humanize your measure key to create a display name. In order to override default behavior, please use the title parameter.

description

This parameter provides a human-readable description of a measure. When applicable, it will be displayed in Playground and exposed to data consumers via APIs and integrations.

public

The public parameter is used to manage the visibility of a measure. Valid values for public are true and false. When set to false, this measure cannot be queried through the API. Defaults to true.

meta

Custom metadata. Can be used to pass any information to the frontend. You can also use the ai_context key to provide context to the AI agent without exposing it in the user interface.

sql

sql is a required parameter. It can take any valid SQL expression depending on the type of the measure. Please refer to the [Measure Types Guide][ref-schema-ref-types-formats-measures-types] for detailed information on the corresponding sql parameter.
Depending on the measure type, the sql parameter would either:
  • Be skipped (in case of the count type).
  • Contain an aggregate function, e.g., STRING_AGG(string_dimension, ',') (in case of string, time, boolean, and number types).
  • Contain a non-aggregated expression that Cube would wrap into an aggregate function according to the measure type (in case of the avg, count_distinct, count_distinct_approx, min, max, and sum types).

mask

The optional mask parameter defines the replacement value used when the measure is masked by a data masking access policy. The mask can be a static value (number, boolean, or string) or a SQL expression. When using a SQL expression, it should be an aggregate expression (the same way as the measure’s sql parameter for number type measures), because the mask replaces the entire measure expression including aggregation:
If no mask is defined, the default mask value is NULL. See data masking for more details.
SQL masks on measures are not applied in ungrouped queries (e.g., SELECT * via the SQL API). If you need dynamic masking in ungrouped mode, use a masked dimension instead.

filters

If you want to add some conditions for a metric’s calculation, you should use the filters parameter. The syntax looks like the following:

type

type is a required parameter. There are various types that can be assigned to a measure. A measure can only have one type.
The number_agg type is only available in Tesseract, the next-generation data modeling engine. Tesseract is currently in preview. Use the CUBEJS_TESSERACT_SQL_PLANNER environment variable to enable it.

rolling_window

The rolling_window parameter is used to for rolling window calculations, e.g., to calculate a metric over a moving window of time, e.g. a week or a month.
Rolling window calculations require the query to contain a single time dimension with a provided date range. It is used to calculate the minimum and maximum values for the series of time windows.With Tesseract, the next-generation data modeling engine, rolling window calculations don’t require the date range for the time dimension. Tesseract is currently in preview. Use the CUBEJS_TESSERACT_SQL_PLANNER environment variable to enable it.

offset

The offset parameter is used to specify the starting point of the time window. You can set the window offset parameter to either start or end, which will match the start or end of the window. By default, the offset parameter is set to end.

trailing and leading

The trailing and leading parameters define the size of the time window. The trailing parameter defines the size of the window part before the offset point, and the leading parameter defines the size of the window part after the offset point. These parameters have a format defined as (-?\d+) (minute|hour|day|week|month|year). It means that you can define these parameters using both positive and negative integers. The trailing and leading parameters can also be set to unbounded, which means infinite size for the corresponding window part. By default, the leading and trailing parameters are set to zero.
Here’s an example of an unbounded window that’s used for cumulative counts:

multi_stage

The multi_stage parameter is used to define measures that are used with multi-stage calculations, e.g., time-shift measures.

grain

The grain parameter is used with multi-stage measures to control the dimensions of the inner aggregation stage’s GROUP BY — the grain at which the base measure is computed before the outer aggregation is applied. It accepts an object with three keys, each taking a list of dimension names from the same cube:
  • keep_only — group the inner stage by only the listed dimensions, ignoring the query’s dimensions. Use it for fixed-grain calculations such as percent of total.
  • exclude — group the inner stage by the query’s dimensions minus the listed dimensions. Use it for ranking within groups.
  • include — group the inner stage by the query’s dimensions plus the listed dimensions. Use it for nested aggregates (an aggregate of an aggregate).
keep_only and exclude are mutually exclusive.
grain replaces the standalone group_by, reduce_by, and add_group_by parameters, which remain supported. To migrate, use grain.keep_only instead of group_by, grain.exclude instead of reduce_by, and grain.include instead of add_group_by.

group_by, reduce_by, and add_group_by (legacy)

These three parameters were the original way to control the inner aggregation stage’s GROUP BY for multi-stage measures. They are still supported, but grain now covers all three and is the recommended way to express the grain of a multi-stage measure. Each accepts a list of dimension names from the same cube. For new data models, use grain instead.

filter

The filter parameter is used with multi-stage measures to override the filter context that the inner aggregation stage inherits from the query. By default, a multi-stage measure inherits all filters applied to the query; filter lets you drop, replace, or extend those filters at the inner stage. This is commonly used for “share of total” calculations where the denominator must ignore a filter applied by the query — for example, computing each status’s share of the per-category total while the query is filtered to a single status.
The filter parameter requires the Tesseract SQL planner (CUBEJS_TESSERACT_SQL_PLANNER=true).
filter accepts the following keys, which can be combined: The mode key controls how the override interacts with the filter context of an upstream multi-stage measure when measures are chained:
  • relative (default): the override is applied relative to the filter context inherited from the chain.
  • fixed: the override is applied as an absolute filter context, ignoring the filters inherited from upstream multi-stage measures.
At the top level (not inside a chain), relative and fixed are equivalent. An include entry uses the same shape as a query filter, and and / or groups can be nested arbitrarily:

time_shift

The time_shift parameter is used to configure a time shift for a measure. It accepts an array of time shift configurations that consist of time_dimension, type, interval, and name parameters.

type and interval

These parameters define the time shift direction and size. The type can be either prior (shifting time backwards) or next (shifting time forwards). The interval parameter defines the size of the time shift and has the following format: quantity unit, e.g., 1 year or 7 days.

time_dimension

The time_dimension parameter is used to specify the time dimension for the time shift. If it’s omitted, Cube will apply the time shift to all time dimensions in the query. In this case, only single time shift configuration is allowed in time_shift. If time_dimension is specified, the time shift will only happen if the query contains this very time dimension. This is useful if you’d like to apply different time shifts to different time dimensions or if you want to apply a time shift only when a specific time dimension is present in the query.

name

The name parameter is used to reference a named time shift that is defined on a time dimension from a calendar cube. Named time shifts are used in cases when different measures use the same time shift configuration (e.g., prior + 1 year) but have to be shifted differently depending on the custom calendar.
Named time shifts also allow to reuse the same time shift configuration across multiple measures and cubes where they are defined.

case

The case parameter is used to define conditional measures, i.e., measures that are calculated based on the value of a switch dimension.
case measures are powered by Tesseract, the next-generation data modeling engine. Tesseract is currently in preview. Use the CUBEJS_TESSERACT_SQL_PLANNER environment variable to enable it.
You do not need to include the sql parameter if the case parameter is used. However, the multi_stage parameter must be set to true for case measures.

format

format is an optional parameter. It controls how measure values are displayed to data consumers. You can use a named format or a custom d3-format specifier string. Named formats accept an optional _N suffix (0–6) to set the maximum decimal precision; insignificant trailing zeros are trimmed, so N is an upper bound rather than a fixed number of digits. Without a suffix, named formats default to a maximum of 2 decimal places. For full control, you can also pass any valid d3-format specifier string directly (e.g., $,.2f, .0%, .2s).

currency

The optional currency parameter specifies the ISO 4217 currency code for the measure. Use it alongside format: currency to indicate which currency the values represent, so downstream tools can display the appropriate symbol. The value is a 3-letter currency code (e.g., USD, EUR, GBP) and is case-insensitive.
The currency parameter can only be used with numeric measure types (sum, avg, min, max, number, count, count_distinct, count_distinct_approx). Using it with string, boolean, or time measures will result in a validation error.

drill_members

Using the drill_members parameter, you can define a set of drill down fields for the measure. drill_members is defined as an array of dimensions. Cube automatically injects dimensions’ names and other cubes’ names with dimensions in the context, so you can reference these variables in the drill_members array. Learn more about how to define and use drill downs.