Sub topic 13
Automating Checks with a Script
This chapter assumes you can read the three financial statements, use spreadsheet cell references, and understand the accounting identity Assets = Liabilities + Equity. It also assumes basic Python knowledge, including lists, dictionaries, loops, and functions.
A spreadsheet check is useful, but a script can test every period consistently. It can identify the first failing period, compare several reconciliations, and run outside the model so that a broken formula does not also break the check.
What a scripted check should test
A useful validation script should test more than whether the balance sheet balances. Common checks include:
- Assets minus liabilities and equity.
- Cash flow statement ending cash minus balance sheet cash.
- Retained earnings against its roll-forward.
- Debt against opening debt, borrowings, and repayments.
- PP&E against opening PP&E, capital expenditure, depreciation, and disposals.
- Working capital balances against their movement schedules.
Each check needs a tolerance. A tolerance is the largest difference accepted as rounding noise. For example, a tolerance of 0.01 accepts differences smaller than one cent.
The script should report the period and check name, not only return True or False.
Finding the first failing period
The first failing period is usually more valuable than the largest error. Later periods may carry the original mistake forward, so starting at the end can hide the cause.
periods = ["2024", "2025", "2026", "2027"]
assets = [500.0, 560.0, 640.0, 735.0]
liab_equity = [500.0, 560.0, 630.0, 725.0]
bs_cash = [80.0, 95.0, 110.0, 125.0]
cf_ending_cash = [80.0, 95.0, 108.0, 123.0]
tolerance = 0.01
def differences(left, right):
return [round(a - b, 2) for a, b in zip(left, right)]
balance_diffs = differences(assets, liab_equity)
cash_diffs = differences(cf_ending_cash, bs_cash)
checks = {
"balance sheet": balance_diffs,
"cash reconciliation": cash_diffs,
}
for check_name, diffs in checks.items():
failures = [
(period, difference)
for period, difference in zip(periods, diffs)
if abs(difference) > tolerance
]
if failures:
period, difference = failures[0]
print(f"{check_name} failed in {period}: {difference:.2f}")
else:
print(f"{check_name}: OK")The output identifies 2026 as the first balance-sheet failure and 2026 as the first cash reconciliation failure. Investigate that period before reviewing later periods.
Reading error signatures
The difference itself provides clues. A sign error often creates a difference equal to twice the affected item. If an inventory movement of 5 is recorded with the wrong sign, the balance can move by 10. An omitted item often creates a difference equal to the item itself.
diff = 10.0
items = {
"Change in inventory": 5.0,
"Capex": 42.0,
"Dividends": 7.0,
}
flipped = [
name for name, value in items.items()
if abs(abs(diff) - 2 * abs(value)) < 0.005
]
omitted = [
name for name, value in items.items()
if abs(abs(diff) - abs(value)) < 0.005
]
print("Possible sign errors:", flipped)
print("Possible omissions:", omitted)This is a shortlist, not proof. Confirm each candidate by tracing the relevant formula and checking whether the item enters the statements with the correct direction.
Common scripting errors
- Using a different period order for two input arrays.
- Comparing rounded values in one statement with unrounded values in another.
- Treating blank cells as zero without checking whether the blank means “not modeled.”
- Checking only the final period.
- Hardcoding expected results in the validation script.
- Allowing a large tolerance that hides a real imbalance.
- Exporting stale spreadsheet values instead of recalculating the model first.
Keep the script independent from the formulas it tests. It should read model outputs and recompute relationships, rather than copying the model's formulas into another format.
> callout note > A passing balance check does not prove that the model is correct. Two errors can offset each other. Combine balance checks with cash reconciliations and line-by-line roll-forward checks.
Exercise
Exercise
A model has a balance difference of 14, and the change in receivables is 7. What possible error signature should you investigate first?
Show answer
Investigate a sign reversal in the receivables movement, because reversing an item of 7 can create a difference of 14. Confirm the result by tracing the formulas.