A cash-flow pipeline can subtract two perfectly valid year-to-date values and still produce a fictional quarter. Suppose one figure covers January through September and another covers February through June. Subtracting the second from the first gives a number, but that number combines January with July through September.
This is easy to miss when records are joined on a fiscal-year label and a quarter label alone. Keep the actual start and end dates alongside each value. Before deriving a quarter, check that both cumulative periods start on the same day and that the shorter one ends immediately before the target quarter begins.
A small example
Imagine a filer reports operating cash flow of $130 million for January through June and $210 million for January through September. With the same currency, entity and accounting basis, the difference is $80 million for July through September. Those values are illustrative, not figures from a company filing.
Now replace the six-month value with $130 million for February through June. The same subtraction still gives $80 million. It no longer isolates July through September: January is included in the result. A check on the two start dates rejects this pair before the number reaches a chart.
Here's a compact implementation using Python's standard library. The caller supplies the intended quarter start and end; the function checks the dates and basic comparability before subtracting. Amounts are in the same base units, such as USD, rather than a mix of dollars and millions.
from dataclasses import dataclass
from datetime import date, timedelta
from decimal import Decimal
@dataclass(frozen=True)
class Fact:
start: date
end: date
value: Decimal
entity: str
concept: str
unit: str
scope: str
accession: str
def derive_quarter(current, previous, quarter_start, quarter_end):
if (current.entity, current.concept, current.unit, current.scope) != (
previous.entity, previous.concept, previous.unit, previous.scope
):
raise ValueError("facts are not comparable")
if current.start != previous.start:
raise ValueError("cumulative periods have different starts")
if previous.end + timedelta(days=1) != quarter_start:
raise ValueError("previous period does not end at quarter boundary")
if current.end != quarter_end:
raise ValueError("current period does not end at quarter boundary")
if not (current.start <= previous.end < quarter_start <= quarter_end):
raise ValueError("invalid period order")
return {
"start": quarter_start,
"end": quarter_end,
"value": current.value - previous.value,
"method": "current_ytd_minus_previous_ytd",
"source_accessions": [current.accession, previous.accession],
}
six_months = Fact(date(2025, 1, 1), date(2025, 6, 30),
Decimal("130000000"), "example", "operating_cash_flow",
"USD", "consolidated", "six-month-filing")
nine_months = Fact(date(2025, 1, 1), date(2025, 9, 30),
Decimal("210000000"), "example", "operating_cash_flow",
"USD", "consolidated", "nine-month-filing")
result = derive_quarter(nine_months, six_months,
date(2025, 7, 1), date(2025, 9, 30))
assert result["value"] == Decimal("80000000")
This function requires the previous YTD period. First-quarter YTD usually covers the quarter itself, so handle that case separately. Annual minus nine months can derive Q4 using the same date checks, with an additional reconciliation step for revisions.
Dates are necessary, not sufficient
Matching dates do not establish that two facts are economically comparable. An acquisition, a change in presentation, a reclassification or a restatement can affect the subtraction. In particular, an annual filing may revise earlier periods while the original nine-month filing still contains the old figure. The arithmetic assigns any difference to Q4 even if the correction belongs to an earlier quarter.
Review the relevant notes and comparative figures. If an updated nine-month comparative is available on the desired information date, consider deriving a revised Q4 from that value and preserve the original result. Label both versions with their source accessions and availability dates. For point-in-time research, do not use a later comparative before it was published.
The scope field above is a placeholder for normalized entity and dimensional context, not a substitute for parsing the XBRL context. A production selector should distinguish consolidated values from segment or other dimensional facts. It should also use a consistent policy when multiple facts qualify. Never silently select a candidate just because it appears first in a response.
What to keep with the result
Store the target period, the calculation method, both source accessions and the raw values used. Add the units and a status for review when the filing changes the accounting basis. A derived result should remain distinguishable from a directly reported quarter, including in exports and charts.
Tests should cover a mismatched start date, a missing previous period, different units and a quarter boundary that is one day off. Use the actual fiscal calendar rather than assuming calendar quarters: a 52- or 53-week filer can have boundaries that fall on different dates each year. For a missing input, return a missing result or an explicit error, never a fabricated zero.
The SEC's EDGAR API documentation describes the company facts and frames endpoints. Check the filing itself when you need context that the selected API facts do not supply. For the related revision problem, see A Revised Nine-Month Filing Can Change Your Derived Q4 Cash Flow.
Top comments (2)
The part that bit me in a similar pipeline wasn't a wrong value, it was a missing one the check couldn't see.
I replayed my partner's futures fills on public 1-minute bars. The alignment check said 233 of 293 fills matched, and every bar that existed was the right bar. Result: a bracket exit lost $75.54 against her hand exits. On her own bars, all 293 covered, the bracket came out +$444.86. A reader made me rerun only the 70 trades both sources shared, and those were identical. The whole sign flip lived in 18 trades the public feed didn't have (it started on the 3rd of the month and only carried the front contract).
Your start-date check is the version of that I wish I'd had. It refuses the pair instead of quietly computing on whatever is there.
Do you surface the rejected pairs anywhere, or do they just drop out of the series? That count ended up being the number I cared about most.
Keeping the start and end dates next to each value is simple and catches a whole class of bugs. The restatement case worries me more, because the dates match and the number is still wrong. How often did date mismatches show up when you ran the check over real filings?