Skip to main content

Define sales metrics as a metric view

The Query and explore your data page computes daily franchise revenue with one saved query. A metric view turns that logic into a governed object. You define Total Sales, Order Count, and Avg Order Value in one place, and dashboards, Genie Agents, and SQL clients read the same definition. Change a measure in that definition, and queries that run after the change use it.

The source is your silver layer. The output is starterjourney_catalog.bakehouse_gold.franchise_sales_metrics.

Requirements​

You need the following before you create the metric view:

  • CREATE TABLE and USE SCHEMA on the schema where you create the metric view, and USE CATALOG on its catalog. To create the gold schema, you also need CREATE SCHEMA on starterjourney_catalog.
  • SELECT on bakehouse_silver.transactions and bakehouse_silver.franchises.
  • CAN USE on a SQL warehouse, such as Starter Warehouse, or on compute that runs Databricks Runtime 18.1 and above.
note

You can create a metric view on Databricks Runtime 16.4 and above. The examples on this page need 18.1 and above, because the YAML sets rely.at_most_one_match on the join. Synonyms and comments on fields and measures also need YAML specification 1.1. SQL warehouses meet both requirements.

Build the metric view​

Create the gold schema, define the metric view with Genie Code or the fallback YAML, and then check its totals against the saved query.

Create the gold schema​

In the SQL editor, attach a SQL warehouse, such as Starter Warehouse, and run:

SQL
CREATE SCHEMA IF NOT EXISTS starterjourney_catalog.bakehouse_gold;

Create the metric view​

Create an empty metric view in the bakehouse_gold schema, and then open it in the editor:

  1. In Catalog Explorer, open the bakehouse_gold schema, and click Create > Metric view.
  2. In the Create metric view dialog, enter the name franchise_sales_metrics, select the catalog starterjourney_catalog and the schema bakehouse_gold, and then click Create.

Generate the definition with Genie Code​

In the editor, open Genie Code from the icon in the upper-right corner, and paste this prompt:

prompt

Tell Genie Code to do this for you:

Create a metric view on starterjourney_catalog.bakehouse_silver.transactions joined to starterjourney_catalog.bakehouse_silver.franchises on franchise_id, many-to-one. Add fields: franchise (the franchise_name from the join), sales_date (the transaction_timestamp truncated to a day), and product. Add measures: total_sales as SUM(total_price), order_count as COUNT(1), and avg_order_value as total_sales divided by order_count. Add the synonyms "revenue" and "sales" to total_sales, and "AOV" to avg_order_value.

Review the definition​

Check the generated definition against this list:

  • The source is bakehouse_silver.transactions, joined to bakehouse_silver.franchises on franchise_id, many-to-one.
  • The fields are franchise, sales_date (at day level), and product.
  • The measures are total_sales as SUM(total_price), order_count as COUNT(1), and avg_order_value as MEASURE(total_sales) / MEASURE(order_count).

Field and measure names matter. The dashboard and the Genie Agent pages refer to them, so rename any generated name that differs from the list above.

Use the fallback YAML

Open the YAML editor with the <> button, replace the definition with the following, and then continue.

YAML
version: 1.1
comment: 'Franchise sales KPIs for the Bakehouse journey'
source: starterjourney_catalog.bakehouse_silver.transactions
joins:
- name: franchise_dim
source: starterjourney_catalog.bakehouse_silver.franchises
'on': source.franchise_id = franchise_dim.franchise_id
rely:
at_most_one_match: true
fields:
- name: franchise
expr: franchise_dim.franchise_name
comment: 'Franchise name'
- name: sales_date
expr: DATE(source.transaction_timestamp)
comment: 'Calendar day of the transaction'
- name: product
expr: source.product
comment: 'Product sold'
measures:
- name: total_sales
expr: SUM(source.total_price)
comment: 'Sum of transaction total price'
synonyms: ['revenue', 'sales']
- name: order_count
expr: COUNT(1)
comment: 'Number of transactions'
- name: avg_order_value
expr: MEASURE(total_sales) / MEASURE(order_count)
comment: 'Average order value'
synonyms: ['AOV']

Preview and save​

Click Preview to check the fields and measures, and then click Save.

Validate against the saved query​

In the SQL editor, run this query. Wrap each measure in MEASURE(), and list each column instead of using SELECT *.

SQL
SELECT
`sales_date`,
`franchise`,
MEASURE(`total_sales`) AS total_sales
FROM starterjourney_catalog.bakehouse_gold.franchise_sales_metrics
GROUP BY ALL
ORDER BY `sales_date`, `franchise`;

The daily totals per franchise should match the daily franchise revenue query that you saved. See Save the query. If they don't, check the join and the measures.

Certify and hand off​

After the numbers match, apply the system.certification_status system-governed tag with the certified value to the metric view. You need the ASSIGN permission on that tag. See Flag data as certified or deprecated.

Only the owner can edit a metric view's definition. So that a team can maintain it, grant the group SELECT on the source tables, and then transfer ownership of the metric view to the group. Each group member can then edit the definition. See Enable collaborative editing. To transfer ownership of a metric view with materializations, see Change the owner of a materialized metric view.

Troubleshooting​

Use these entries to troubleshoot the create dialog, totals, field names, and the MEASURE() function.

Metric view creation fails or the option is unavailable

Attach a SQL warehouse, or compute that runs Databricks Runtime 18.1 and above, and confirm that you have CAN USE on it.

Totals look inflated

The default join cardinality is many_to_one, which treats the joined table as a dimension lookup. Totals inflate when you set rely.at_most_one_match: true on a franchise join that has more than one row per franchise_id. Databricks doesn't validate the rely setting at runtime, so duplicate matches make SUM and COUNT return wrong results. Confirm that the franchise join is many-to-one on franchise_id, and that franchises has one row per franchise.

Genie Code used different field names

The dashboard and Genie Agent pages use franchise, sales_date, product, total_sales, order_count, and avg_order_value. Rename the generated fields and measures to match, or paste the fallback YAML.

Daily totals don't match the saved query

Check that sales_date truncates transaction_timestamp to a day, and that total_sales sums total_price. Reconcile any time zone difference between the two queries.

A query fails with an error about the MEASURE function

Wrap every measure in MEASURE(), and pass the measure's name, not its display name. If the name contains spaces, wrap it in backticks, such as MEASURE(`Total Revenue`). See Query metric views.

Next steps​