Data engineering
Margin at the grain of the unit
Built at Virtumed. The method is described here. The public version of the same technique runs on synthetic data.
The problem
The business could not say what it actually made on a product. Margin was being worked out from list price and valuation figures, and neither of those is what anything cost.
Stock bought abroad arrives on a purchase order in a foreign currency, shares the freight, customs and bank charges of the shipment it came in on, and is sold one numbered unit at a time. A list price captures none of that.
What I built
Landed cost rebuilt at the grain of the individual serialised unit. Each unit takes its cost from the supplier price at the exchange rate actually paid on that transaction, plus its share of the freight, customs and bank charges on its shipment.
Every inventory record traces back to the purchase order line that paid for it, so any margin figure can be followed to its source. The logic runs through a mix of Dataverse and dbt models, with tests written to fail the build when a number stops reconciling. A figure that cannot be defended stops there instead of reaching a report.
What changed
Margin by product is measured against real cost rather than list price or valuation figures, and any figure can be followed back to the transactions that produced it.
with fact as ( select count(*) as row_count, count(distinct serial_number) as serial_count from {{ ref('fct_serial_margin') }}) select *from factwhere row_count <> serial_count