Invoice lines
One posted sales invoice line
228,265Sales.InvoiceLinesFeeds net sales, gross profit and product margin.
A reporting analyst prepares the wholesaler’s May sales, margin and stock report from invoice lines, supplier orders and warehouse movements, with each figure checked before finance, sales and warehouse managers use it.
A wholesale month end
A sales manager and finance partner can receive plausible but different margin totals when invoice lines, supplier orders and stock movements use different joins, dates or product references. The reporting analyst must settle those definitions before the May report is used.
This project builds one SQL reporting foundation from those records. It gives the analyst a clear choice: publish the checked May 2016 report, or hold any failing record with its original key and reason for investigation.
Primary retained evidence
This view is generated from the retained DuckDB outputs after the clean build. It is an evidence render, not a database-client screenshot; the executable SQL and its ordered build stages remain inspectable below.
The same retained files provide 6,351 invoice lines, £4,970,933 net sales, 49.15% gross margin and 18 passing report checks. Open the evidence viewer for the full-resolution result; the mobile source uses a dedicated readable composition.
The May 2016 management view
The retained SQL tables include the monthly commercial report and product margin report. Finance can review the movement in net sales and margin while sales and warehouse managers can identify products whose value, margin and stock position warrant a closer look.
What management should notice
These observations are calculated from the retained monthly and product reports. They identify where to investigate; they do not claim a cause that the invoice and stock records cannot prove.
May reached £4,970,933, up from £4,563,666 in April and +10.9% against May 2015.
Gross margin recovered to 49.15% after April, but remained 0.41 points below May 2015. Finance can separate a sales recovery from a margin recovery.
The eight largest products produced almost three tenths of retained net sales. Air cushion machine (Blue) alone contributed 6.4%, but its 39.97% margin was 9.80 points below the overall product mix.
Three event streams
The monthly pack does not flatten the wholesaler's records into one vague table. Each stream keeps its own business meaning, then joins through shared customer, product, supplier, warehouse and date definitions.
One posted sales invoice line
228,265Sales.InvoiceLinesFeeds net sales, gross profit and product margin.
One ordered supplier item
8,367Purchasing.PurchaseOrderLinesShows purchasing commitments beside stock and sales demand.
One warehouse movement event
236,667Warehouse.StockItemTransactionsExplains whether the month-end stock picture agrees with product movement.
How the records become a report
Each fact table has a declared grain: one posted invoice line, one purchase order line or one stock movement. Shared dimensions make sure a product or customer means the same thing wherever it appears.
Sales invoices, purchase orders and warehouse stock movements arrive at their own level of detail.
Business keys, references, dates and values are tested. Failed rows keep their reason and original key.
Every report uses the same customers, products, suppliers, warehouses, salespeople and dates.
Sales, margin, purchasing and stock outputs are released only after all 18 checks pass.
The reporting tables cover 2013-01-01 to 2016-05-31. Sales and margin come from invoice lines, purchasing commitments come from order lines, and warehouse movement remains a separate event fact.
The selected Microsoft records all passed the release rules. Nine deliberately broken fixture records prove that duplicate, missing-reference, date, cancellation, credit and value-mismatch cases are detected without being mixed into Microsoft's sample data.
The SQL that builds the monthly report
The monthly table counts invoice lines, adds net sales and gross profit, then keeps purchase order and stock movement totals at their own meaningful level. DuckDB makes the example portable; it does not imply a live cloud platform or production scheduler.
CREATE TABLE mart.monthly_commercial_report AS
WITH months AS (
SELECT DISTINCT month_start
FROM conformed.dim_date
),
sales AS (
SELECT
date.month_start,
COUNT(*) AS invoice_lines,
COUNT(DISTINCT invoice_id) AS invoices,
SUM(quantity) AS units_sold,
SUM(net_sales_value) AS net_sales_value,
SUM(line_profit) AS gross_profit,
SUM(extended_price) AS invoice_value
FROM conformed.fact_sales sale
JOIN conformed.dim_date date ON sale.invoice_date_key = date.date_key
GROUP BY date.month_start
),
purchasing AS (
SELECT
date.month_start,
COUNT(*) AS purchase_order_lines,
SUM(expected_order_value) AS expected_purchase_value
FROM conformed.fact_purchasing purchase
JOIN conformed.dim_date date ON purchase.order_date_key = date.date_key
GROUP BY date.month_start
),
stock AS (
SELECT
date.month_start,
COUNT(*) AS stock_movements,
SUM(quantity) AS net_stock_movement
FROM conformed.fact_stock_movement movement
JOIN conformed.dim_date date ON movement.transaction_date_key = date.date_key
GROUP BY date.month_start
)
SELECT
months.month_start,Eight ordered files create the schemas, load the Microsoft records, build the reporting tables and stop the release when a check fails.
00_create_schemas.sqlCreates raw, staging, conformed, mart and audit schemas before any reporting table is built.
10_load_raw.sqlLoads the selected Wide World Importers tables without changing the Microsoft business values.
20_stage_sources.sqlJoins operational headers to their lines, types the fields and records failures before the reporting tables are released.
30_build_dimensions.sqlCreates shared date, customer, product, supplier, warehouse and salesperson dimensions.
40_build_facts.sqlBuilds one sales row per posted invoice line, one purchasing row per order line and one stock movement per event.
50_build_marts.sqlProduces the monthly commercial report, product margin view and current stock position.
60_run_tests.sqlChecks keys, dates, references, row counts and invoice, purchase and stock values before release.
70_build_trace_and_release.sqlRetains the six-step invoice-line trace and records whether the monthly report is ready.
One invoice line, start to finish
The trace is generated by the build. It is not a narrative example typed into the website after the fact.
Sales.InvoiceLinesInvoice 1 · stock item 67 · quantity 10 · invoice value £2,645
The original Microsoft row is retained without business rewriting.
staging.sales_line_qualityCustomer 832 · order 1 · invoice date 1 January 2013
The line is joined to its invoice and sales order, and its dates and references are checked.
conformed dimensionsCustomer key 434 · product key 67 · salesperson key 1
Stable reporting keys connect the line to the same customer, product, salesperson and date used by every report.
conformed.fact_salesNet sales £2,300 · gross profit £850
The published fact keeps one row per posted invoice line.
audit.validation_results18 of 18 checks passed
Count, key, relationship, date and value checks must pass before release.
mart.monthly_commercial_reportRevenue measure = SUM(fact_sales[net_sales_value]) · month 1 January 2013
The line contributes once to the retained monthly sales result and to the documented downstream DAX measure.
Before the report is released
The SQL build stops if a control fails. A second clean rebuild then compares the row counts, financial totals and reproducibility fingerprint so the same inputs cannot quietly produce a different result.
| Check | Result | Owner | Why it matters |
|---|---|---|---|
| Invoice-line source scale | Pass | Data engineer | The selected official source must exceed the publication threshold. |
| Invoice-line business key is unique | Pass | Finance systems analyst | One source invoice line can contribute at most once. |
| Invoice value reconciles to fact and held rows | Pass | Finance business partner | The invoice value bridge must balance to one penny. |
| Purchase-order lines reconcile to fact | Pass | Purchasing analyst | Every valid purchase line enters the purchasing fact once. |
| Stock movements reconcile to fact | Pass | Warehouse manager | Every valid movement enters the stock fact once. |
| Monthly sales mart ties to sales fact | Pass | Finance business partner | The monthly report retains the complete net sales value. |
The retained validation file contains all 18 checks, including 6 release controls highlighted here.
Release reconciliation · May 2016
The May 2016 mart contains 6,351 invoice lines and £4,970,933 of net sales. Gross margin is 49.15%.
Purchasing contributes 210 order lines, while the warehouse view retains 6,562 movement events. Finance, sales and warehouse managers can compare those outputs without flattening them into one ambiguous table.
posted invoice lines across the retained Microsoft sample.
invoice lines enter the sales fact once after the checks pass.
official invoice lines are kept out of the report in this build.
broken fixture rows are rejected before they can affect a report.
Data and limits
npm run verify:sql-foundationThe command verifies Microsoft's BACPAC checksum, extracts the selected tables, runs all eight SQL stages, exports the results and performs a second clean rebuild. The retained result fingerprint is 47ea77cb3db30d65821907bb622a48a1.
The data comes from Microsoft's Wide World Importers public sample, release wide-world-importers-v1.0, under the Microsoft SQL Server samples MIT licence.
Next step
Start with the files, joins and checks that make the current report difficult to explain or maintain.