---
title: Use level of detail (LOD) expressions in metric views
description: Use level of detail expressions in metric views to control aggregation granularity independently of the fields in your query.
canonical_url: https://docs.databricks.com/aws/en/uc-semantics/metric-views/level-of-detail
---

# Use level of detail (LOD) expressions in metric views

Level of detail (LOD) expressions let you specify the granularity at which to calculate aggregations, independently of the fields (also called dimensions) in your query. This lets you compute metrics like percent of total, where the denominator aggregates at a coarser grain than the query.

## <a id="what-are-lod" />What are level of detail expressions?

Level of detail expressions enable you to specify exactly which fields to use when calculating an aggregate, regardless of the fields present in your query. This gives you fine-grained control over the scope of your calculations.

There are three types of level of detail expressions:

* **Fixed level of detail**: Aggregate over a pre-defined set of fields specified in the expression itself, ignoring other fields in the query.
* **Coarser level of detail**: Aggregate at a coarser granularity than the query by excluding specific fields from the grouping.
* **Finer level of detail**: Aggregate at a finer granularity than the query by including additional fields, then reaggregate the result back to the query's grain.

## <a id="fixed-lod" />Fixed level of detail

A fixed level of detail expression computes an aggregate at a granularity that you define, ignoring the fields in your query. In metric views, fixed LOD expressions are defined directly in the `expr` field of a column definition using SQL window functions with `PARTITION BY` clauses.

### <a id="fixed-when-to-use" />When to use a fixed level of detail

Use fixed level of detail expressions when you need the following:

* **No dependency on query groupings**: Metrics with static partitioning across all uses.
* **Dataset-level aggregates**: Global aggregates compared with row-level groupings (for example, percent of total sales by priority).
* **Multi-level hierarchies**: Detail-level and rollup-level metrics available in the same metric view.

### <a id="fixed-syntax" />Syntax

Fixed LOD expressions use [SQL window functions](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-window-functions.md) to compute aggregates at a defined granularity. Place the window function directly in the `expr` field of a field definition:

```yaml
fields:
  - name: <lod_name>
    expr: <AGGREGATE_FUNCTION>(<column>) OVER (PARTITION BY <dim1>, <dim2>, ...)
```

To aggregate over the entire dataset, omit the `PARTITION BY` clause and leave empty parentheses after `OVER`.

### <a id="fixed-example" />Example: total sales by order priority

Suppose you want to define a metric view where each order's sales can be compared alongside the total sales for its priority group. The following example computes `priority_total_price` in the source query and exposes it as an identity field:

```yaml
version: 1.1

source: samples.tpch.orders

fields:
  - name: order_priority
    expr: o_orderpriority
  - name: order_date
    expr: o_orderdate
  - name: priority_total_price
    expr: SUM(o_totalprice) OVER (PARTITION BY o_orderpriority)

measures:
  - name: total_sales
    expr: SUM(o_totalprice)

  - name: pct_of_priority_total
    expr: SUM(o_totalprice) / ANY_VALUE(priority_total_price)
```

The `priority_total_price` field defines the fixed total for each priority group directly in its `expr` field. The `pct_of_priority_total` measure divides individual order sales by that fixed total to produce a percentage, regardless of how the query groups results.

> [!NOTE]
>
> When referencing a fixed level of detail field in a measure expression, wrap it in an aggregate function. Use `ANY_VALUE` when the value is constant within a group, as in the previous example.

**Create the metric view using SQL**

To create this metric view outside Catalog Explorer, wrap the YAML in `CREATE OR REPLACE VIEW ... WITH METRICS LANGUAGE YAML AS` and place the definition between the `$$` delimiters:

```sql
CREATE OR REPLACE VIEW catalog.schema.sales_by_priority WITH METRICS LANGUAGE YAML AS
$$
  version: 1.1

  source: samples.tpch.orders

  fields:
    - name: order_priority
      expr: o_orderpriority
    - name: order_date
      expr: o_orderdate
    - name: priority_total_price
      expr: SUM(o_totalprice) OVER (PARTITION BY o_orderpriority)

  measures:
    - name: total_sales
      expr: SUM(o_totalprice)

    - name: pct_of_priority_total
      expr: SUM(o_totalprice) / ANY_VALUE(priority_total_price)
$$
```

The coarser level of detail example on this page follows the same pattern.

### <a id="fixed-filtering" />Filtering on fixed level of detail expressions

Fixed level of detail expressions are computed before any query-time filters are applied. To apply a filter to a fixed LOD calculation, include the filter condition within the window function expression using a `CASE` statement or a `FILTER` clause.

## <a id="coarser-lod" />Coarser level of detail

A coarser level of detail expression aggregates at a coarser granularity than the query by excluding one or more fields from the partition. In metric views, coarser LOD expressions are implemented using [window measures](https://docs.databricks.com/aws/en/uc-semantics/metric-views/advanced-techniques.md#window-measures) with the `all` range specification.

### <a id="coarser-when-to-use" />When to use a coarser level of detail

Use coarser level of detail expressions when you need:

* **Dynamic groupings**: Aggregates that adapt to query groupings (for example, percent of total for any selected field).
* **Filter-aware aggregations**: Compute at a coarser granularity while respecting query-time filters.

### <a id="coarser-syntax" />Syntax

For each field to exclude from the partition, define a window measure with `range: all`:

```yaml
measures:
  - name: <measure_name>
    expr: <AGGREGATE_EXPRESSION>
    window:
      - order: <field_to_exclude>
        range: all
```

To exclude multiple fields, add an entry to the `window` array for each field.

### <a id="coarser-example-pct-total" />Example: percent of total sales

To calculate the percentage of total sales for each order priority:

```yaml
version: 1.1

source: samples.tpch.orders

fields:
  - name: order_priority
    expr: o_orderpriority

measures:
  - name: total_sales
    expr: SUM(o_totalprice)

  - name: all_priorities_sales
    expr: SUM(o_totalprice)
    window:
      - order: order_priority
        range: all

  - name: pct_of_total_sales
    expr: SUM(o_totalprice) / MEASURE(all_priorities_sales)
```

In this example:

* `total_sales` aggregates at the query's grouping level.
* `all_priorities_sales` uses `range: all` to compute a grand total across all order priorities, ignoring the `order_priority` field in the query.
* `pct_of_total_sales` divides priority-level sales by the grand total to produce a percentage.

## <a id="finer-lod" />Finer level of detail

A finer level of detail expression computes an aggregate at a finer granularity than your query, then reaggregates the result back to the query's grain. The inner aggregate is evaluated at the query's fields plus one or more additional fields that you include, and an outer aggregate rolls those values back up. In metric views, finer LOD expressions are measures that use a `partition` block with an `include` list and an `outer_aggregate` function.

### <a id="finer-when-to-use" />When to use a finer level of detail

Use finer level of detail expressions when you need:

* **Aggregations of aggregations**: Calculate a per-entity aggregate, such as total revenue per customer, and then summarize it, such as the average across customers, at the query's grain.
* **Grain-independent summaries**: Compute the inner metric at a finer grain than the query and roll it up automatically as the query grain changes.

### <a id="finer-syntax" />Syntax

Define a measure with a `partition` block. The `partition` block adds fields to the query's grain for the inner aggregate and specifies the aggregate that rolls the result back up:

```yaml
measures:
  - name: <measure_name>
    expr: <INNER_AGGREGATE_EXPRESSION>
    partition:
      include: [<field1>, <field2>, ...]
      outer_aggregate: <AGGREGATE_FUNCTION>
```

In this syntax:

* `expr` is the inner aggregate. It's evaluated at the query's grain plus the fields in `include`.
* `include` lists one or more fields to add to the query's grain when computing the inner aggregate. Each entry must be a field defined in the metric view.
* `outer_aggregate` is the aggregate function that rolls the inner results back up to the query's grain.

> [!NOTE]
>
> The `outer_aggregate` must be a single-argument aggregate function: `SUM`, `AVG`, `MIN`, `MAX`, `COUNT`, or `MEDIAN`. `COUNT(DISTINCT ...)` and multi-argument functions such as `PERCENTILE` aren't supported as the outer aggregate, though you can use them in `expr`. A finer level of detail measure can't also use a `window` block, and it can't reference another finer or window measure.

### <a id="finer-example-avg-per-customer" />Example: average revenue per customer

Suppose you want the average revenue per customer, summarized at whatever grain your query uses, such as order priority. Compute total revenue per customer as the inner aggregate, then average across customers with the outer aggregate:

```yaml
version: 1.1

source: samples.tpch.orders

fields:
  - name: order_priority
    expr: o_orderpriority
  - name: customer
    expr: o_custkey

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

  - name: avg_revenue_per_customer
    expr: SUM(o_totalprice)
    partition:
      include: [customer]
      outer_aggregate: AVG
```

In this example:

* `total_revenue` aggregates at the query's grouping level.
* `avg_revenue_per_customer` computes `SUM(o_totalprice)` at the (`order_priority`, `customer`) grain, giving each customer's total revenue within a priority, then uses `outer_aggregate: AVG` to average those per-customer totals back up to the query's grain.

Query the measure like any other measure. The `include` fields are added to the query's grain only while the inner aggregate is computed:

```sql
SELECT order_priority, MEASURE(avg_revenue_per_customer)
FROM catalog.schema.orders_mv
GROUP BY order_priority
```

Each row returns the average per-customer revenue for that order priority.

## Additional resources

* [Window measures](https://docs.databricks.com/aws/en/uc-semantics/metric-views/advanced-techniques.md#window-measures): The underlying mechanism for coarser level of detail expressions in metric views.
* [Unity Catalog metric views](https://docs.databricks.com/aws/en/uc-semantics/metric-views/index.md): Overview of metric view components and modeling patterns.
* [Level of detail (LOD) expressions](https://docs.databricks.com/aws/en/dashboards/manage/data-modeling/custom-calculations/level-of-detail.md): LOD expressions in AI/BI dashboards.
