Unity Catalog metric views: define, create, query, and manage governed business metrics in YAML. Use when building standardized KPIs, revenue metrics, order analytics, or any reusable business metrics that need consistent definitions across teams and tools.
Define reusable, governed business metrics in YAML that separate measure definitions from dimension groupings for flexible querying.
Use this skill when:
CAN USESELECT on source tables, CREATE TABLE + USE SCHEMA in the target schemaBefore creating a metric view, call get_table_stats_and_schema to understand available columns for dimensions and measures:
get_table_stats_and_schema(
catalog="catalog",
schema="schema",
table_names=["orders"],
table_stat_level="SIMPLE" # Use "DETAILED" for cardinality, min/max, histograms
)
CREATE OR REPLACE VIEW catalog.schema.orders_metrics
WITH METRICS
LANGUAGE YAML
AS $$
version: 1.1
source: catalog.schema.orders
comment: "Orders KPIs for sales analysis"
filter: order_date > '2020-01-01'
dimensions:
- name: Order Month
expr: DATE_TRUNC('MONTH', order_date)
comment: "Month of order"
- name: Order Status
expr: CASE
WHEN status = 'O' THEN 'Open'
WHEN status = 'P' THEN 'Processing'
WHEN status = 'F' THEN 'Fulfilled'
END
comment: "Human-readable order status"
measures:
- name: Order Count
expr: COUNT(1)
- name: Total Revenue
expr: SUM(total_price)
comment: "Sum of total price"
- name: Revenue per Customer
expr: SUM(total_price) / COUNT(DISTINCT customer_id)
comment: "Average revenue per unique customer"
$$
All measures must use the MEASURE() function. SELECT * is NOT supported.
SELECT
`Order Month`,
`Order Status`,
MEASURE(`Total Revenue`) AS total_revenue,
MEASURE(`Order Count`) AS order_count
FROM catalog.schema.orders_metrics
WHERE extract(year FROM `Order Month`) = 2024
GROUP BY ALL
ORDER BY ALL
| Topic | File | Description |
|---|---|---|
| YAML Syntax | yaml-reference.md | Complete YAML spec: dimensions, measures, joins, materialization |
| Patterns & Examples | patterns.md | Common patterns: star schema, snowflake, filtered measures, window measures, ratios |
Use the manage_metric_views tool for all metric view operations:
| Action | Description |
|---|---|
create | Create a metric view with dimensions and measures |
alter | Update a metric view's YAML definition |
describe | Get the full definition and metadata |
query | Query measures grouped by dimensions |
drop | Drop a metric view |
grant | Grant SELECT privileges to users/groups |
manage_metric_views(
action="create",
full_name="catalog.schema.orders_metrics",
source="catalog.schema.orders",
or_replace=True,
comment="Orders KPIs for sales analysis",
filter_expr="order_date > '2020-01-01'",
dimensions=[
{"name": "Order Month", "expr": "DATE_TRUNC('MONTH', order_date)", "comment": "Month of order"},
{"name": "Order Status", "expr": "status"},
],
measures=[
{"name": "Order Count", "expr": "COUNT(1)"},
{"name": "Total Revenue", "expr": "SUM(total_price)", "comment": "Sum of total price"},
],
)
manage_metric_views(
action="query",
full_name="catalog.schema.orders_metrics",
query_measures=["Total Revenue", "Order Count"],
query_dimensions=["Order Month"],
where="extract(year FROM `Order Month`) = 2024",
order_by="ALL",
limit=100,
)
manage_metric_views(
action="describe",
full_name="catalog.schema.orders_metrics",
)
manage_metric_views(
action="grant",
full_name="catalog.schema.orders_metrics",
principal="data-consumers",
privileges=["SELECT"],
)