Use parameters with metric views
Parameters let you pass values into a metric view when you query it. Use parameters to reuse a single metric view definition across different inputs, such as a discount rate or a date window. Reference a parameter anywhere a constant SQL expression is valid, such as in filters, field and measure expressions, the source, and joins. Pass a value as an argument when you query the view.
Parameters reduce duplication. Instead of maintaining a separate metric view for each variant, you define the logic once and let each caller supply its own values. A parameter can include a default value, so callers that don't pass an argument fall back to the default.
Materialization isn't currently supported for metric views that define parameters.
Requirements
Querying a metric view that defines parameters requires a compute resource that meets the minimum version requirements. See Metric view feature availability for minimum compute version requirements.
Define parameters
You can define parameters in the Catalog Explorer low-code editor or directly in YAML. After you create a parameter, reference it by name anywhere a constant SQL expression is valid, such as in filters, field and measure expressions, the source, and joins.
To add a parameter in the low-code editor:
- In the editor heading, click Add parameter.
- Enter a parameter name, for example
start_dateoroffset_int. - Enter a default value and data type.
- Click Save.
The parameter appears in the editor heading after you create it.
To define parameters in YAML, add a top-level parameters block to the metric view definition. For the complete field reference and a YAML example, see Parameters.
Query a metric view with parameters
To query a metric view that defines parameters, call the metric view as a table-valued function and pass parameters as arguments. You can pass arguments by name or by position, or omit them to use the default values.
Pass arguments by name
To pass an argument by name, use the => operator. The following example passes a discount value of 0.15:
SELECT product, MEASURE(discountedSales)
FROM catalog.schema.metric_view(discount => 0.15)
GROUP BY product;
Pass arguments by position
To pass arguments by position, list the values in the order the parameters are defined. The following example passes the same discount value of 0.15:
SELECT product, MEASURE(discountedSales)
FROM catalog.schema.metric_view(0.15)
GROUP BY product;
Use default parameter values
To use the default parameter values when you call the metric view, omit arguments. The following queries are equivalent and use all default values:
SELECT product, MEASURE(discountedSales) FROM catalog.schema.metric_view() GROUP BY product;
SELECT product, MEASURE(discountedSales) FROM catalog.schema.metric_view GROUP BY product;
View the parameters a metric view defines
To see the parameters that a metric view defines, view its YAML definition, which includes the parameters block. See View metric view definition and metadata.