This is Part 5 of the “Restaurant PO Automation” series. Earlier parts established the five-node n8n workflow - Gmail Trigger, PDF text extraction, the Code node, and the Google Sheets data model - that appends every purchase-order line into Order_Lines. This post solves the last mile: those 20 recorded rows are raw intake, not something a warehouse can dispatch from. We build two report sheets with one QUERY formula each, so per-outlet sorting and central staging both update automatically.

Series guide: Restaurant Purchase Order Automation: Series Guide & Case Study

1. The gap between “20 rows recorded” and “a warehouse can dispatch”

The brief matters. A Singapore warehouse operator receives purchase orders by email from restaurants of the same brand but different outlets; goods cover fresh produce, protein, frozen and dry goods; each email carries a PDF. The manual job is opening that PDF, reading the PO number and quantities, and typing them into Google Sheets for daily dispatch. By Part 4, four simulated Harbour Kitchen PDFs - Orchard, Marina Bay, Jurong East, Tampines, five lines each - had produced 20 rows, 10 per delivery date (2026-11-23 and 2026-11-24), correctly labelled. Recording was done.

The 20 raw Order_Lines rows appended by the n8n workflow

But recorded is not dispatchable. A flat append log preserves arrival order; it does not tell a picker what goes on the Orchard van, nor staging how many kilograms of tomatoes to pull. Stopping at 20 rows automates the typing but not the decision. This post closes that gap without adding a node.

2. Two different questions the warehouse asks

A warehouse asks two different questions. What does each outlet get? is per-outlet and per-date, driving sorting, packing and the delivery run. Losing the outlet dimension is a serious error: Orchard’s 20 KG of Fresh Tomatoes and Marina Bay’s 15 KG are two deliveries, not one. How much do we stage in total? is asked before any van is loaded; prep wants one number per product per day, with the outlet deliberately dropped.

Both answers come from the same 20 rows. We keep three sheets in one workbook, Restaurant PO Automation - Demo:

SheetRowsGrouped byQuestion it answersUsed by
Order_Lines20Nothing - raw append from n8nWhat did each PDF say?Audit, source of truth
Daily_Dispatch20Delivery Date + Outlet Code + SKU + Product + UnitWhat does each outlet get on each date?Per-outlet sorting and delivery
Product_Summary19Delivery Date + SKU + Product + Unit (no outlet)How much do we stage in total?Central staging and prep

The 20 / 20 / 19 shape is intentional: Daily_Dispatch keeps 20 because every raw line is a distinct outlet-and-product combination; Product_Summary drops to 19 because two outlets ordered the same product on the same date. A merge, not data loss.

3. Why Google Sheets formulas, not more n8n nodes

The obvious reflex is a second workflow, or a scheduled node that reads Order_Lines, aggregates it and writes two more sheets. We deliberately did not. New mail keeps appending to Order_Lines, and a QUERY reads that growing range directly, so both report sheets update automatically whenever a row lands - no second workflow, no re-execution, no scheduler, no state to reconcile. A workflow-generated report would re-run on every email and either replace rows in place or append duplicates. The formula has no write step at all.

This was an explicit “do not add features” decision. The final workflow is five main nodes - Gmail Trigger -> Extract From File -> Information Extractor -> Code -> Google Sheets - with the AI Chat Model a sub-node of Information Extractor, not a sixth node. Five nodes already show the MVP’s value; adding Airtable, inventory logic, dispatch emails or another AI Agent would add parts without evidence. We let the spreadsheet report.

A successful n8n execution alongside the dispatch data in Google Sheets

4. The eleven-column map

QUERY addresses columns by letter, so the schema must be pinned down. Order_Lines has exactly eleven:

LetterHeaderExampleUsed
APO_NumberPO-20261123-001No
BOrder_Date2026-11-20No
CDelivery_Date2026-11-23Group, order, filter
DBrandHarbour KitchenNo
EOutletOrchardNo
FOutlet_CodeHK-ORC-01Daily_Dispatch only
GSKUVEG-001Group, order
HProductFresh TomatoesGroup
ICategoryProduceNo
JQuantity20sum(J)
KUnitKGGroup

Three outlet codes appear in the notes: HK-ORC-01, HK-MBY-02, HK-JRG-03. SKU prefixes VEG-, PRO-, FRZ-, DRY- map to Produce, Protein, Frozen, Dry Goods; units are KG, CTN, BAG, BTL. Both formulas read Order_Lines!A1:K, with 1 declaring one header row. where C is not null is essential: an open range includes empty rows below the data, and without the filter the aggregation produces a stray blank group.

5. Building Daily_Dispatch

Create Daily_Dispatch and put this in A1:

=QUERY(
  Order_Lines!A1:K,
  "select C,F,G,H,K,sum(J)
   where C is not null
   group by C,F,G,H,K
   order by C,F,G
   label C 'Delivery Date',
         F 'Outlet Code',
         G 'SKU',
         H 'Product',
         K 'Unit',
         sum(J) 'Total Qty'",
  1
)

select picks five dimensions plus sum(J); where C is not null drops blanks; group by sets the grain - one row per date, outlet, SKU, product and unit; order by sorts by delivery date, then outlet, then SKU, the order a picker works in; label renames raw letters into business headers. A1:F1 auto-generates six columns: Delivery Date | Outlet Code | SKU | Product | Unit | Total Qty.

The Daily_Dispatch sheet showing six generated columns and 20 sorted rows

Confirmed rows:

Delivery DateOutlet CodeSKUProductUnitTotal Qty
2026-11-23HK-ORC-01VEG-001Fresh TomatoesKG20
2026-11-23HK-MBY-02VEG-001Fresh TomatoesKG15
2026-11-24HK-JRG-03VEG-004Fresh CarrotsKG25

The first two are the point: same date, SKU, product and unit, but two rows because F is in group by. Orchard loads 20 KG, Marina Bay 15 KG - no silent folding.

6. Building Product_Summary

Create Product_Summary with the outlet dimension removed:

=QUERY(
  Order_Lines!A1:K,
  "select C,G,H,K,sum(J)
   where C is not null
   group by C,G,H,K
   order by C,G
   label C 'Delivery Date',
         G 'SKU',
         H 'Product',
         K 'Unit',
         sum(J) 'Total Qty'",
  1
)

F is gone from select, group by, order by and label. Staging does not care which van a case goes on, only how much to prepare. Output is five columns in A1:E1: Delivery Date | SKU | Product | Unit | Total Qty.

The Product_Summary sheet showing five generated columns and 19 consolidated rows

Now the 20 -> 19 drop makes sense. On 2026-11-23 Orchard ordered Fresh Tomatoes at 20 KG and Marina Bay at 15 KG. Daily_Dispatch keeps two rows; here both collapse into one with sum(J) = 35. That single merge is the entire difference. Our staging example:

ProductOrchardMarina BayStaging total
Fresh Tomatoes20 KG15 KG35 KG
Romaine Lettuce12 KG-12 KG
Frozen French Fries8 CTN-8 CTN

Dispatch says 20 and 15 to two addresses; staging says 35 KG out of cold storage. Both are correct, from the same untouched source rows.

7. When the formula shows up as plain text

One gotcha is worth keeping. The QUERY appeared as plain text in A1: the whole formula visible inside one cell, spanning many rows, nothing calculated. Sheets had not recognised it as a formula. The fix is formatting, not logic:

  1. Select A1, press Delete.
  2. Format -> Number -> Automatic, and confirm the cell is not “Plain text” - a text-formatted cell never evaluates a leading =.
  3. Copy the single-line formula, not the pretty-printed block.
  4. Paste into A1, press Enter.
  5. Ignore and close the Gemini “convert selected text to table” side panel; it is unrelated.
  6. Check the formula bar starts with a real =, no leading ' or space.
  7. #ERROR! usually means the locale requires ; instead of , as the argument separator. Swap the two top-level commas; leave the quoted column lists unchanged.

Single-line forms to paste:

=QUERY(Order_Lines!A1:K, "select C,F,G,H,K,sum(J) where C is not null group by C,F,G,H,K order by C,F,G label C 'Delivery Date', F 'Outlet Code', G 'SKU', H 'Product', K 'Unit', sum(J) 'Total Qty'", 1)
=QUERY(Order_Lines!A1:K, "select C,G,H,K,sum(J) where C is not null group by C,G,H,K order by C,G label C 'Delivery Date', G 'SKU', H 'Product', K 'Unit', sum(J) 'Total Qty'", 1)

Once evaluated, A1:F1 fills with six headers and A1:E1 with five, data rows beneath. Verify Daily_Dispatch before creating Product_Summary, so there is one unknown at a time.

8. Why Unit is a grouping key

K (Unit) is in both group by clauses because Total Qty must only be summed for the same SKU and the same unit. Adding 8 CTN of fries to 10 BAG of rice, or mixing BTL into a KG total, gives a precise-looking number that means nothing. Grouping on Unit keeps the four units separate; we never convert, since that needs pack-size master data we do not have.

9. Category drift on PO 04 and why it does not affect these reports

The fourth PDF used a different template, and the AI returned Vegetables and Meat for Category where the first three produced Produce and Protein. We do not hide it, but it has no effect on either report: neither QUERY references column I, so a label change cannot alter a sum(J) value or a row count.

A small SKU-prefix mapping in the Code node would fix it later; we have not implemented it.

10. What these two sheets replace in the manual routine, and the scope note

Manually, someone opened each PDF, read the PO number and quantities, typed them into Sheets, then re-grouped those rows by outlet and product to plan picking and staging. Daily_Dispatch and Product_Summary replace that second pass. A dispatcher filters Daily_Dispatch by delivery date and outlet code; the staging lead reads Product_Summary for total prep per SKU. Both refresh as new purchase emails append rows - no re-keying, no re-execution.

The boundary must be explicit: this is a DEMO, not a production warehouse system. There is no duplicate-order handling, no revised PO handling, no exception review and no audit of dispatched quantities. A production version would retain the original PDF for audit and route unrecognised rows to human review. Everything beyond these two report sheets is phase two. Part 6 covers the four-PDF test.

Next: Part 6 - Testing Four PDF Variants and Presenting the Result

Data note: all business data here - Harbour Kitchen, its outlets, PO numbers, SKUs, quantities and addresses - is fictional mock data for a Self-initiated Demonstration. Images come from the demo workbook, not a live client system.

Related reading