Build with Daana
Chapter 8: A wrong number
In chapter 7, you applied three reporting documents and queried the amounts on lines received each day. This is the first chapter that goes further after the tutorial.
Chapter 7 explains how its daily output dates lines. Here, the task is to prove which rows a second daily revenue report moves to another day, by comparing the report with the source at its grain. You will compare the report with the source, test possible causes, find the declaration responsible, and verify a fix. Afterward, you will be able to investigate a report at its grain.
Start state: The main project bookstore has completed daana execute. The terminal is in bookstore, and DuckDB is closed. To start at this chapter, follow Start at any chapter.
Project setup
Choose a route to create a side project with the order lines and the website's daily revenue declaration. The website is daana.dev, as the first line of dar/order_line.yaml says: "Daily revenue on daana.dev's bookstore example".
The free route
This route uses the main project's model and needs no license.
Create and enter bookstore-late at checkpoint ch5:
Run in terminal
daana init ../bookstore-late --example bookstore --checkpoint ch5
cd ../bookstore-late
Load the data file ch8-late-order-lines before the first daana execute:
Run in terminal
daana init . --example bookstore --load-data ch8-late-order-lines
Expected output
Loaded data/ch8-late-order-lines.sql into bookstore.duckdb
This data file can be loaded into a project only once: Daana refuses a second load with an is already loaded error and leaves the order lines unchanged.
The data file is now in bookstore.duckdb, so the first daana execute reads the order lines as the data file left them. Build the data warehouse with the four commands:
Run in terminal
daana check && daana install && daana deploy && daana execute
Expected output, summary line of daana execute
Pipelines: 50 ran
Daana ran 50 pipelines. This project's dar/ holds only a README.md, so copy the website's declaration from a ch8 checkpoint. Leave the printed "Next steps" for ch8-files unrun. Create ch8-files and copy its dar/order_line.yaml into this project's dar/:
Run in terminal
daana init ../ch8-files --example bookstore --checkpoint ch8
cp ../ch8-files/dar/order_line.yaml dar/
ch8-files is only created, never deployed, so it needs no license.
The licensed route
This route needs a license because it uses the full model.
Create bookstore-ch8 at checkpoint ch8, with the full model and the website's declaration, then load the data file and build it:
Run in terminal
daana init ../bookstore-ch8 --example bookstore --checkpoint ch8
cd ../bookstore-ch8
daana init . --example bookstore --load-data ch8-late-order-lines
daana check && daana install && daana deploy && daana execute
Continue in the side project created by the chosen route. Both routes produce the same numbers in the report, the parity query, and the comparisons of this chapter. The query on f_order_line near the end was recorded on the free route only.
The report's daily revenue
The website's 49-line dar/order_line.yaml has a dimension, a line fact, and daily_revenue, without chapter 7's relationships, keys, wide view, or output check. Chapter 7 explains the declaration keys. Read what daily_revenue promises to sum:
daily_revenue: # compiles to fct__order_line_daily_revenue
kind: fact.consolidated
description: >
Daily revenue: order lines' amounts summed by the day the order was
placed. One row per day and currency; every line is USD in this data.
grain: [order_date, order_line_amount_currency]
This description promises revenue by the day the order was placed. Chapter 7's daily output counts each line on the day it was received instead. This report's grain names its day order_date, while chapter 7's names it received_date. Which of the two days this report follows is the question of this chapter. Check and apply the document:
Run in terminal
daana dar check dar/ && daana dar apply dar/
Expected output of daana dar check
Checking dar check: dar/
Spec: dar/v1.0
Anchor: entity:order_line
Documents: 1
Outputs: 3
✓ Validation passed
The check validates the form of one document with three outputs.
Expected output of daana dar apply
Applied 4 objects (4 views) after 0 checks to daana_dar
Daana created four views from three outputs because the dimension also has a view of its latest versions. The file declares no output check.
The build's success does not prove that the daily revenue agrees with the source data. Open DuckDB:
Run in terminal
duckdb -readonly bookstore.duckdb
List the report's daily revenue:
Run in DuckDB
SELECT order_date, round(CAST(daily_revenue_amount AS DOUBLE), 2) AS daily_revenue
FROM daana_dar.fct__order_line_daily_revenue
ORDER BY order_date;
Expected output
| order_date | daily_revenue |
|---|---|
| 2024-11-01 | 60.87 |
| 2024-11-02 | 24.49 |
| 2024-11-03 | 71.39 |
| 2024-11-04 | 53.27 |
| 2024-11-05 | 19.52 |
| 2024-11-06 | 56.5 |
| 2024-11-07 | 44.57 |
| 2024-11-08 | 74.7 |
| 2024-11-09 | 58.7 |
| 2024-11-10 | 42.39 |
| 2024-11-12 | 19.52 |
| 2024-11-15 | 42.39 |
| 2024-11-16 | 19.56 |
| 2024-11-20 | 35.88 |
| 2024-11-21 | 43.47 |
| 2024-11-23 | 39.13 |
| 2024-11-25 | 38.05 |
| 2024-11-28 | 18.48 |
The report has 18 days, although orders were placed on 16 days. It shows revenue on 16 November and 28 November, when no orders were placed, yet its total is still 762.88. That matches the source total of chapter 2.
A daily report also answers questions such as revenue up to the end of 15 November. Compare the report's answer with the source's revenue by order date for the same days:
Run in DuckDB
SELECT 'report' AS layer, round(CAST(sum(daily_revenue_amount) AS DOUBLE), 2) AS revenue_to_2024_11_15
FROM daana_dar.fct__order_line_daily_revenue
WHERE order_date <= DATE '2024-11-15'
UNION ALL
SELECT 'source by order date', round(CAST(sum(l.line_total) AS DOUBLE), 2)
FROM stage.sales_order_lines l
JOIN stage.sales_orders o USING (order_id)
WHERE CAST(timezone('UTC', o.order_date) AS DATE) <= DATE '2024-11-15'
ORDER BY layer;
Expected output
| layer | revenue_to_2024_11_15 |
|---|---|
| report | 568.31 |
| source by order date | 587.87 |
The report returns 568.31 against the source's 587.87, so it disagrees with its promised order dates. Next, compare counts and totals across the layers to check where they disagree.
The parity query
A parity query is one read-only query that compares line counts and revenue totals across the layers. A difference can help locate rows lost or duplicated between layers.
Here, stage holds the source tables, DAS (Data according to System). The views in daana_dw are DAB (Data according to Business), and the outputs in daana_dar are DAR (Data according to Requirements).
Before you continue
The report has 18 days, although orders were placed on 16. Its 762.88 matches chapter 2's total for 31 lines. Predict whether the DAB also holds 31 lines and whether the parity query will report a fault. Write down your answer and the reason for it. Then compare the line counts and revenue totals of the three layers:
Run in DuckDB
SELECT (SELECT count(*) FROM stage.sales_order_lines) AS stage_lines,
(SELECT count(*) FROM daana_dw.view_order_line) AS dab_lines,
(SELECT round(CAST(sum(line_total) AS DOUBLE), 2) FROM stage.sales_order_lines) AS stage_revenue,
(SELECT round(CAST(sum(order_line_amount_amount) AS DOUBLE), 2) FROM daana_dw.view_order_line) AS dab_revenue,
(SELECT round(CAST(sum(daily_revenue_amount) AS DOUBLE), 2) FROM daana_dar.fct__order_line_daily_revenue) AS dar_revenue;
Expected output
| stage_lines | dab_lines | stage_revenue | dab_revenue | dar_revenue |
|---|---|---|---|---|
| 31 | 31 | 762.88 | 762.88 | 762.88 |
The source and DAB each hold 31 lines, and all three revenue totals equal 762.88. Equal counts and totals show no net difference in lines or revenue, so the parity query reports no fault. The query cannot show whether a lost line and a duplicated line cancel out. Equal totals do not show on which day each amount is reported, so the next section compares the amounts per day.
Two possible causes
Exercise
Chapter 7 showed one cause that moves an amount to another day: a report that dates lines by their load time. Write down a second possible cause of the incorrect daily amounts and one query that would tell the two causes apart. Predict the result for each cause before opening a correct answer.
Hint: Compare the report with the source table at the grain of the report, a day. Join the source order lines to their order headers to get the order date.
Show a correct answer
A correct answer
The totals agree, and the report has amounts on two days without orders. This suggests that some amounts are reported on days other than their order dates. A time zone conversion would move an amount to an adjacent day. A report that dates lines by another timestamp than the order date could move an amount by several days. Comparing the report with the source at its grain, a day, distinguishes these causes.
This query compares the report with the source per day, where each cause predicts a different pattern of differences. Run it at the next step, after writing down the prediction there:
WITH stage_days AS (
SELECT CAST(timezone('UTC', o.order_date) AS DATE) AS order_date, round(CAST(sum(l.line_total) AS DOUBLE), 2) AS stage_revenue
FROM stage.sales_order_lines l JOIN stage.sales_orders o USING (order_id)
GROUP BY ALL
), dar_days AS (
SELECT order_date, round(CAST(sum(daily_revenue_amount) AS DOUBLE), 2) AS dar_revenue
FROM daana_dar.fct__order_line_daily_revenue
GROUP BY ALL
)
SELECT order_date, stage_revenue, dar_revenue
FROM stage_days FULL JOIN dar_days USING (order_date)
WHERE stage_revenue IS DISTINCT FROM dar_revenue
ORDER BY order_date;
Before you continue
The report has two dates with no orders. Can dates that have orders also have incorrect amounts in the report? Write down your answer and the reason for it. Then compare daily totals, keeping dates found on only one side:
Run in DuckDB
WITH stage_days AS (
SELECT CAST(timezone('UTC', o.order_date) AS DATE) AS order_date, round(CAST(sum(l.line_total) AS DOUBLE), 2) AS stage_revenue
FROM stage.sales_order_lines l JOIN stage.sales_orders o USING (order_id)
GROUP BY ALL
), dar_days AS (
SELECT order_date, round(CAST(sum(daily_revenue_amount) AS DOUBLE), 2) AS dar_revenue
FROM daana_dar.fct__order_line_daily_revenue
GROUP BY ALL
)
SELECT order_date, stage_revenue, dar_revenue
FROM stage_days FULL JOIN dar_days USING (order_date)
WHERE stage_revenue IS DISTINCT FROM dar_revenue
ORDER BY order_date;
Expected output
| order_date | stage_revenue | dar_revenue |
|---|---|---|
| 2024-11-06 | 82.59 | 56.5 |
| 2024-11-09 | 32.61 | 58.7 |
| 2024-11-10 | 61.95 | 42.39 |
| 2024-11-16 | NULL | 19.56 |
| 2024-11-25 | 56.53 | 38.05 |
| 2024-11-28 | NULL | 18.48 |
Yes, four of the six days that the comparison returns have orders. stage_revenue groups source line totals by the order headers' dates after converting their timestamps to UTC. NULL means the source has no row for that day. The overall totals match despite these daily differences.
Each amount on a day without orders came from a day with orders, which then shows less. Looking only at the two days without orders misses those lower amounts. The transfer between 6 and 9 November leaves the report's number of days unchanged, because both days have orders.
The amounts moved by three and six days, which rules out a time zone conversion. For example, the 26.09 missing on 6 November matches the extra 26.09 on 9 November.
With the overall total reconciled and the daily differences located, trace the declaration that chooses the day. Then fix it and repeat the comparison.
Find the declaration
The daily differences point to the timestamp used by the report. The project files show which expression gives it that value. Find the line fact's axis in dar/order_line.yaml:
axis:
expr: eff_tmstp # the mapping sets it to the order's order_date
description: When the order the line is on was placed (event time, not load time)
The axis reads eff_tmstp, the effective timestamp, as in chapter 7. Its comment and description call that the order's time. Compare them with the dating in dab/mappings/order-line-mapping.yaml:
# FULL: the source overwrites lines in place (fulfillment_status changes,
# no updated_at), so each run is a snapshot compared against the previous one.
# A FULL source is dated by the time its rows landed: a status change lands
# with a later ingested_at and becomes a new version of the line.
ingestion_strategy: FULL
entity_effective_timestamp_expression: ingested_at
The mapping sets eff_tmstp to ingested_at, the line's load time. So the comment in the axis block is wrong: this report counts each line on the day it was received, as chapter 7's daily output does. The descriptions of the axis and of daily_revenue promise the day the order was placed. The attribute order_line_purchase_ts holds the order date from the order header.
Exercise
Write a query that lists the lines reported on another day than their order, with both days and each amount. The amounts must explain every difference in the daily comparison. Try writing the query before opening a correct answer.
Hint: The order headers are in stage.sales_orders. Compare the line's ingested_at with the header's order_date, converting both to UTC before taking their dates.
Show a correct answer
A correct answer
List the lines whose load date differs from their order date, using UTC for both:
Run in DuckDB
SELECT l.order_id, l.order_line_id,
CAST(timezone('UTC', o.order_date) AS DATE) AS order_day,
CAST(timezone('UTC', l.ingested_at) AS DATE) AS ingested_day, l.line_total
FROM stage.sales_order_lines l
JOIN stage.sales_orders o USING (order_id)
WHERE CAST(timezone('UTC', l.ingested_at) AS DATE) <> CAST(timezone('UTC', o.order_date) AS DATE)
ORDER BY l.order_id, l.order_line_id;
Expected output
| order_id | order_line_id | order_day | ingested_day | line_total |
|---|---|---|---|---|
| 6 | 3 | 2024-11-06 | 2024-11-09 | 26.09 |
| 10 | 3 | 2024-11-10 | 2024-11-16 | 19.56 |
| 13 | 2 | 2024-11-25 | 2024-11-28 | 18.48 |
The columns order_day and ingested_day show when each order was placed and when its line arrived. The amounts moved are 26.09 from 6 to 9 November, 19.56 from 10 to 16 November, and 18.48 from 25 to 28 November. These explain all six differences while leaving every total unchanged.
The data file data/ch8-late-order-lines.sql changed only ingested_at for these three lines. The other 28 lines arrived 20 minutes after their order, on the same day.
Optional agent request
For this investigation, you could give your agent the project directory, the path of the DuckDB CLI, and this request:
The bookstore's daily revenue report, `daana_dar.fct__order_line_daily_revenue` in `bookstore.duckdb`, has a row for 2024-11-16 and one for 2024-11-28, days on which no order was placed. The total still matches the source. Find out why.
Rules:
- You may read every file in this project: `daana.yaml`, `dab/` (model, mappings, workflow), `dar/` (reporting declarations), `data/` (the source data scripts).
- You may query `bookstore.duckdb`, read-only only: `duckdb -readonly bookstore.duckdb -c "<sql>"`. The source tables are in schema `stage`, the business model's views in `daana_dw`, the reporting outputs in `daana_dar`.
- You may run `daana check` (offline validation). Do not run anything that writes: no `daana deploy`, `execute`, `install` or `dar apply`, no file edits.
Report:
1. The cause, citing the file and line that produce it.
2. The evidence: each SQL query you ran that supports the cause, with its output.
3. The change that fixes it, and the commands that would rebuild the affected data afterwards (do not run them).
Keep the report under 400 words. Then, after the report, list every command and file read you made, in order, one line each, exactly as you ran them (this trace will be published).
Compare the agent's cause with the axis and the mapping above, and its evidence with the daily comparison and your query from the exercise. Compare a fix that it proposes with the one in "Fix it", and test the fix with the queries of "Verify the daily result". The queries and the fix in this chapter work without an agent or plugin.
In a recorded run on 15 November 2026, an agent with the Daana plugin v0.1.1 found that the line fact's axis dates each line by its load time. It proposed order_line_purchase_ts as the axis, as in "Fix it", and simulated that change with a read-only query. It did not apply the changed declaration.
Fix it
The daily report needs the order's event time to group each amount on the day of the order. In dar/order_line.yaml, change the axis expression to order_line_purchase_ts and fix its comment:
axis:
expr: order_line_purchase_ts # the order's order_date; eff_tmstp is when the line landed
description: When the order the line is on was placed (event time, not load time)
Keep the mapping unchanged: this FULL source uses load time so that a later fulfillment status becomes a new version.
The order date stays the same when a status changes. Using it in the mapping would store a later status as a correction at the same effective timestamp. A query at an instant before the status change would then return the new status. See Dating a FULL source for the reason to keep load time in the mapping.
Quit DuckDB, then check and apply the declaration:
Run in terminal
daana dar check dar/ && daana dar apply dar/
Expected output of daana dar check
Checking dar check: dar/
Spec: dar/v1.0
Anchor: entity:order_line
Documents: 1
Outputs: 3
✓ Validation passed
Expected output of daana dar apply
Applied 4 objects (4 views) after 0 checks to daana_dar
Both outputs are the same as before the fix: the changed declaration passes validation, and Daana applies its four views again. The DAB needs no deploy, execution, or full refresh.
Unlike this chapter's declaration, chapter 7's line fact has keys and wide. With the order's time on its axis, each line's link to its order starts after fact_ts, at the line's load time. Both relationships are changes: never, so DAR still reads every line's order key and customer key from the latest links. The wide view then joins the dim__customer version valid at the order's time.
Every line of the main project arrived 20 minutes after its order, on the same day. The daily output and the revenue by tier of chapter 7 are therefore the same with either axis. In the main project, leave the axis of chapter 7's line fact unchanged: its daily output counts the lines received each day, as chapter 7 says.
Verify the daily result
The new axis must restore both the set of days and each day's amount. Reopen DuckDB, then count the report's days and check its total:
Run in DuckDB
SELECT count(*) AS n_days, round(CAST(sum(daily_revenue_amount) AS DOUBLE), 2) AS total
FROM daana_dar.fct__order_line_daily_revenue;
The table records the number of days in the report at each stage of the chapter. The query above returns one row, which must match the table's last row:
| Step | n_days | total |
|---|---|---|
| Faulty state (after project setup) | 18 | 762.88 |
| Fixed state (after "Fix it") | 16 | 762.88 |
The report returns to 16 days and keeps the total of 762.88. This count does not prove that each day's amount is correct, so repeat the daily comparison using UTC for the source order dates:
Run in DuckDB
WITH stage_days AS (
SELECT CAST(timezone('UTC', o.order_date) AS DATE) AS order_date, round(CAST(sum(l.line_total) AS DOUBLE), 2) AS stage_revenue
FROM stage.sales_order_lines l JOIN stage.sales_orders o USING (order_id)
GROUP BY ALL
), dar_days AS (
SELECT order_date, round(CAST(sum(daily_revenue_amount) AS DOUBLE), 2) AS dar_revenue
FROM daana_dar.fct__order_line_daily_revenue
GROUP BY ALL
)
SELECT order_date, stage_revenue, dar_revenue
FROM stage_days FULL JOIN dar_days USING (order_date)
WHERE stage_revenue IS DISTINCT FROM dar_revenue
ORDER BY order_date;
Expected output
| order_date | stage_revenue | dar_revenue |
|---|
The query returns no differences: the report agrees with the source on every day.
What Daana did
- Daana replaced the reporting views from the revised declaration; the count changed from 18 days to 16.
- Daana grouped revenue using the revised axis; the empty comparison shows agreement with the source order dates.
Repeat the comparison of revenue through 15 November:
Run in DuckDB
SELECT 'report' AS layer, round(CAST(sum(daily_revenue_amount) AS DOUBLE), 2) AS revenue_to_2024_11_15
FROM daana_dar.fct__order_line_daily_revenue
WHERE order_date <= DATE '2024-11-15'
UNION ALL
SELECT 'source by order date', round(CAST(sum(l.line_total) AS DOUBLE), 2)
FROM stage.sales_order_lines l
JOIN stage.sales_orders o USING (order_id)
WHERE CAST(timezone('UTC', o.order_date) AS DATE) <= DATE '2024-11-15'
ORDER BY layer;
Expected output
| layer | revenue_to_2024_11_15 |
|---|---|
| report | 587.87 |
| source by order date | 587.87 |
Both rows now show 587.87, because the 19.56 that the report counted on 16 November now counts on 10 November, the day of its order. Read the same cutoff in the DAB: count and sum the lines that f_order_line returns at the end of 15 November:
Run in DuckDB
SELECT count(*) AS order_lines, round(CAST(sum(order_line_amount_amount) AS DOUBLE), 2) AS revenue
FROM daana_dw.f_order_line(TIMESTAMP '2024-11-15 23:59:59');
Expected output
| order_lines | revenue |
|---|---|
| 24 | 568.31 |
The function returns 24 lines and 568.31, the figure that the report gave before the fix, and the fix does not change it. f_order_line selects lines by effective timestamp, here the load time. The line with the 19.56 arrived on 16 November, so it is not effective at this cutoff.
The 568.31 is not a fault, because the DAB answers another question: which lines were effective at that instant. This report promises revenue by the day the order was placed.
daana check and daana dar check validate the form of a declaration. They cannot tell whether eff_tmstp is the right timestamp for this report. The daily comparison gives that evidence for this dataset. Quit DuckDB.
What you can do now
You can compare a report with its source per day, test hypotheses, trace its timestamp, and prove a fix. Daana rebuilt the reporting views and regrouped the amounts from your revised declaration.
Late rows changed daily revenue without changing its total. A matching total does not prove that each day is right.
The parity query still compares totals only, so the comparison per day is a query that you must keep and run yourself. Distinguish event time from load time when you choose a report's timestamp. Return to bookstore:
Run in terminal
cd ../bookstore
Chapter 9: One customer, two systems adds an external delivery of reader profiles that identifies customers by email. Alternatively, Chapter 10: Going to production copies the main project into a side project. It deploys to a second target in the same DuckDB file and generates a Dataform preview of a reporting declaration.
The terminal is in bookstore, and DuckDB is closed.