Level 1: Model the data

Find, fix, recheck

Test the model before you hand it to AI.

Turn the checklists from this series into tests that run every time the model changes: row counts, keys, date rules, the three empty cases and reference answers.

Updated November 2026Example data, not from a client.
On this page
  1. Find, fix, recheck
  2. The short set
  3. The SQL version
  4. The Power BI version
  5. When to run them
  6. What I check

Every guide in this series ended with a checklist. A checklist you run by hand gets run once, before launch. Then the model changes, and nobody runs it again.

This guide turns those checklists into tests that run every time the model changes, and before any tool, AI or otherwise, gets pointed at it.

Find, fix, recheck

The pattern is simple. A test finds a problem. You fix the model. The same test runs again and passes. Then it keeps running, so the problem can't come back quietly.

Figure 1 · The loop

Find, fix, recheck: the test loop A loop. The model changes, with a new column, relationship or source. The tests run every time. On a fail, fix the model, not the test, then recheck by running the tests again. On a pass, publish, then hand the model over. A failure stops the publish; a pass doesn't stop the tests. Model changes new column, relationship, source Run the tests every time fail pass Fix the model not the test Publish then hand it over recheck A failure stops the publish. A pass doesn't stop the tests.
The tests run on every change. A failure stops the publish. Open full size

One rule I hold to: when a test fails, fix the model, not the test. If the test itself was wrong, change it on purpose, write down why, and have the person who owns the number agree.

The short set

You don't need hundreds of tests to start. Five kinds cover most of what goes wrong, and each one comes from an earlier guide:

  • Row counts. A join must not change the row count of the table being summed. (Guide 1)
  • Key uniqueness. Each claimed key identifies one row. (Guide 2)
  • Date rules. Every row has the dates its measures need, or it's counted as unknown. (Guide 5)
  • The three empty cases. No rows, a zero and an unknown date give three different results. (Guide 5)
  • Reference answers. Known questions return known numbers: 100.00, -20.00 and 400.00.

Copy-paste: the SQL version

One query, one row per test, with the expected value, the actual value and a result. Run it against the example and every row passes.

SELECT test, expected, actual,
       CASE WHEN actual = expected
            THEN 'PASS' ELSE 'FAIL'
       END AS result
FROM (
  SELECT 'join keeps row count' AS test,
    (SELECT COUNT(*) FROM revenue
      WHERE reporting_date >= '2025-04-01'
        AND reporting_date < '2025-05-01')
      AS expected,
    (SELECT COUNT(*) FROM revenue r
       JOIN customer c
         ON c.customer = r.customer
       JOIN product  p
         ON p.product  = r.product
      WHERE r.reporting_date >= '2025-04-01'
        AND r.reporting_date < '2025-05-01')
      AS actual
  UNION ALL
  SELECT 'revenue id is unique', 0,
    (SELECT COUNT(*) FROM (SELECT id
       FROM revenue
      GROUP BY id HAVING COUNT(*) > 1))
  UNION ALL
  SELECT 'customer key is unique', 0,
    (SELECT COUNT(*) FROM (SELECT customer
       FROM customer
      GROUP BY customer
     HAVING COUNT(*) > 1))
  UNION ALL
  SELECT 'rows with no reporting date', 0,
    (SELECT COUNT(*) FROM revenue
      WHERE reporting_date IS NULL)
  UNION ALL
  SELECT 'April, reporting date', 100.00,
    (SELECT SUM(amount) FROM revenue
      WHERE reporting_date >= '2025-04-01'
        AND reporting_date < '2025-05-01')
  UNION ALL
  SELECT 'April, accounting date', -20.00,
    (SELECT SUM(amount) FROM revenue
      WHERE accounting_date >= '2025-04-01'
        AND accounting_date < '2025-05-01')
  UNION ALL
  SELECT 'All of 2025', 400.00,
    (SELECT SUM(amount) FROM revenue
      WHERE reporting_date >= '2025-01-01'
        AND reporting_date < '2026-01-01')
  UNION ALL
  SELECT 'July has no records', 0,
    (SELECT COUNT(*) FROM revenue
      WHERE reporting_date >= '2025-07-01'
        AND reporting_date < '2025-08-01')
) AS tests;

Figure 2 · Test results on the example

Model test results on the example A results card with eight tests on the made-up example, all PASS: join keeps row count, expected 2, actual 2; revenue id is unique, 0 duplicates; customer key is unique, 0; rows with no reporting date, 0; April by reporting date, 100.00; April by accounting date, -20.00; all of 2025, 400.00; July has no records, 0 rows. Model tests TEST EXPECT ACTUAL join keeps row count 2 2 PASS revenue id is unique 0 0 PASS customer key is unique 0 0 PASS rows with no reporting date 0 0 PASS April, reporting date 100.00 100.00 PASS April, accounting date -20.00 -20.00 PASS All of 2025 400.00 400.00 PASS July has no records 0 0 PASS
Eight tests, eight passes. The point of a test is the day it says FAIL. Open full size

To see a failure, point the first test at the guide 1 join to Terms:

-- Point the first test at the guide 1
-- join to Terms
SELECT
  (SELECT COUNT(*) FROM revenue
    WHERE reporting_date >= '2025-04-01'
      AND reporting_date < '2025-05-01')
    AS expected,
  (SELECT COUNT(*) FROM revenue r
     JOIN terms t
       ON t.customer = r.customer
      AND t.product = r.product
    WHERE r.reporting_date >= '2025-04-01'
      AND r.reporting_date <  '2025-05-01')
    AS actual;

It returns 2 expected against 4 actual. That's the fanout, caught by a count, before anyone sees 200.00 on a report. I make a point of seeing every test fail once. A test that can't fail isn't testing anything.

The Power BI version

In Power BI, DAX query view runs the same kind of check against the model itself, through the measures people actually use. One ROW per reference answer, stacked with UNION when you have several:

EVALUATE
VAR AprilReporting =
    CALCULATE (
        [Net Revenue (reporting date)],
        'Date'[Date] >= DATE ( 2025, 4, 1 ),
        'Date'[Date] <  DATE ( 2025, 5, 1 ),
        Customer[Customer] = "C-001",
        Product[Product] = "P-01"
    )
RETURN
    ROW (
        "test", "April, reporting date",
        "expected", 100,
        "actual", AprilReporting,
        "result",
            IF ( AprilReporting = 100,
                "PASS", "FAIL" )
    )

If your models are built in dbt, data tests do the same job at the warehouse layer. Uniqueness and not-null tests cover the first three kinds on the list.

When to run them

  • Every time the model changes: a new column, a new relationship, a renamed measure.
  • After a source change, even when nobody touched the model.
  • Before you publish.
  • Before you point an AI tool at the model, and again after you change what it can see.

Keep the results, not just the pass. Save each run with the date, the model version and the actual values. When a number changes next quarter and someone asks why, the history tells you which change moved it and which test should have caught it.

And start small. Five tests that run every time beat fifty that ran once. Add a test each time you find a new way the model can be wrong. That's how the list grows into something you trust.

Checks on an example prove the example. Your model needs tests written for your model, with your reference answers, agreed by the person who owns each number.

What I check before any tool answers from a model

  1. Are there row-count, key, date, empty-case and reference-answer tests?
  2. Do they run on every model change, not just at launch?
  3. Has each test been seen to fail once, so you know it can?
  4. Does a failure stop the publish?

Test the model. Then hand it over.

Building or checking a Power BI model?

Needs a coding agent on Windows. Not there yet? Start with the free starter.

See the Professional Harness

Related