Level 1: Model the data
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.
On this page
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
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
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
- Are there row-count, key, date, empty-case and reference-answer tests?
- Do they run on every model change, not just at launch?
- Has each test been seen to fail once, so you know it can?
- Does a failure stop the publish?
Test the model. Then hand it over.
Related
- The 4-question checklistHub