---
title: Metric view YAML syntax reference
description: "Complete YAML syntax reference for metric view definitions: version, source, joins, fields, measures, filters, window measures, and materialization."
canonical_url: https://docs.databricks.com/aws/en/uc-semantics/metric-views/yaml-reference
---

# Metric view YAML syntax reference

Metric view definitions use standard YAML syntax to declare the source, joins, fields, measures, filters, window measures, and materialization. The following sections document the complete grammar for each.

For minimum runtime and YAML specification version requirements for each feature, see [Metric view feature availability](https://docs.databricks.com/aws/en/uc-semantics/metric-views/feature-availability.md).

See [YAML Specification 1.2.2](https://yaml.org/spec/1.2.2/) documentation to learn more about YAML specifications.

## <a id="edit-yaml" />Edit YAML in the metric view editor

You can write and edit the YAML described on this page directly in the metric view editor. In Catalog Explorer, open a metric view and click the `<>` button to edit the definition. To generate YAML from a natural language description instead, open Genie Code from the editor. For the full editor walkthrough, see [Create a metric view](https://docs.databricks.com/aws/en/uc-semantics/metric-views/create.md).

## Top-level YAML fields

The YAML definition for a metric view includes the following top-level fields:

| Field             | Type   | Description                                                                                                                                                                                                                                                                                                                                                                                            |
| ----------------- | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `version`         | String | Required. The version of the metric view YAML specification that the definition uses, such as `1.1`. This is the version of the specification format, not a revision number that you assign to your own definition. Use one of the supported specification versions. See [YAML specification versions](https://docs.databricks.com/aws/en/uc-semantics/metric-views/feature-availability.md#versions). |
| `comment`         | String | Optional. Description of the metric view.                                                                                                                                                                                                                                                                                                                                                              |
| `source`          | String | Required. The source data for the metric view. Can be any table-like Unity Catalog asset including a metric view or a SQL query. See [Source](https://docs.databricks.com/aws/en/uc-semantics/metric-views/yaml-reference.md#source).                                                                                                                                                                  |
| `parameters`      | Array  | Optional. Named values that callers pass when they query the metric view as a table-valued function. See [Parameters](https://docs.databricks.com/aws/en/uc-semantics/metric-views/yaml-reference.md#parameters).                                                                                                                                                                                      |
| `filter`          | String | Optional. A SQL boolean expression that applies to all queries. See [Filter](https://docs.databricks.com/aws/en/uc-semantics/metric-views/yaml-reference.md#filter).                                                                                                                                                                                                                                   |
| `joins`           | Array  | Optional. Star schema and snowflake schema joins. See [Joins](https://docs.databricks.com/aws/en/uc-semantics/metric-views/yaml-reference.md#joins).                                                                                                                                                                                                                                                   |
| `fields`          | Array  | Conditional. Field definitions including name, expression, and optional semantic metadata. Required if no `measures` are specified. See [Fields](https://docs.databricks.com/aws/en/uc-semantics/metric-views/yaml-reference.md#fields). The `dimensions` keyword is accepted as a synonym for backward compatibility.                                                                                 |
| `measures`        | Array  | Conditional. Measure definitions including name, aggregate expression, and optional semantic metadata. Required if no `fields` are specified. See [Measures](https://docs.databricks.com/aws/en/uc-semantics/metric-views/yaml-reference.md#measures).                                                                                                                                                 |
| `materialization` | Object | Optional. Configuration for accelerating queries with materialized views. Includes refresh schedule and materialized view definitions. See [Materialization](https://docs.databricks.com/aws/en/uc-semantics/metric-views/yaml-reference.md#materialization).                                                                                                                                          |

## <a id="source" />Source

The `source` field specifies the data source for the metric view. Supported sources include tables, views, metric views, and SQL queries. To use a metric view as a source, see [Metric view source](https://docs.databricks.com/aws/en/uc-semantics/metric-views/yaml-reference.md#metric-view-source).

### Table-like asset source

Reference a table-like asset using its three-part name:

```yaml
source: catalog.schema.source_table
```

### SQL query source

To use a SQL query, write the query text directly in the YAML:

```yaml
source: SELECT * FROM samples.tpch.orders o
  LEFT JOIN samples.tpch.customer c
  ON o.o_custkey = c.c_custkey
```

> [!NOTE]
>
> When using a SQL query as a source with a `JOIN` clause, set primary and foreign key constraints on underlying tables and use the `RELY` option for optimal query performance. For more information, see [Declare primary key, foreign key, and unique constraints](https://docs.databricks.com/aws/en/tables/constraints.md#pk-fk) and [Optimize queries with `RELY` on primary key and unique constraints](https://docs.databricks.com/aws/en/sql/user/queries/query-optimization-constraints.md).

### <a id="metric-view-source" />Metric view source

A metric view can be the source for another metric view. The new metric view can reference the fields (previously called dimensions) and measures of the parent metric view, so you don't need to redefine them. To inherit all fields and measures from the parent metric view, use the `source.*` wildcard in both the `fields` and `measures` definitions. Then add new fields and measures that build on the inherited ones. See [Bulk-import fields and measures with wildcards](https://docs.databricks.com/aws/en/uc-semantics/metric-views/yaml-reference.md#wildcard-expressions) and [Composability](https://docs.databricks.com/aws/en/uc-semantics/metric-views/advanced-techniques.md#composability).

The following definition uses a metric view as its source, imports all of its fields and measures, and adds a composed measure that references two measures from the parent metric view:

```yaml
version: 1.1
source: catalog.schema.parent_metric_view

fields:
  # Inherit all fields from the parent metric view
  - expr: source.*

measures:
  # Inherit all measures from the parent metric view
  - expr: source.*

  # Add a measure composed from parent measures
  - name: avg_order_value
    expr: MEASURE(source.total_revenue) / MEASURE(source.order_count)
```

Wildcards require Databricks Runtime 18.2 and above with YAML specification 1.1. Databricks expands wildcards when you create the metric view. To pick up fields or measures added to the parent metric view later, recreate the metric view with `CREATE OR REPLACE` or `ALTER`.

## <a id="parameters" />Parameters

The `parameters` block defines named values that callers pass when they query the metric view as a table-valued function. For when and how to use parameters, including querying a parameterized metric view, see [Use parameters with metric views](https://docs.databricks.com/aws/en/uc-semantics/metric-views/use-parameters.md).

Each parameter definition includes the following fields:

| Field       | Type   | Description                                                                                                                                                                                                                                                                        |
| ----------- | ------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `name`      | String | Required. The parameter name. Reference the parameter by this name in field and measure expressions, and pass it as a named argument when you query the metric view.                                                                                                               |
| `data_type` | String | Required. The SQL data type of the parameter, such as `double`, `int`, `string`, or `date`.                                                                                                                                                                                        |
| `default`   | Varies | Optional. The value used when a caller does not pass the parameter. The default must be castable to `data_type`, and it can't reference another parameter or contain a subquery. If you set a default for one parameter, every parameter that follows it must also have a default. |

The following example defines a `discount` parameter and references it in a measure expression:

```yaml
version: 1.1
source: main.default.sales

parameters:
  - name: discount
    data_type: double
    default: 0

fields:
  - name: product
    expr: product

measures:
  - name: discountedSales
    expr: SUM((1 - discount) * amount)
```

## <a id="filter" />Filter

A filter in the YAML definition applies to all queries that reference the metric view. Write filters as SQL boolean expressions.

```yaml
# Single condition filter
filter: o_orderdate > '2024-01-01'

# Multiple conditions with AND
filter: o_orderdate > '2024-01-01' AND o_orderstatus = 'F'

# Multiple conditions with OR
filter: o_orderpriority = '1-URGENT' OR o_orderpriority = '2-HIGH'

# Complex filter with IN clause
filter: o_orderstatus IN ('F', 'P') AND o_orderdate >= '2024-01-01'

# Filter with NOT
filter: o_orderstatus != 'O' AND o_totalprice > 1000.00

# Filter with LIKE pattern matching
filter: o_comment LIKE '%express%' AND o_orderdate > '2024-01-01'
```

## <a id="joins" />Joins

Joins in metric views support both direct joins from a fact table to dimension tables ([star schema](https://www.databricks.com/glossary/star-schema)) and multi-hop joins across normalized dimension tables ([snowflake schemas](https://www.databricks.com/glossary/snowflake-schema)). You can also join to a SQL query using a `SELECT` statement. See [Use a SQL query as a source](https://docs.databricks.com/aws/en/uc-semantics/metric-views/basic-modeling.md#query-source).

> [!NOTE]
>
> Joined tables can't include `MAP` type columns. To unpack values from `MAP` type columns, see [Explode nested elements from a map or array](https://docs.databricks.com/aws/en/semi-structured/complex-types.md#map-array).

Each join definition includes the following fields:

| Field         | Type   | Description                                                                                                                                                                                                                                                                                                                                   |
| ------------- | ------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `name`        | String | Required. Alias for the joined table or SQL query. Use this alias when referencing columns from the joined table in fields or measures.                                                                                                                                                                                                       |
| `source`      | String | Required. Three-part name of the table to join. Can also be a SQL query.                                                                                                                                                                                                                                                                      |
| `on`          | String | Conditional. Boolean expression defining the join condition. Required if `using` is not specified.                                                                                                                                                                                                                                            |
| `using`       | Array  | Conditional. List of column names present in both the parent table and joined table. Required if `on` is not specified.                                                                                                                                                                                                                       |
| `cardinality` | String | Optional. Defaults to `many_to_one`. The relationship between the source and the joined table. Set to `one_to_many` to aggregate a table that has multiple matching rows per source row as a separate fact source. See [One-to-many joins](https://docs.databricks.com/aws/en/uc-semantics/metric-views/yaml-reference.md#one-to-many-joins). |
| `joins`       | Array  | Optional. A list of nested join definitions for snowflake schema modeling. See [Metric view feature availability](https://docs.databricks.com/aws/en/uc-semantics/metric-views/feature-availability.md) for minimum runtime requirements.                                                                                                     |
| `rely`        | Map    | Optional. Promises about the join that the analyzer can rely on to produce more efficient query plans. See [Optimize joins with `rely`](https://docs.databricks.com/aws/en/uc-semantics/metric-views/yaml-reference.md#join-rely).                                                                                                            |

### Star schema joins

In a star schema, the `source` is the fact table and joins with one or more dimension tables using a `LEFT OUTER JOIN`. Metric views join the fact and dimension tables needed for the specific query, based on the selected columns.

Specify join columns using either an `ON` clause or a `USING` clause:

* `ON` clause: Uses a boolean expression to define the join condition.
* `USING` clause: Lists columns with the same name in both the parent table and the joined table.

The join must follow a many-to-one relationship. In cases of many-to-many, the first matching row from the joined dimension table is selected.

```yaml
version: 1.1
source: samples.tpch.lineitem

joins:
  - name: orders
    source: samples.tpch.orders
    on: source.l_orderkey = orders.o_orderkey

  - name: part
    source: samples.tpch.part
    on: source.l_partkey = part.p_partkey

fields:
  - name: Order Status
    expr: orders.o_orderstatus

  - name: Part Name
    expr: part.p_name

measures:
  - name: Total Revenue
    expr: SUM(l_extendedprice * (1 - l_discount))

  - name: Line Item Count
    expr: COUNT(1)
```

> [!NOTE]
>
> The `source` namespace references columns from the metric view's source, while a join's `name` refers to columns from that joined table. For example, in `source.l_orderkey = orders.o_orderkey`, `source` refers to `lineitem` and `orders` refers to the joined table. If no prefix is provided in an `on` clause, the reference defaults to the joined table.

### Snowflake schema joins

A snowflake schema extends a star schema by normalizing dimension tables and connecting them to subdimensions. This creates a multi-level join structure. See [Metric view feature availability](https://docs.databricks.com/aws/en/uc-semantics/metric-views/feature-availability.md) for minimum runtime requirements.

To define a snowflake schema, nest `joins` inside a parent join definition:

```yaml
version: 1.1
source: samples.tpch.orders

joins:
  - name: customer
    source: samples.tpch.customer
    'on': o_custkey = c_custkey
    joins:
      - name: nation
        source: samples.tpch.nation
        'on': c_nationkey = n_nationkey

fields:
  - name: customer_nation
    expr: customer.nation.n_name
```

### <a id="one-to-many-joins" />One-to-many joins

The `cardinality` field sets the relationship between the source and a joined table. The default, `many_to_one`, treats the joined table as a dimension lookup. Set `cardinality: one_to_many` to treat the joined table as a fact source that the engine aggregates independently at the source grain, which lets a single source row match multiple rows in the joined table. One-to-many joins require Databricks Runtime 18.1 or above and YAML specification version 1.1. See [Metric view feature availability](https://docs.databricks.com/aws/en/uc-semantics/metric-views/feature-availability.md).

The following rules apply to one-to-many joins:

* A one-to-many column can't be used in a `fields` definition, because a field must resolve to a single value per source row.
* A single aggregation function must reference columns from one source. You can apply arithmetic across the results of separate aggregations, such as `count(orders.order_id) / count(*)`.
* All descendants of a one-to-many join must also be `one_to_many`. Top-level sibling joins can mix cardinalities.
* Reference a column in a nested join with its full dot-path through the join names, such as `orders.order_items.item_id`.

> [!NOTE]
>
> When a metric view uses a `one_to_many` join, its materializations qualify for exact match only. Rollup match is not available. See [Rollup match](https://docs.databricks.com/aws/en/uc-semantics/metric-views/materialization.md#rollup-match).

The following example joins `orders` to a `customers` source with `cardinality: one_to_many` so that order measures aggregate without duplicating customer rows:

```yaml
version: 1.1
source: main.sales.customers

joins:
  - name: orders
    source: main.sales.orders
    on: orders.customer_id = source.customer_id
    cardinality: one_to_many

fields:
  - name: customer_name
    expr: customer_name

measures:
  - name: customer_count
    expr: count(*)
  - name: order_count
    expr: count(orders.order_id)
  - name: total_order_revenue
    expr: sum(orders.amount)
```

For conceptual details and nested and sibling join examples, see [Join cardinality](https://docs.databricks.com/aws/en/uc-semantics/metric-views/joins.md#join-cardinality).

### <a id="join-rely" />Optimize joins with `rely`

Use the `rely` field on a join to declare guarantees about the relationship that the query analyzer uses when planning queries. These guarantees allow the engine to plan queries more efficiently and reduce data scanned, especially when fields from the joined table are referenced in filters.

The `rely` map supports the following fields:

| Field               | Type    | Description                                                                                                                                                                      |
| ------------------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `at_most_one_match` | Boolean | Optional. Defaults to `false`. When `true`, declares that at most one row in the joined table matches each row in the source (a many-to-one relationship that does not fan out). |

> [!WARNING]
>
> Set `at_most_one_match: true` only when the join is many-to-one. This relationship is not validated at runtime. If multiple rows in the joined table match a single source row, measures (such as `SUM` and `COUNT`) return incorrect results.

The following example enables `at_most_one_match` on a many-to-one join from `orders` to `customer`. Queries that filter or group by customer attributes benefit the most:

```yaml
version: 1.1
source: samples.tpch.orders

joins:
  - name: customer
    source: samples.tpch.customer
    on: source.o_custkey = customer.c_custkey
    rely:
      at_most_one_match: true

fields:
  - name: Customer name
    expr: customer.c_name
  - name: Customer market segment
    expr: customer.c_mktsegment

measures:
  - name: Total revenue
    expr: SUM(o_totalprice)
```

## <a id="fields" /><a id="dimensions" />Fields

> [!NOTE]
>
> `fields` and `dimensions` are equivalent keywords in a metric view definition. `fields` is the preferred term and is used throughout this documentation. The Catalog Explorer low-code editor labels these columns **Fields**, but the YAML it generates uses the `dimensions` keyword. Existing metric views that use `dimensions` continue to work, and both keywords are accepted on new or updated definitions.

Fields are metric view columns used in `SELECT`, `WHERE`, and `GROUP BY` clauses at query time. Each expression must return a scalar value. Fields can reference columns from the source data or earlier-defined fields in the metric view.

A field can be either:

* A categorical or grouping column, such as a region, status, or department.
* An unaggregated numeric column, such as an age, price, or quantity. Numeric fields can be aggregated at query time using SQL functions such as `SUM` or `AVG`.

Each field definition includes the following properties:

| Property       | Type   | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| -------------- | ------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `name`         | String | Optional on Databricks Runtime 19 and above. The column alias for the field. If omitted, Databricks derives the name from `expr`. See [Derive column names from expressions](https://docs.databricks.com/aws/en/uc-semantics/metric-views/yaml-reference.md#derived-column-names). For wildcard expressions, `name` isn't allowed. See [Bulk-import fields and measures with wildcards](https://docs.databricks.com/aws/en/uc-semantics/metric-views/yaml-reference.md#wildcard-expressions). |
| `expr`         | String | Required. A SQL expression that can reference columns from the source data or a previously defined field. Can be a wildcard to import all columns from the source or a joined table. See [Bulk-import fields and measures with wildcards](https://docs.databricks.com/aws/en/uc-semantics/metric-views/yaml-reference.md#wildcard-expressions).                                                                                                                                               |
| `comment`      | String | Optional. Description of the field. Appears in Unity Catalog and documentation tools.                                                                                                                                                                                                                                                                                                                                                                                                         |
| `display_name` | String | Optional. Label that appears in visualization tools. Limited to 255 characters. Requires YAML specification 1.1. See [Metric view feature availability](https://docs.databricks.com/aws/en/uc-semantics/metric-views/feature-availability.md).                                                                                                                                                                                                                                                |
| `format`       | Map    | Optional. Format specification for how values are displayed. Requires YAML specification 1.1. See [Format specifications](https://docs.databricks.com/aws/en/uc-semantics/agent-metadata.md#format-specifications).                                                                                                                                                                                                                                                                           |
| `synonyms`     | Array  | Optional. Alternative names for AI and BI tools to discover the field. Up to 10 synonyms, each limited to 255 characters. Requires YAML specification 1.1. See [Synonyms](https://docs.databricks.com/aws/en/uc-semantics/agent-metadata.md#synonyms).                                                                                                                                                                                                                                        |

> [!WARNING]
>
> String-like metric view fields are always `STRING`, even when the source column is `CHAR` or `VARCHAR`. Because `CHAR(n)` space-padding is lost, comparisons can return different results. For example, `column = 'COLLEGE'` matches a `CHAR(10)` value in the source table (which is space-padded) but not in the metric view field.

Example:

```yaml
fields:
  # Basic field
  - name: order_date
    expr: o_orderdate
    comment: 'Date the order was placed'
    display_name: 'Order Date'

  # Field with SQL expression
  - name: order_month
    expr: DATE_TRUNC('MONTH', o_orderdate)
    display_name: 'Order Month'

  # Field with synonyms
  - name: order_status
    expr: CASE
      WHEN o_orderstatus = 'O' THEN 'Open'
      WHEN o_orderstatus = 'P' THEN 'Processing'
      WHEN o_orderstatus = 'F' THEN 'Fulfilled'
      END
    display_name: 'Order Status'
    synonyms: ['status', 'fulfillment status']
```

## <a id="measures" />Measures

Measures are expressions that produce results without a pre-determined level of aggregation. They must be expressed using aggregate functions. To reference a measure in a query, use the `MEASURE` function. Measures can reference base columns in the source data, earlier-defined fields, or earlier-defined measures.

Each measure definition includes the following fields:

| Field          | Type   | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| -------------- | ------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `name`         | String | Optional on Databricks Runtime 19 and above. The alias for the measure. If omitted, Databricks derives the name from `expr`. See [Derive column names from expressions](https://docs.databricks.com/aws/en/uc-semantics/metric-views/yaml-reference.md#derived-column-names). For wildcard expressions, `name` isn't allowed. See [Bulk-import fields and measures with wildcards](https://docs.databricks.com/aws/en/uc-semantics/metric-views/yaml-reference.md#wildcard-expressions). |
| `expr`         | String | Required. A SQL expression containing one or more aggregate functions. Can be a wildcard to import all measures from a metric view source. See [Bulk-import fields and measures with wildcards](https://docs.databricks.com/aws/en/uc-semantics/metric-views/yaml-reference.md#wildcard-expressions).                                                                                                                                                                                    |
| `comment`      | String | Optional. Description of the measure. Appears in Unity Catalog and documentation tools.                                                                                                                                                                                                                                                                                                                                                                                                  |
| `display_name` | String | Optional. Label that appears in visualization tools. Limited to 255 characters. Requires YAML specification 1.1. See [Metric view feature availability](https://docs.databricks.com/aws/en/uc-semantics/metric-views/feature-availability.md).                                                                                                                                                                                                                                           |
| `format`       | Map    | Optional. Format specification for how values are displayed. Requires YAML specification 1.1. See [Format specifications](https://docs.databricks.com/aws/en/uc-semantics/agent-metadata.md#format-specifications).                                                                                                                                                                                                                                                                      |
| `synonyms`     | Array  | Optional. Alternative names for AI and BI tools to discover the measure. Up to 10 synonyms, each limited to 255 characters. Requires YAML specification 1.1. See [Metric view feature availability](https://docs.databricks.com/aws/en/uc-semantics/metric-views/feature-availability.md).                                                                                                                                                                                               |
| `window`       | Array  | Optional. Window specifications for windowed, cumulative, or semiadditive aggregations. When not specified, the measure behaves as a standard aggregate. See [Window measures](https://docs.databricks.com/aws/en/uc-semantics/metric-views/yaml-reference.md#window-measures).                                                                                                                                                                                                          |

See [Aggregate functions](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-functions-builtin.md#aggregate-functions) for a list of aggregate functions.

Example:

```yaml
measures:
  # Simple count measure
  - name: order_count
    expr: COUNT(1)
    display_name: 'Order Count'

  # Sum aggregation measure with synonyms
  - name: total_revenue
    expr: SUM(o_totalprice)
    comment: 'Gross revenue from all orders'
    display_name: 'Total Revenue'
    synonyms: ['revenue', 'total sales']

  # Distinct count measure
  - name: unique_customers
    expr: COUNT(DISTINCT o_custkey)
    display_name: 'Unique Customers'

  # Calculated measure combining multiple aggregations
  - name: avg_order_value
    expr: SUM(o_totalprice) / COUNT(DISTINCT o_orderkey)
    display_name: 'Avg Order Value'
    synonyms: ['AOV', 'average order']

  # Filtered measure with WHERE condition
  - name: open_order_revenue
    expr: SUM(o_totalprice) FILTER (WHERE o_orderstatus = 'O')
    display_name: 'Open Order Revenue'
    synonyms: ['backlog', 'outstanding revenue']
```

## <a id="derived-column-names" />Derive column names from expressions

**Applies to:** Databricks SQL and Databricks Runtime 19 and above

If you omit `name` from a field or measure definition, Databricks derives the column name from `expr`. For complex expressions, use an explicit name to make the column easier to query. For example, add `name: total_revenue` to the measure definition to query it as `MEASURE(total_revenue)` instead of ``MEASURE(`SUM(o_totalprice)`)``.

The following rules determine the name:

* A direct column reference uses its final identifier. For example, `source.o_orderstatus` produces `o_orderstatus`.
* A `CAST` directly around a column reference retains that identifier. For example, `CAST(source.o_totalprice AS BIGINT)` produces `o_totalprice`.
* Other expressions use an expression-derived name. For example, `SUM(o_totalprice)` produces `SUM(o_totalprice)`. The name preserves function and identifier casing and removes whitespace between expression tokens. For example, `SUM(a + b)` produces `SUM(a+b)`.

Unlike wildcard-imported names, derived names can be referenced in later field or measure expressions. Reference a field as `o_orderstatus` and a measure as ``MEASURE(`SUM(o_totalprice)`)``.

The following definition omits names for one field and one measure:

```yaml
version: 1.1
source: samples.tpch.orders

fields:
  - expr: source.o_orderstatus

measures:
  - expr: SUM(o_totalprice)
```

If you create a metric view named `orders_metrics` with this definition, use `DESCRIBE TABLE` to inspect its column names:

```sql
DESCRIBE TABLE orders_metrics;
```

Enclose derived names that contain punctuation in backticks when you query them:

```sql
SELECT o_orderstatus, MEASURE(`SUM(o_totalprice)`)
FROM orders_metrics
GROUP BY o_orderstatus;
```

Names must be unique across fields and measures, ignoring case. If derived names collide, specify explicit, unique names. An explicitly empty `name` is invalid.

## <a id="wildcard-expressions" />Bulk-import fields and measures with wildcards

**Applies to:** Databricks Runtime 18.2 and above with YAML specification 1.1

In a `fields` or `measures` definition, you can use a wildcard (`*`) in the `expr` field to import all columns from the source or a joined table without listing each one. This is useful when you want a metric view to expose every column from an upstream asset, similar to `SELECT *` in a standard view. Databricks expands the wildcard to concrete columns when you create or replace the metric view, and derives each column name from the source column name.

Like explicit column definitions, wildcard expressions are expanded when you create the metric view. To pick up columns added to the source later, recreate the metric view with `CREATE OR REPLACE` or `ALTER`.

Wildcards support the following forms:

| Syntax                                | Description                                                                                                                           |
| ------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
| `source.*`                            | Import all columns from the metric view source.                                                                                       |
| `<join>.*`                            | Import all columns from a joined table, referenced by its join name. Nested joins use the full dot-path, such as `customer.nation.*`. |
| `<target>.* EXCEPT (col1, col2, ...)` | Import all columns from the target except those listed.                                                                               |
| `<target>.<struct>.*`                 | Expand the fields of a `STRUCT` column into separate columns.                                                                         |

The following rules apply to wildcard expressions:

* Omit the `name` field. Databricks derives column names from the source, so `name` isn't allowed on a wildcard expression.
* Semantic metadata isn't allowed on a wildcard expression. Don't set `comment`, `display_name`, `format`, or `synonyms` on a wildcard. To add metadata to a specific column, exclude it from the wildcard with `EXCEPT` and define it explicitly.
* In a `measures` definition, a wildcard imports measures only from a metric view source. Base tables have no measures, so a wildcard expands to no measures when the source is a base table.
* You can't reference a wildcard-imported column by its derived name in a later `fields` or `measures` expression. Reference the source column with its full path instead.

### Resolve name collisions

When you import columns from more than one source with a wildcard, columns that share a name (such as `id` or `date`) collide and cause an error when you save the definition. To resolve a collision, exclude the column from each wildcard with `EXCEPT`, then define it explicitly with a unique name:

```yaml
fields:
  - expr: source.* EXCEPT (id)
  - expr: customer.* EXCEPT (id)
  - name: source_id
    expr: source.id
  - name: customer_id
    expr: customer.id
```

### Wildcard example

The following definition imports all columns from the source and from a joined table, excludes two columns, and defines one column explicitly to add metadata:

```yaml
version: 1.1
source: samples.tpch.orders

joins:
  - name: customer
    source: samples.tpch.customer
    on: source.o_custkey = customer.c_custkey
    joins:
      - name: nation
        source: samples.tpch.nation
        on: customer.c_nationkey = nation.n_nationkey

fields:
  # Import all columns from the source
  - expr: source.*

  # Import all columns from a joined table, excluding two
  - expr: customer.nation.* EXCEPT (n_name, n_comment)

  # Define a specific column explicitly to add metadata
  - name: nation_name
    expr: customer.nation.n_name
    comment: "Customer's nation"
    display_name: 'Nation Name'
```

## <a id="window-measures" />Window measures

The `window` field defines windowed, cumulative, or semiadditive aggregations for measures. For detailed information about window measures and use cases, see [Window measures](https://docs.databricks.com/aws/en/uc-semantics/metric-views/advanced-techniques.md#window-measures).

Each window specification includes the following fields:

| Field          | Type              | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| -------------- | ----------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `order`        | String            | Required. The field that determines the ordering of the window. [(1)](https://docs.databricks.com/aws/en/uc-semantics/metric-views/yaml-reference.md#window-order-footnote)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| `range`        | String            | Required. The extent of the window. See [Supported `range` values](https://docs.databricks.com/aws/en/uc-semantics/metric-views/yaml-reference.md#supported-range-values). The numeric value in a `trailing` or `leading` range can be an integer parameter instead of a literal, so a caller passes the window size in at query time. See [Pass a window size as a parameter](https://docs.databricks.com/aws/en/uc-semantics/metric-views/yaml-reference.md#window-parameters).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| `semiadditive` | String            | Optional on Databricks Runtime 19 and above. Aggregation method. Supported values: `first` or `last`. The default is `last`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| `offset`       | String or integer | Optional. Requires YAML specification version 1.1 or above. Shifts the window frame backward or forward along the `order` field by a fixed amount. A negative value looks backward and a positive value looks forward. `offset` supports two forms. A **dated offset** has the form `<n> <period>`, where `period` is one of `day`, `days`, `month`, `months`, `year`, or `years`, for example `-12 month` or `1 year`. A dated offset requires the `order` field to be a date or timestamp column and requires Databricks Runtime 18.1 or above. A **numeric offset** is a unitless signed integer, such as `-1` or `3`, that steps the frame by that many positions along a consecutive integer `order` column. A numeric offset requires Databricks Runtime 19 or above. `offset` has no effect on `range: all`. If the shifted frame falls outside the available data, the measure evaluates to `NULL`. The signed integer can be an integer parameter instead of a literal, so a caller passes the offset in at query time. The sign must be part of the parameter's value, not written before the parameter name. See [Pass a window size as a parameter](https://docs.databricks.com/aws/en/uc-semantics/metric-views/yaml-reference.md#window-parameters). For dated examples, see [How `offset` shifts the window frame](https://docs.databricks.com/aws/en/uc-semantics/metric-views/advanced-techniques.md#offset). For numeric-index examples, see [Window measures on a numeric index column](https://docs.databricks.com/aws/en/uc-semantics/metric-views/advanced-techniques.md#numeric-index). |

<a id="window-order-footnote" />

**(1)** The referenced field must be deterministic. Non-deterministic expressions such as `rand()`, `uuid()`, or `current_timestamp()` produce unpredictable window ordering and can lead to incorrect aggregation results.

### <a id="supported-range-values" />Supported `range` values

* `current`: Rows where the window ordering value equals the anchor row's value.
* `cumulative`: All rows where the window ordering value is less than or equal to the anchor row's value.
* `trailing <value> [<unit>] [inclusive | exclusive]`: Rows from the anchor row going backward by the specified amount, for example `trailing 7 day`. The optional `inclusive` or `exclusive` modifier requires Databricks Runtime 18.1 and YAML specification version 1.1 or above, and controls whether the anchor row is included in the window. The default is `exclusive`. See [Include or exclude the anchor row](https://docs.databricks.com/aws/en/uc-semantics/metric-views/advanced-techniques.md#anchor-row).
* `leading <value> [<unit>] [inclusive | exclusive]`: Rows from the anchor row going forward by the specified amount, for example `leading 3 month`. The optional `inclusive` or `exclusive` modifier requires Databricks Runtime 18.1 and YAML specification version 1.1 or above, and controls whether the anchor row is included in the window. The default is `exclusive`. See [Include or exclude the anchor row](https://docs.databricks.com/aws/en/uc-semantics/metric-views/advanced-techniques.md#anchor-row).
* `all`: All rows regardless of the window ordering value.

`<unit>` is one of `day`, `days`, `month`, `months`, `year`, or `years`, and requires the `order` field to be a date or timestamp column. Omit `<unit>` to write a **unitless range** such as `trailing 3` or `leading 2`, which steps by that many positions along a consecutive integer `order` column instead of by a time unit. A unitless range requires Databricks Runtime 19 or above. See [Window measures on a numeric index column](https://docs.databricks.com/aws/en/uc-semantics/metric-views/advanced-techniques.md#numeric-index).

### Window measure example

The following example calculates a rolling 7-day count of unique customers:

```yaml
version: 1.1
source: samples.tpch.orders

fields:
  - name: order_date
    expr: o_orderdate

measures:
  - name: rolling_7day_customers
    expr: COUNT(DISTINCT o_custkey)
    display_name: '7-Day Rolling Customers'
    window:
      - order: order_date
        range: trailing 7 day
```

### <a id="window-parameters" />Pass a window size as a parameter

Instead of hardcoding the numeric value in a `trailing` or `leading` `range` or in an `offset`, you can reference a [parameter](https://docs.databricks.com/aws/en/uc-semantics/metric-views/yaml-reference.md#parameters), so a caller passes the window size in when they query the metric view. This requires a SQL warehouse or other compute resource running Databricks Runtime 18.2 or above.

The following rules apply to a parameter used as a window size:

* The parameter's `data_type` must be integral, such as `int`, `smallint`, or `bigint`.
* The value must be a bare parameter name, not an expression. For example, use `trailing window_size day`, not `trailing window_size + 1 day`. You also can't write a sign before the parameter name, such as `-window_size` in an `offset`. To pass a negative offset, put the sign inside the parameter's value.
* The parameter can't be named after a window keyword, such as a range type (`trailing`, `leading`), a period (`day`, `month`, `year`), an inclusivity keyword (`inclusive`, `exclusive`), or `offset`.
* The unit stays literal. You can parameterize only the numeric magnitude, not the period.

The following example defines a `window_size` parameter and references it in a `trailing` range, so each caller chooses the number of days in the rolling window:

```yaml
version: 1.1
source: samples.tpch.orders

parameters:
  - name: window_size
    data_type: int
    default: 7

fields:
  - name: order_date
    expr: o_orderdate

measures:
  - name: rolling_customers
    expr: COUNT(DISTINCT o_custkey)
    display_name: 'Rolling Customers'
    window:
      - order: order_date
        range: trailing window_size day
```

To query a metric view that defines parameters, see [Query a metric view with parameters](https://docs.databricks.com/aws/en/uc-semantics/metric-views/use-parameters.md#query-parameterized).

## <a id="materialization" />Materialization

The `materialization` field configures automatic query acceleration using materialized views. For detailed information about how materialization works, requirements, and best practices, see [Materialization for metric views](https://docs.databricks.com/aws/en/uc-semantics/metric-views/materialization.md).

> [!NOTE]
>
> You can't materialize a metric view that defines parameters.

The `materialization` field includes the following top-level fields:

| Field                | Type   | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| -------------------- | ------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `schedule`           | String | Optional. Refresh schedule. Uses the same syntax as the [schedule clause on materialized views](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-ddl-create-materialized-view.md#syntax). If omitted, materializations are refreshed only manually. To trigger a manual refresh, see [Manual refresh](https://docs.databricks.com/aws/en/uc-semantics/metric-views/materialization.md#manual-refresh). The `TRIGGER ON UPDATE` clause is not supported. |
| `mode`               | String | Required. Must be set to `relaxed`.                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| `materialized_views` | Array  | Required. List of materialized views to materialize. Each entry requires the fields described below.                                                                                                                                                                                                                                                                                                                                                                         |

Each entry in `materialized_views` includes the following fields:

| Field            | Type   | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| ---------------- | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `name`           | String | Required. The name of the materialization.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| `type`           | String | Required. Type of materialization. Supported values: `aggregated` (requires `dimensions`, `measures`, or both) or `unaggregated`. Only one `unaggregated` entry is allowed per metric view. Unaggregated entries don't use the `dimensions` or `measures` fields.                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| `dimensions`     | Array  | Conditional. List of field names to materialize, using the `dimensions` keyword even if your top-level definition uses `fields`. Required if `type` is `aggregated` and no `measures` are specified.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| `measures`       | Array  | Conditional. List of measure names to materialize. Required if `type` is `aggregated` and no `dimensions` are specified.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| `cluster_by`     | Object | Optional. Clustering columns for the materialization, equivalent to the [`CLUSTER BY` clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-ddl-create-materialized-view.md) on a materialized view. Specify `cols` with a list of column names, or set `auto: true` to let Databricks choose the clustering columns automatically.                                                                                                                                                                                                                                                                                                                                            |
| `partition_by`   | Array  | Optional. List of columns to partition the materialization by, equivalent to the [`PARTITIONED BY` clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-ddl-create-materialized-view.md) on a materialized view. You can't use `partition_by` and `cluster_by` in the same materialization.                                                                                                                                                                                                                                                                                                                                                                                   |
| `refresh_policy` | String | Optional. Requires YAML specification version 1.1 and Databricks Runtime 18 LTS or above. Controls whether the materialization refreshes incrementally. Supported values: `auto` (default), `incremental`, `incremental strict`, and `full`. Values aren't case-sensitive. Write `incremental strict` with a space. The values behave like the `REFRESH POLICY` clause on a materialized view. See [Refresh policy](https://docs.databricks.com/aws/en/ldp/incremental-refresh.md#refresh-policy). With `incremental` or `incremental strict`, creating or altering the metric view fails with `METRIC_VIEW_MATERIALIZATION_NOT_INCREMENTALIZABLE` if the materialization can't refresh incrementally. |

> [!NOTE]
>
> The materialization block uses the `dimensions:` keyword rather than `fields:`. Use `dimensions:` when listing fields to materialize, even if your top-level definition uses `fields:`.

### Materialization example

The following example defines a metric view with multiple materializations:

```yaml
version: 1.1
source: prod.operations.orders_enriched_view
filter: revenue > 0
# filter, fields, and measures can't use invoker-dependent expressions: no current_user(), is_member(), etc.
# source can't have RLS, column masking, or ABAC policies

joins:
  - name: customers
    source: prod.operations.customers
    on: source.customer_id = customers.id
    # if one-to-many, all materializations below drop to exact match only

fields:
  - name: category
    expr: substring(category, 5)
  - name: order_date
    expr: order_date

measures:
  - name: total_revenue
    expr: SUM(revenue)

  - name: number_of_suppliers
    expr: COUNT(DISTINCT supplier_id)

  - name: revenue_for_open_orders
    expr: SUM(revenue) FILTER (WHERE status = 'O')

  - name: blended_margin
    expr: SUM(revenue) - SUM(cost)

  - name: rolling_7day_customers
    expr: COUNT(DISTINCT customer_id)
    window:
      - order: order_date
        range: trailing 7 day

materialization:
  schedule: every 6 hours
  mode: relaxed

  materialized_views:
    - name: baseline
      type: unaggregated
      refresh_policy: incremental strict
      # only one allowed per metric view; doesn't use dimensions or measures keys
      # no benefit if source is an unfiltered direct table reference

    - name: daily_status_metrics
      type: aggregated
      dimensions:
        - order_date
        - category # avoid overly granular dimensions, such as millisecond timestamps
      measures:
        - total_revenue # rollup-eligible
        - number_of_suppliers # exact match only (non-additive)
        - revenue_for_open_orders # rollup-eligible (deterministic filter)
        - blended_margin # exact match only (multiple aggregates)
        - rolling_7day_customers # exact match only (window measure)
      cluster_by:
        cols:
          - order_date
          - category
```

## <a id="column-references" />Column name references

When referencing column names that contain spaces or special characters in YAML expressions, enclose the column name in backticks. If the expression starts with a backtick and is used directly as a YAML value, wrap the entire expression in double quotes. Valid YAML values cannot start with a backtick.

## <a id="formatting-examples" />Formatting examples

Use the following examples to learn how to format YAML correctly in common scenarios.

### <a id="reference-column-name" />Reference a column name

The following examples show how to format column references depending on the characters they contain.

#### <a id="no-spaces" />No spaces

Source column: `revenue`

```yaml
expr: "revenue"
expr: 'revenue'
expr: revenue
```

Use double quotes, single quotes, or no quotes around the column name.

#### <a id="column-name-with-spaces" />Column name with spaces

Source column: `` `First Name` ``

```yaml
expr: '`First Name`'
```

Use backticks to escape spaces. Enclose the entire expression in double quotes.

#### <a id="column-names-with-spaces-in-sql" />Column names with spaces in a SQL expression

Source columns: `` `First Name` ``, `` `Last Name` ``

```yaml
expr: CONCAT(`First Name`, ' ', `Last Name`)
```

If the expression doesn't start with a backtick, double quotes are not required.

#### <a id="column-name-containing-quotes" />Column name containing quotes

Source column: `"name"`

```yaml
expr: '`"name"`'
```

Use backticks to escape the double quotes in the column name. Enclose the expression in single quotes.

### <a id="expressions-with-colons" />Expressions with colons

```yaml
expr: "CASE WHEN `Customer Tier` = 'Enterprise: Premium' THEN 1 ELSE 0 END"
```

> [!NOTE]
>
> YAML interprets unquoted colons as key-value separators. Always use double quotes around expressions that include colons.

### <a id="multi-line-indentation" />Multi-line expressions

```yaml
expr: |
  CASE WHEN
    revenue > 100 THEN 'High'
  ELSE 'Low'
  END
```

> [!NOTE]
>
> Use the `|` block scalar after `expr:` for multiline expressions. All lines must be indented at least two spaces beyond the `expr` key for correct parsing.

## <a id="upgrade-yaml" />Upgrade to YAML 1.1

Upgrading a metric view to YAML specification version 1.1 requires care, because comments are handled differently than in earlier versions.

### Types of comments

* **YAML comments (`#`)**: Inline or single-line comments written directly in the YAML file.
* **Unity Catalog comments**: Comments stored in Unity Catalog for the metric view or its columns. These are separate from YAML comments.

### <a id="upgrade-recs" />Upgrade considerations

Select the upgrade path that matches how you want to handle comments in your metric view.

#### Option 1: Preserve YAML comments using notebooks or the SQL editor

If your metric view contains YAML comments (`#`) that you want to keep, use the following steps:

1. Use the `ALTER VIEW` command in a notebook or SQL editor.
2. Copy the original YAML definition into the `$$..$$` section after `AS`. Change the value of `version` to `1.1`.
3. Save the metric view.

```sql
ALTER VIEW metric_view_name AS
$$
# The notebook preserves inline comments
version: 1.1
source: samples.tpch.orders
fields:
- name: order_date # The notebook preserves inline comments
  expr: o_orderdate
measures:
# The notebook preserves commented out definitions
# - name: total_orders
#   expr: COUNT(o_orderid)
- name: total_revenue
  expr: SUM(o_totalprice)
$$
```

> [!WARNING]
>
> Running `ALTER VIEW` removes Unity Catalog comments unless they are explicitly included in the `comment` fields of the YAML definition. To preserve comments shown in Unity Catalog, see [Option 2](https://docs.databricks.com/aws/en/uc-semantics/metric-views/yaml-reference.md#option-2).

#### <a id="option-2" />Option 2: Preserve Unity Catalog comments

> [!NOTE]
>
> The following guidance applies only when using the `ALTER VIEW` command in a notebook or SQL editor. If you upgrade your metric view to version 1.1 using the YAML editor UI, the YAML editor UI automatically preserves your Unity Catalog comments.

1. Copy all Unity Catalog comments into the appropriate `comment` fields in your YAML definition. Change the value of `version` to `1.1`.
2. Save the metric view.

```sql
ALTER VIEW metric_view_name AS
$$
version: 1.1
source: samples.tpch.orders
comment: "Metric view of order (Updated comment)"

fields:
- name: order_date
  expr: o_orderdate
  comment: "Date of order - Copied from Unity Catalog"

measures:
- name: total_revenue
  expr: SUM(o_totalprice)
  comment: "Total revenue"
$$
```

For YAML specification version history and minimum runtime requirements for each feature, see [Metric view feature availability](https://docs.databricks.com/aws/en/uc-semantics/metric-views/feature-availability.md).
