Three closes ago, our automated monthly revenue variance script failed finance sign-off for the third month in a row. I had written it in Python to replace a brittle spreadsheet before board review. Whenever the catalog stayed static, the bridge tied out. The moment a product launched or retired, our waterfall spit out an unexplained gap that someone had to hide inside an "Other" bar.
I assumed we had a duplicate invoice row in our SQL extract. The real bug was the textbook price-volume formula I had ported straight into pandas.
To isolate what was happening, I built a four-SKU test fixture mirroring a real close: two continuing products (Core-A and Premium-B), one retired SKU (Legacy-C), and one new launch (Launch-D). Period 0 revenue is $340,000.00 and Period 1 revenue is $430,400.00, so the true revenue variance is +$90,400.00.
Here is what our first version of the script ran:
import pandas as pd
df = pd.DataFrame([
{"sku": "Core-A", "p0": 50.0, "q0": 4000, "p1": 52.0, "q1": 4200},
{"sku": "Premium-B", "p0": 120.0, "q0": 1000, "p1": 115.0, "q1": 1600},
{"sku": "Legacy-C", "p0": 40.0, "q0": 500, "p1": 40.0, "q1": 0},
{"sku": "Launch-D", "p0": 0.0, "q0": 0, "p1": 80.0, "q1": 350},
])
df = df.assign(
r0=lambda d: d["p0"] * d["q0"],
r1=lambda d: d["p1"] * d["q1"],
)
delta_total = df["r1"].sum() - df["r0"].sum() # 430400.0 - 340000.0 = 90400.0
naive_price = ((df["p1"] - df["p0"]) * df["q0"]).sum()
naive_vol = ((df["q1"] - df["q0"]) * df["p0"]).sum()
naive_sum = naive_price + naive_vol # 65000.0
plug = delta_total - naive_sum # 25400.0
print(f"True delta: ${delta_total:,.2f}")
print(f"Naive sum: ${naive_sum:,.2f}")
print(f"Plug: ${plug:,.2f}")
That script explained +$65,000.00 of a +$90,400.00 move and dropped +$25,400.00 on the floor. Over 28% of total growth vanished into thin air.
Two mechanical mistakes caused the entire plug. First, evaluating both price variance and volume variance at base weights (q0 and p0) silently discards the discrete cross-term (p1 - p0) × (q1 - q0). On Core-A, that dropped +$400.00. On Premium-B, where a $5.00 discount drove 600 extra units, it dropped -$3,000.00. Second, passing Launch-D through the same formula with p0 = 0.0 and q0 = 0 set both (p1 - p0) × q0 and (q1 - q0) × p0 to zero, pushing its entire +$28,000.00 launch revenue into the missing cross-term. Add +$400.00 - $3,000.00 + $28,000.00 and you get the exact +$25,400.00 plug.
In production, realized net price comes from dividing net invoiced revenue by units (r / q), never from a list price. A new SKU has q0 = 0, yielding NaN in pandas; filling NaN with 0.0 either drops the launch into a plug or fakes a 100% price hike. Pull credit memos and return-only rows (q <= 0 or r < 0) into a separate Returns & Allowances bucket before dividing anything.
The 5-Bucket Identity That Ties to Zero
The fix was to partition the table before calculating a single average price: continuing (q0 > 0 and q1 > 0), discontinued (q0 > 0 and q1 == 0), and new (q0 == 0 and q1 > 0). Then, for continuing SKUs, value Volume and Mix at base continuing prices (p0) and value Net Price at current quantity (q1). That folds the Δp × Δq interaction cleanly into Price with zero residual.
cont = df[(df["q0"] > 0) & (df["q1"] > 0)].copy()
disc_rows = df[(df["q0"] > 0) & (df["q1"] == 0)]
new_rows = df[(df["q0"] == 0) & (df["q1"] > 0)]
q0_c = cont["q0"].sum() # 5000
q1_c = cont["q1"].sum() # 5800
pbar0_c = cont["r0"].sum() / q0_c # 64.00
s0 = cont["q0"] / q0_c
s1 = cont["q1"] / q1_c
vol = (q1_c - q0_c) * pbar0_c # +51200.00
mix = q1_c * ((s1 - s0) * (cont["p0"] - pbar0_c)).sum() # +30800.00
price = ((cont["p1"] - cont["p0"]) * cont["q1"]).sum() # +400.00
disc = -disc_rows["r0"].sum() # -20000.00
new = new_rows["r1"].sum() # +28000.00
assert abs(vol + mix + price + disc + new - delta_total) < 1e-9
Every bar now maps to something a controller can audit in ten seconds:
- Continuing Volume Effect:
+$51,200.00(800 more continuing units valued at the base continuing average pricePbar0_c = $64.00). - Continuing Mix Effect:
+$30,800.00(Premium-Bclimbed from20.0%to27.59%of continuing unit volume whileCore-Ashifted down). - Continuing Price Effect at
q1:+$400.00(+$8,400.00from the+$2.00hike onCore-Aminus$8,000.00from the-$5.00discount onPremium-B). - Discontinued SKU Effect (
Legacy-C):-$20,000.00. - New SKU Effect (
Launch-D):+$28,000.00.
Sum all five bars and you land on +$90,400.00 exact, with the 1e-9 assertion guarding the pipeline before any chart or deck is built. One pandas note: with Copy-on-Write active in newer versions, avoid chained assignments like df[mask]["bucket"] = "new" and use .assign() or .loc[].
Two Edge Cases That Still Broke Our Close Script
Once the revenue bridge passed reconciliation, two real-world quirks surfaced in the next quarter.
First, unit-based PVM collapses when a division sells incommensurate physical units, such as $12,000 hardware appliances alongside $4 replacement cables. Adding those unit counts distorts Pbar0_c and turns routine cable orders into wild mix swings. For mixed-unit portfolios, we drop unit counts and run a revenue-weighted Gross Margin % bridge in basis points (1% = 100 bps), centering every mix term around base portfolio margin gm0 so the weights cancel cleanly:
def gm_bps_bridge(df_gm: pd.DataFrame) -> dict[str, float]:
r0, r1 = df_gm["r0"].sum(), df_gm["r1"].sum()
gm0, gm1 = df_gm["gp0"].sum() / r0, df_gm["gp1"].sum() / r1
w0, w1 = df_gm["r0"] / r0, df_gm["r1"] / r1
m0 = df_gm["gp0"].div(df_gm["r0"]).fillna(0.0)
m1 = df_gm["gp1"].div(df_gm["r1"]).fillna(0.0)
is_c = (df_gm["r0"] > 0) & (df_gm["r1"] > 0)
is_n = (df_gm["r0"] == 0) & (df_gm["r1"] > 0)
is_d = (df_gm["r0"] > 0) & (df_gm["r1"] == 0)
rate_bps = (w1[is_c] * (m1[is_c] - m0[is_c])).sum() * 10_000
mix_c_bps = ((w1[is_c] - w0[is_c]) * (m0[is_c] - gm0)).sum() * 10_000
mix_n_bps = (w1[is_n] * (m1[is_n] - gm0)).sum() * 10_000
mix_d_bps = -(w0[is_d] * (m0[is_d] - gm0)).sum() * 10_000
delta_bps = (gm1 - gm0) * 10_000
assert abs((rate_bps + mix_c_bps + mix_n_bps + mix_d_bps) - delta_bps) < 1e-9
return {
"rate_bps": rate_bps,
"mix_c_bps": mix_c_bps,
"mix_n_bps": mix_n_bps,
"mix_d_bps": mix_d_bps,
}
Second, multi-currency entities will leak exchange-rate noise straight into your Price bar unless you separate FX before running PVM. We revalue Period 1 local-currency revenue at Period 0 exchange rates (r1_cc), feed the constant-currency delta into the 5-bucket PVM function, and isolate translation in its own bar:
r0_rep = df_fx["r0_loc"] * df_fx["fx0"]
r1_cc = df_fx["r1_loc"] * df_fx["fx0"]
r1_rep = df_fx["r1_loc"] * df_fx["fx1"]
ops_delta = (r1_cc - r0_rep).sum() # Run the 5-bucket PVM bridge on this
fx_effect = (r1_rep - r1_cc).sum() # Separate FX translation bar
assert abs((ops_delta + fx_effect) - (r1_rep.sum() - r0_rep.sum())) < 1e-9
Putting hard 1e-9 assertions right next to the algebra changed how close week feels. When a late credit memo lands an hour before the review, we re-run one script and know in two seconds whether every bar ties to the cent. If you build variance pipelines for finance, does your team fold the cross-term into Price at q1, or do they ask for symmetric midpoint weights instead?
Top comments (0)