Skip to content

Lesson 6 ยท Metrics and KPIs

Defining Metrics Precisely

Write metric definitions that two people calculate the same way: grain, filters, dates, nulls and versions, kept in a metric dictionary and a shared SQL view.

Two teams both report "revenue" for November and get different totals. Neither made a typing error. They used different rules. This is a common way for reports to lose trust. The cure is a written definition that is precise enough for two people to calculate the same value independently.

What a definition needs

Element Question it answers
Name and plain meaning What would you tell a new colleague?
Formula What is added, counted or divided?
Grain What does one source row represent?
Inclusions and exclusions Which records count?
Date rule Which date places a record in a period?
Time zone and period When does a day or week start?
Missing values What happens when a field is empty?
Unit and rounding Currency, tax, decimals
Source and owner Where from, and who decides changes?
Version Since when has this rule applied?

Grain comes first

Grain means what one row stands for: one order, one order line, one payment or one customer per day. If you mix grains, totals can multiply without any error message. A typical case is joining orders to order lines and then summing an order-level amount, such as shipping. The shipping fee is repeated once for each line.

SQL A join that silently inflates a total

Press Run (or Ctrl + Enter) to see the result here.

The first query reports 40 and the second 20. When a total changes after you add a join, check the grain before anything else.

Date rules and time zones

An order can have a creation date, payment date, shipping date and delivery date. "November orders" differs for each. Pick the date that matches the question and name it in the definition.

Time zones matter near midnight. An order placed late in the evening on November 30 in one place can already be December 1 in UTC. Choose one reporting time zone, convert consistently and state it. Also say what a week means: the day it starts, and how weeks that cross a month or year are handled.

Missing values and exclusions

An empty value is not the same as zero. If a rating is missing, an average of the existing ratings describes only people who answered. Report how many are missing. In SQL, AVG(column) skips nulls, while replacing nulls with zero changes the meaning completely.

Common exclusions include test accounts, internal orders, canceled transactions and duplicates. List each one, and count how many rows it removes. A sudden jump in excluded rows can point to a data problem.

A metric dictionary

A metric dictionary (or catalog) is the shared place for definitions. It can start as one table in a shared document. This is an illustrative entry.

YAML
metric: net_revenue
meaning: Money from paid orders after refunds, without sales tax
formula: sum(paid item amount) - sum(refunded item amount)
grain: one row per order line
date_rule: payment date for sales; refund date for refunds
time_zone: UTC
excludes: test accounts, canceled orders
currency: converted to USD at the rate stored with each payment
source: orders warehouse table
owner: finance reporting lead
version: 2
changed: "2026-10-01: refunds now counted by refund date"

Whether refunds belong to the sale date or the refund date is a choice for a management report. Formal accounting has its own rules. This tutorial is not accounting advice, so agree such definitions with the people responsible for your financial statements.

Define once, reuse everywhere

A document alone does not stop two queries from drifting apart. Put the rule in one shared place that reports read from, such as a database view, a shared dataset or a semantic layer in a reporting tool.

SQL Put the rule in one view

Press Run (or Ctrl + Enter) to see the result here.

This small example uses one date and a refund column to keep it short, so it is simpler than the dictionary entry above. The point is the pattern: filters live in the view, and every report that uses it applies them in the same way.

Changing a definition

Definitions must change sometimes. Do it visibly:

  1. Describe the old rule, the new rule and the reason.
  2. Calculate both for several past periods and show the difference.
  3. Get agreement from the metric owner and main readers.
  4. Decide whether to restate history. If you cannot, mark the break on charts.
  5. Update the version, the dictionary and a note in each affected report.

Test a definition with edge cases

A definition looks complete until real records test it. Before you publish one, write down awkward cases and decide each answer with the owner.

Case Question to settle
Order paid in October, refunded in November Which month does the refund reduce?
Order partly shipped Does it count as shipped?
Customer with two accounts One customer or two?
Payment in a second currency Which rate, from which date?
Record corrected after the report ran Restate the period or leave it?

The last row deserves a clear policy. Source data often changes after you publish: late records arrive and mistakes are fixed. Decide whether published periods are frozen or restated. If they can be restated, show when the figures were last calculated, so that two different values for the same month can be explained.

A good final test is to hand the written definition to a colleague and ask them to calculate the value for a small sample without seeing your query. If your results differ, the difference shows which sentence needs to be sharper. Add the awkward cases and their answers to the dictionary entry, because they will come up again.

Never change a rule quietly under the same name. A line that jumps because of a definition change looks like a real business event, and someone may act on it.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the grain of a table describe?

    Grain is the meaning of one row. Mixing grains often multiplies totals.
  2. 2. A rating field is empty for 40% of responses. What is the honest approach?

    Readers need to know the average covers only those who answered.
  3. 3. Why put a metric rule in a shared view or dataset?

    One shared implementation prevents separate copies from drifting apart.
  4. 4. What should you do before switching to a new definition?

    A side-by-side comparison shows the effect and prevents false conclusions.

Summary

  • A definition states formula, grain, filters, date rule, time zone, missing values, source, owner and version.
  • Check grain whenever a join changes a total.
  • Empty is not zero, so report how much is missing.
  • Keep a metric dictionary and implement each rule in one shared place.
  • Announce definition changes and mark breaks in history.

Next: Comparisons, Targets and Variance

Your progress is saved in this browser. No account needed.