Level 2: AI layer
Metric contracts as code: one definition, many consumers.
Guide 5's contract says what a number means. This piece puts it where every consumer, a report, a subscription, an AI tool, gets the same value and the same answer status.
On this page
Guide 5 argued that self-service needs a contract, not a license: a one-page statement of what a number means, which date it uses, and what it does when data is missing. A contract on a wiki page is a good start. It's also easy to ignore, because the formula people actually run lives somewhere else.
The fix is to put the contract where the number is computed, once, and have every consumer read it from there.
The contract is the spec
Here's the contract for the example's main metric. Every line came from a signed concept card in part 1:
| Field | Value |
|---|---|
| Definition | SUM(amount) over Revenue, adjustments included |
| Date rule | reporting_date |
| Period | required; end date exclusive; no default |
| Never | sum Terms amount as revenue |
| Statuses | COMPLETE · NO_RECORDS_IN_PERIOD · NO_MATCHED_RECORDS · INCOMPLETE_UNKNOWN_PERIOD · INCOMPLETE_AMOUNT |
| Reference | C-001, P-01, April 2025 = 100.00 (COMPLETE) |
| Owner | named in the concept card |
Example data, not from a client.
Two lines matter most for AI. Period: required, no default means “revenue for all time” isn't a question this metric answers. Statuses means the metric says what kind of answer it's giving, not just a value.
Put it in one governed place
Diagram · Metric contracts as code: one definition, many consumers
Each platform has a place where a metric is defined once:
- dbt. Simple metrics are declared in YAML on a semantic model and served to tools through the Semantic Layer APIs.
- Power BI. A measure in one shared semantic model. Report authors get Build permission to create their own reports on it, but no edit role on its workspace, so they can't change the official measure. Certification tells people it's the reviewed one. It's a trust signal, not a lock.
- Tableau. A Pulse metric definition holds the measure, its time dimension and the dimensions viewers can filter by, as the source of truth for every metric built on it.
- Fabric IQ ontology Preview. It can be generated from a Power BI semantic model. Measures come across as metrics, and the semantic model stays the owner of the DAX expression. Generating one needs Read and Build permission on the model.
In the dbt version of the example, the two metrics from the contract look like this. They sit under the revenue semantic model from part 2:
metrics: - name: net_revenue label: Net Revenue (reporting date) description: >- Recognized revenue including adjustments, by reporting date. type: simple agg: sum expr: amount - name: net_revenue_accounting label: Net Revenue (accounting date) type: simple agg: sum expr: amount agg_time_dimension: accounting_date
Asked for April 2025 through MetricFlow, net_revenue returns 100.00 and net_revenue_accounting returns -20.00, the same answers as guide 1. Two dates, two named metrics, no guessing which one “revenue” meant.
Then delete or hide every other copy. A contract in one place and three unofficial formulas elsewhere is still four definitions.
Return a status with the value
A plain metric returns a number or a blank, and a blank can mean several things. The contract's statuses split them. Here's the status logic as SQL, so you can see every case:
WITH scope AS ( SELECT * FROM revenue WHERE customer = 'C-001' AND product = 'P-01' ), d AS ( SELECT COUNT(*) AS scope_rows, COALESCE(SUM(CASE WHEN reporting_date IS NULL THEN 1 ELSE 0 END), 0) AS unknown_date_rows, COALESCE(SUM(CASE WHEN reporting_date >= '2025-04-01' AND reporting_date < '2025-05-01' AND amount IS NULL THEN 1 ELSE 0 END), 0) AS missing_amount_rows, COALESCE(SUM(CASE WHEN reporting_date >= '2025-04-01' AND reporting_date < '2025-05-01' THEN 1 ELSE 0 END), 0) AS period_rows, SUM(CASE WHEN reporting_date >= '2025-04-01' AND reporting_date < '2025-05-01' THEN amount END) AS subtotal FROM scope ) SELECT CASE WHEN scope_rows = 0 THEN 'NO_MATCHED_RECORDS' WHEN unknown_date_rows > 0 THEN 'INCOMPLETE_UNKNOWN_PERIOD' WHEN missing_amount_rows > 0 THEN 'INCOMPLETE_AMOUNT' WHEN period_rows = 0 THEN 'NO_RECORDS_IN_PERIOD' ELSE 'COMPLETE' END AS answer_status, CASE WHEN scope_rows > 0 AND unknown_date_rows = 0 AND missing_amount_rows = 0 AND period_rows > 0 THEN subtotal END AS value FROM d;
| answer_status | value |
|---|---|
| COMPLETE | 100.00 |
Result on the example. Example data, not from a client.
I ran it on the example for each case. April returns COMPLETE with 100.00. July returns NO_RECORDS_IN_PERIOD with no value. Customer C-999, who has no rows at all, returns NO_MATCHED_RECORDS instead of falling through to a misleading “complete.” Add an April row with no date and the answer becomes INCOMPLETE_UNKNOWN_PERIOD, with the value withheld. A row with no amount gets INCOMPLETE_AMOUNT.
The value is withheld, not just flagged. That matters for AI. A tool that receives 100.00 next to “withheld” can easily repeat the 100.00. A tool that receives no number and a status can only say why it can't answer.
Every consumer, same answer
The test of a contract is simple. Ask the reference question, C-001, P-01, April 2025, through every consumer you have: the certified report, the subscription, the notebook, the AI tool. Each one should return 100.00 and COMPLETE. Any that doesn't is reading a copy, or applying its own date.
I keep that reference question in the test suite from guide 7, so the check runs every time the model changes, not just on launch day.
What I check before a metric goes to AI
- Is the metric defined in exactly one governed place, with every copy removed or hidden?
- Does it require a period instead of defaulting to all time?
- Does it return a status, and withhold the value when the data is incomplete?
- Does every consumer return the reference answer: 100.00, COMPLETE?
One definition. Every consumer reads it.
Related
- All 8 parts of this seriesSeries overview
- All the diagrams in one placeDiagrams
- Level 1: Model the data (7 guides)Read level 1
- The 4-question checklistChecklist