Skip to the page
Tecyfy

Databricks metric views: examples and limitations

A metric view defines a business number once in Unity Catalog, as YAML, and computes it correctly at any grouping with MEASURE(). A complete example on the TPC-H sample, how to query it, how Power BI and Tableau use it, and the limits to know: no SELECT *, no query-time joins, runtime versions, and materialization.

Tecyfy · Published · 8 min read

A jug marked Revenue pouring into 3 glasses of different sizes, each filled to its mark, for Databricks metric views

A Databricks metric view is a business number defined once, in YAML, in Unity Catalog. You write the formula, such as revenue divided by distinct customers, and the fields people may group by. Every query then asks for the measure with MEASURE() and Databricks computes it from the source rows at whatever grouping was asked. Dashboards, Genie and SQL all get the same answer.

It fixes a quiet bug most BI setups have: numbers that are right in one report and wrong when someone regroups them.

Why not a normal view?

A normal view fixes its grouping when you create it. Store revenue per customer by region and month, and anyone who wants it by region alone has to average the monthly figures, which is wrong whenever months have different numbers of customers. Distinct counts break the same way: a customer who bought in 2 months is counted twice.

Try both ways on 7 orders:

Try itWhy a metric view beats a pre-aggregated view
SELECT region, AVG(revenue_per_customer)
FROM sales_by_region_month
GROUP BY region
regionRevenue per customerRight answer
North187.5225 Off
South170140 Off

Wrong in 2 of 2 rows: an average of monthly averages weights a 1-customer month the same as a 3-customer month. North shows 187.5 instead of 225.

Our own 7 orders. The standard view stores one row per region and month, with each measure already computed; regrouping it can only add or average those rows. A metric view keeps the formula and runs it on the orders for the grouping asked.

Revenue adds up, so any approach gets it right. Customer counts and ratios do not, and those are the numbers people argue about in meetings.

A complete example

This is Databricks' own example, with its comments trimmed, on the TPC-H sample data every workspace has. It joins orders to customers, adds 3 fields to group by and 3 measures:

CREATE OR REPLACE VIEW orders_metric_view WITH METRICS LANGUAGE YAML AS
$$
  version: 1.1
  comment: "Orders KPIs for sales analysis"
  source: samples.tpch.orders
  joins:
    - name: customer
      source: samples.tpch.customer
      'on': source.o_custkey = customer.c_custkey
      rely:
        at_most_one_match: true
  filter: source.o_orderdate > '1990-01-01'
  fields:
    - name: Order Month
      expr: DATE_TRUNC('MONTH', source.o_orderdate)
    - name: Order Status
      expr: |-
        CASE
          WHEN source.o_orderstatus = 'O' THEN 'Open'
          WHEN source.o_orderstatus = 'P' THEN 'Processing'
          WHEN source.o_orderstatus = 'F' THEN 'Fulfilled'
        END
    - name: Market Segment
      expr: customer.c_mktsegment
  measures:
    - name: Order Count
      expr: COUNT(1)
    - name: Total Revenue
      expr: SUM(source.o_totalprice)
    - name: Total Revenue per Customer
      expr: SUM(source.o_totalprice) / COUNT(DISTINCT source.o_custkey)
$$;

What each part does:

KeyMeaning
sourceThe table, view or SQL query the metrics are computed from
joinsDimension tables, many-to-one by default. at_most_one_match promises each order has one customer, a safe speed-up here
filterA condition applied to every query
fieldsWhat people may group and filter by (older YAML calls them dimensions)
measuresAggregate formulas, computed at query time for the grouping asked

You need SELECT on the source, USE CATALOG, USE SCHEMA and CREATE TABLE where the view goes, and compute on Databricks Runtime 17.3 or later for this example. Metric views in general need 16.4 or later.

Query it

SELECT
  `Market Segment`,
  MEASURE(`Total Revenue`),
  MEASURE(`Total Revenue per Customer`)
FROM orders_metric_view
GROUP BY ALL;

Change Market Segment to Order Month and revenue per customer is computed again for each month from the orders, not averaged from segments. 3 rules to remember:

  • No SELECT *. Every measure must be wrapped in MEASURE(), so list the columns.
  • No joins at query time. Query the metric view in a CTE, then join the CTE to other tables.
  • Read the definition with DESCRIBE TABLE EXTENDED orders_metric_view AS JSON.

Power BI, Tableau and other tools

ToolHow it reads a metric view
AI/BI dashboards, GenieDirectly; MEASURE() is applied for you
Power BIThe Databricks connector's Native query, with MEASURE(), in DirectQuery mode
TableauCustom SQL with MEASURE(), or BI compatibility mode, which rewrites Tableau's SUM and COUNT into the measure logic
Anything elseA standard "wrapper" view that selects the measures for a fixed set of fields; one per combination a report needs

Moving from an existing BI model? Give Genie Code a Tableau (.twb, .twbx, .tds, .tdsx) or Power BI (.pbit, .tmdl) file with /importBI and it writes the metric view for you. Genie Code covers what that costs.

Limitations

  • Flat columns only. Fields, measures and joins work on scalar columns. Turn ARRAY and MAP columns into rows with explode(), or into one value with array_size(), in the source query.
  • Only the owner edits it. To share editing, transfer ownership to a group. That does not work for materialized metric views.
  • ALTER VIEW replaces everything. Send the full YAML, not the changed lines.
  • Materialization has limits. You can pre-compute aggregations to speed queries up, but with a one-to-many join a query must match a materialization exactly; it cannot be rolled up from a finer one.
  • Dropping is permanent. Check the Lineage tab for dashboards and queries that use it first.

Beyond the basics, metric views also have window measures for trailing averages, period-over-period change and running totals.

Why it matters more now

Genie reads metric views. They are the part of the Genie Ontology you define yourself, so the formula you write is the one Genie One chat can use for business users. The idea is also no longer Databricks-only: Apache Spark 4.2 added CREATE VIEW ... WITH METRICS to open-source Spark (what's new in Spark 4.2).

The general idea is a semantic layerSemantic LayerOne shared definition of business terms and metrics, such as revenue or active customer, that reports, dashboards and AI assistants all read instead of writing their own.. To build one on a real gold table, our data engineering course has a part on metric views.

Questions

What is a metric view in Databricks?
A metric view is a Unity Catalog object that defines business measures once, in YAML, separately from the fields you group and filter by. A query asks for a measure with MEASURE() and any grouping, and Databricks computes it from the source rows for that grouping. A ratio such as revenue per customer stays correct at every level, which a standard view with fixed aggregations cannot do.
How do you query a Databricks metric view?
Select the fields you want, wrap each measure in MEASURE(), and group by the fields: SELECT `Order Month`, MEASURE(`Total Revenue`) FROM orders_metric_view GROUP BY ALL. SELECT * does not work, because every measure needs MEASURE(). To join a metric view to another table, query it in a CTE first and join the CTE.
Which Databricks Runtime do metric views need?
Creating a metric view is supported on Databricks Runtime 16.4 and above. Databricks' own example with YAML version 1.1 needs Runtime 17.3 or above, and some YAML features need later runtimes. Querying needs a SQL warehouse or compute you can use.
Can Power BI and Tableau use metric views?
Yes. Power BI uses the Databricks connector's Native query option with the MEASURE() pattern in DirectQuery mode; Tableau uses Custom SQL or BI compatibility mode, which rewrites the tool's SUM and COUNT into the right measure logic. For any tool, a standard wrapper view over the metric view works like a table. Genie Code can also turn a Tableau or Power BI file into a metric view.
Are metric views in open-source Apache Spark?
Yes, since Apache Spark 4.2.0, released in July 2026, with CREATE VIEW ... WITH METRICS (SPARK-54119). Databricks' metric views in Unity Catalog add governance, BI tool support, materialization and use by Genie.