A missing XBRL fact can turn into a clean-looking zero when a data pipeline fills nulls before aggregation. Suppose a company reports operating cash flow of $120 million, but your extraction cannot find capital expenditure. If the missing value becomes zero, a free cash flow calculation returns $120 million. The arithmetic works; the evidence does not support the result.
Missing facts need to survive the trip into the calculation. The following example uses invented values and a small Python standard-library implementation. It also keeps a reported zero distinct from an absent input.
Keep the input state
A missing result has several possible causes. The filer may not have reported the selected concept, the concept may use an extension, or the extraction may have missed the relevant period or unit. A network failure is different again: it says the retrieval failed, not that the filing lacks the fact.
The SEC's EDGAR API documentation describes the company facts endpoint and its aggregation scope. A failed lookup there is a reason to inspect the filing and its taxonomy, not proof that the underlying economic activity was zero.
Represent each input with its state and source. Here, values are already normalized to USD for the same entity and period. A real ingestion layer must establish those conditions before calling the calculation.
from dataclasses import dataclass from decimal import Decimal from typing import Optional @dataclass(frozen=True) class Fact: value: Optional[Decimal] status: str # reported, missing, or retrieval_error accession: Optional[str] @dataclass(frozen=True) class Result: value: Optional[Decimal] status: str sources: tuple[str, ...] def free_cash_flow(operating_cash: Fact, capex: Fact) -> Result: # Capex is a positive cash-outflow magnitude in this convention. facts = (operating_cash, capex) if any(f.status == "retrieval_error" for f in facts): return Result(None, "retrieval_error", ()) if any(f.status != "reported" or f.value is None for f in facts): return Result(None, "missing_input", ()) if capex.value < 0: raise ValueError("capex must be a nonnegative outflow magnitude") sources = tuple(f.accession for f in facts if f.accession is not None) return Result(operating_cash.value - capex.value, "calculated", sources) This is a calculation boundary, not a complete fact selector. It assumes the caller has already verified period, currency, entity scope and the meaning of the selected capex line. The function permits a negative operating cash flow. It rejects a negative capex input because this example uses a positive outflow magnitude. If your source stores outflows as signed negatives, normalize them deliberately before the calculation.
Test zero and absence separately
D = Decimal ocf = Fact(D("120000000"), "reported", "annual-001") zero = Fact(D("0"), "reported", "annual-001") missing = Fact(None, "missing", None) failed = Fact(None, "retrieval_error", None) assert free_cash_flow(ocf, zero).value == D("120000000") assert free_cash_flow(ocf, missing).status == "missing_input" assert free_cash_flow(ocf, missing).value is None assert free_cash_flow(ocf, failed).status == "retrieval_error" assert free_cash_flow(ocf, Fact(D("45000000"), "reported", "annual-001")).value == D("75000000") A reported zero still deserves the same context checks as any other fact. For example, a prior-year zero should not silently satisfy a current-year query. Keep the period and unit in the storage record even though the compact example leaves them outside Fact.
In SQL, SUM skips null values and COALESCE can hide missing inputs if it is applied too early. Count the rows that actually contain a value and compare them with the number of expected inputs before computing the result. You also need a unique row for each required input: a duplicate operating cash flow row cannot stand in for a missing capex row. Keep retrieval errors in a separate status column so that they do not disappear into a generic null count.
Decide what the result means
Free cash flow has more than one definition. This example subtracts a chosen capex outflow from operating cash flow. It does not claim the result matches a company's own non-GAAP measure, and it does not assume one XBRL concept captures every relevant investment outflow. Record the definition with the result and link back to the underlying filings. If a filing has a broader purchase-of-property line or a different classification, review the line before mapping it into this formula.
A useful review queue includes the entity, requested period, concept, failure status and filing accession when available. Resolve a missing concept by inspecting the filing and mapping the appropriate disclosed line, or leave the result unavailable. Preserve the original status and source when a later extraction supplies a value, so a downstream report can explain why its history changed.
For the related period-matching problem, see the quarterly versus year-to-date walkthrough. A valid zero and a missing input need the same period checks, but only one supplies a number for the calculation.

