Skip to content
Start free

Trace a report total back to its rows

Run the report for September, then follow the North total back to the exact orders behind it.

2 parts · 11 and 2 min · part 1 on a computer

Free course. Your Maestro, an AI, .

In the September file, you saw that every row stands for one order. When you run the monthly , those individual orders disappear behind regional totals in reports/history.csv and a single monthly summary in reports/latest.json. When someone asks why a region's total looks the way it does, you need to trace that figure back through the code to the exact rows of the file that produced it.

Tracing a number's lineage means following each transformation between the raw input and the finished output. Rerunning the script only proves that the code still gives the same answer today; it does not show which rows went into the calculation. Accepting a total because it looks reasonable carries its own risk, since missing orders or a wrong filter can still land on a believable sum.

Open report.py in the workspace below and read run(month). Every figure that reaches reports/history.csv passes through four steps in order:

  1. csv.DictReader opens data/sales_{month}.csv and reads every line into rows as a dictionary.
  2. check(rows, columns, month) from contract.py inspects the column list and every row's order_id, order_date, region, and amount. If it finds any problems, run records a rejected status in reports/runs.csv and stops before changing the report files.
  3. A loop steps through rows, converts r["amount"] with float(), and adds it to totals[r["region"]]. Here the shifts from one row per order to one sum per region.
  4. upsert formats each regional sum to two decimal places, replaces any existing rows for that month in reports/history.csv, and writes the file through write_atomic.

The diagram below maps those four steps from the input file to the saved history table.

  1. File rows in data/sales_2026-09.csv

    One row per order

  2. Contract check in contract.py

    check(rows, columns, month)

  3. Totals by region in report.py

    Sum float(r["amount"]) per region

  4. Regional rows in reports/history.csv

    One row per month and region

Read from top to bottom to see how report.py builds the file; read from bottom to top to trace a regional total back to its orders.

Following the arrows forward shows how report.py builds the output; walking them backward gives you a checklist for tracing any number on the page. Start at the line in reports/history.csv, check how upsert and the totals loop grouped the amounts by region, and pull the matching orders from data/sales_2026-09.csv. If any line in report.py is hard to follow, ask your Maestro to walk through it with you.

Try it

In the workbench below, report.py and contract.py are read-only so you can inspect the exact code that runs. Run trace.py first to execute report.run("2026-09") and print reports/history.csv. Then edit trace.py to read data/sales_2026-09.csv, keep the rows where r["region"] == "North", print each order's order_id and amount, and print their sum. At the bottom of trace.py, write a comment naming the steps in report.py between those source rows and the history line.

Your turn

Run trace.py. Then add code that lists the September rows behind the North total and their sum. In a comment, name the steps in report.py between those rows and the history line. You're done when your sum matches the report.

A good place to stop. Up next: part 2 of 2 · 2 min

What your trace should show

When trace.py runs report.run("2026-09"), it prints a report total of 3000.0 and writes four regional lines for 2026-09 into reports/history.csv: East with 650.00, North with 1200.00, South with 800.00, and West with 350.00.

Filtering data/sales_2026-09.csv for r["region"] == "North" returns three orders: S-0901 with amount 500, S-0904 with amount 400, and S-0908 with amount 300. Summing those three amounts with float(r["amount"]) gives 1200.0, which report.py formats to two decimal places as 1200.00.

Your comment at the bottom of trace.py should name all four steps in order: csv.DictReader reads the rows from data/sales_2026-09.csv, contract.check checks and accepts them, the totals loop groups by region and sums float(r["amount"]), and upsert writes the month's regional rows into reports/history.csv. If your sum does not match 1200.0 or a step in the chain is unclear, share your output with your Maestro.

Check yourself: A colleague sees 2026-09,North,1200.00 in reports/history.csv and asks you to verify where 1200.00 came from. What traces that figure back to its source?