P&L Calculation
Exactly how MarginEngine calculates Real Profit — the full formula.
The waterfall
MarginEngine calculates Real Profit using a strict waterfall. Each line subtracts a cost category from revenue:
Gross Revenue (before discounts)
- Discounts
= Net Revenue (after discounts, before refunds)
- Refunds
= Net Revenue (after refunds)
- Product Costs (per unit × quantity)
= Gross Profit
- Ad Spend (Meta, Google, etc.)
- Shipping Costs (net: charged − actual cost)
- Payment Processing Fees (rate × order + fixed per txn)
- Platform Fees (Shopify commission rate)
- Transaction Fees
= Contribution Profit ("Real Profit")
- Fixed Expenses (rent, salaries, software — monthly)
= Operating Profit
Key rules
NULL means unknown
If any cost in the waterfall is unknown, everything below it is also unknown. For example: if Product Costs are missing, Gross Profit is Fog, and so is Real Profit. We never fill in zero for something we don't know.
Money is stored in cents
All money values are stored as whole cents (integers) to avoid floating-point rounding errors. $12.50 is stored as 1250. This is why you'll never see a one-cent rounding discrepancy in MarginEngine.
Refunds reduce revenue, not costs
When a customer gets a refund, the refund amount reduces Net Revenue. It does not reduce Product Costs or other expenses. This matches how refunds actually work financially — you lose the revenue but you already spent the cost.
Snapshots are immutable
Once a day's margin snapshot is calculated, it's never changed retroactively. If new data arrives (late orders, cost corrections), a new snapshot is generated for that day, but historical snapshots remain as they were.
Receipts reference
Every calculated number references "Definition v1" in its receipt. This tells you exactly which formula was used. If we ever change the formula, it would be "Definition v2" and old receipts would still reference v1.