Skip to content

14. Test the Grain of a Daily Fact Table

Difficulty: Medium · Topic: Data tests

fct_store_sales (store_id, sale_date, orders, revenue) should have one row per store per day. Write models/schema.yml tests for its rules:

Clean datasets must pass every test; broken ones must make at least one test fail. dbt_utils is installed (unique_combination_of_columns, expression_is_true, accepted_range).

Your schema.yml tests run on 7 datasets, including hidden ones: clean datasets must pass every test, broken ones must make a test fail.

Clean data (clean)

{{ ref('fct_store_sales') }}

store_idsale_dateordersrevenue
12024-03-0112340
12024-03-029210.5
22024-03-0100
22024-03-02480

The same store-day twice (broken)

A rerun appended store 1's day again.

{{ ref('fct_store_sales') }}

store_idsale_dateordersrevenue
12024-03-0112340
12024-03-0112340
22024-03-01360

Hints

Hint 1The grain is two columns together: a store has many days and a day has many stores, so unique on either column fails on clean data.
Hint 2Tests about several columns go under the model itself (data_tests: next to columns:): dbt_utils.unique_combination_of_columns with combination_of_columns: [store_id, sale_date].
Hint 3"No orders means no revenue" is a condition on two columns: dbt_utils.expression_is_true at model level with expression: "orders > 0 or revenue = 0".

Learn the concepts

More dbt problems

All dbt problems