Every planner asks the same question each morning
“For this job, which parts can I issue today, which ones are coming, and where exactly are they coming from?”
Epicor holds every number needed to answer it: required quantities on job material lines, on-hand stock, open purchase order releases, and make-to-stock or rework jobs that will produce parts. What it does not do is commit that supply to specific demand. The part-level view says “40 on hand and 65 in demand”. It does not say which three jobs get the 40, which job waits for the rework batch due Friday, and which one still has nothing coming.
We built a material allocation and pegging engine in SQL Server for a precision-assembly manufacturer, with a Power BI kit-readiness report on top, and ran it in production for five years. This article explains the business logic, the SQL design and the report, and the redesign that followed the five-year review.
The business problem: one pool of stock, many jobs
A build is released as a job. Each job has a bill of material: material lines that each require a quantity of a part by a date. The kit is complete only when every line is either issued or physically available. Supply for a part comes from three places:
| Supply source | What it is | Usable from | Epicor tables |
|---|---|---|---|
| On hand | Stock in nettable warehouses | Today | PartWhse |
| Open jobs | Rework or make-to-stock jobs that will put the part into stock | The job’s due date | JobHead, JobProd |
| Purchase orders | Open PO releases from suppliers | The release’s promise or due date | PODetail, PORel |
The hard part is that many jobs want the same part at the same time. Without a rule for who gets what, every job sees the same 40 units and looks fine until the shelf is empty. Planners then fix it by hand in spreadsheets that are out of date before the meeting ends. The business needed four answers, refreshed several times a day:
- What can be issued now? Lines fully covered by on-hand stock.
- What is coming, from where, and when? For every line, the exact job or PO release that will cover it, the quantity and the date.
- What is short? Quantity with no supply at all, which drives new purchase orders.
- Is the kit on time? Whether the covering supply arrives before the line’s need date.
The allocation rules
Five simple rules, applied together, turn a pile of ERP numbers into a commitment planners can act on:
- Only open demand counts. A material line takes part only if its job is not complete or closed, the line is not issue-complete, and required quantity exceeds issued quantity. Open quantity is required minus issued.
- Demand is served in need-date order. For each part, lines queue by material need date, then job start date, then job number. The tie-breakers make every run repeatable.
- Supply is consumed in a fixed priority. On hand first, then open rework or make-to-stock jobs, then PO releases. Within a source, the earliest-available supply goes first.
- Supply is counted once. A PO release offers only what has not been received; a job offers only what it has not yet put into stock. Anything received is already in on-hand.
- Hard pegs win. A PO bought for one specific job line serves that line first and is not given to another job.
One line can be covered by several sources, for example 4 from stock, 1 from a rework job and 2 from a PO. The engine keeps every slice, not just a total.
A worked example: 35 units wanted, 32 available
Five jobs need Part A, 35 units in total. Supply adds up to 32 units across four sources, consumed in this order:
| # | Source | Qty | Available on |
|---|---|---|---|
| 1 | On hand | 10 | Today |
| 2 | Rework job RW-01 | 4 | 5 Dec |
| 3 | PO-501, line 1, release 1 | 12 | 12 Dec |
| 4 | PO-502, line 1, release 1 | 6 | 10 Jan |
The result is one row per job line, with every source that covers it:
| Job | Need date | Open qty | Covered by | Short | Status |
|---|---|---|---|---|---|
| J-101 | 2 Dec | 6 | On hand 6 | 0 | Ready to issue |
| J-102 | 4 Dec | 5 | On hand 4 + RW-01 1 (5 Dec) | 0 | Late by 1 day |
| J-103 | 8 Dec | 7 | RW-01 3 (5 Dec) + PO-501 4 (12 Dec) | 0 | Late by 4 days |
| J-104 | 15 Dec | 8 | PO-501 8 (12 Dec) | 0 | On time |
| J-105 | 5 Jan | 9 | PO-502 6 (10 Jan) | 3 | Short, on order |
Three things stand out. J-102 and J-103 are fully covered but late, because part of their supply arrives after the need date; a plain shortage report would show them as fine. J-105 is short by exactly 3, the net quantity purchasing still has to buy (35 minus 32). And the planner can see which PO to expedite: pulling PO-501 forward by four days fixes J-103.
Now add the kit view. J-101 also needs 2 of Part B, which has no stock and no PO. So although its Part A line is ready, the J-101 kit is Not on PO, and that is the line to act on first.
Architecture: five thin layers
Each layer has one job. Epicor is only read, never written. Demand and supply are prepared as two clean views. One stored procedure does all the allocation and publishes four result tables. Power BI only displays and slices those tables and holds no business rules of its own.
From cursors to one set-based query
The first version allocated row by row: an outer cursor walked the demand lines and, for each one, an inner cursor walked that part’s supply, updating both tables at every step. It worked for five years, but it was slow, hard to test, and could store only one source per line.
The redesign uses one idea: put demand and supply on the same number line. For each part, lay open demand end to end in priority order, and do the same for supply. Wherever a demand interval overlaps a supply interval, the overlap is the allocated quantity.
WITH dem AS (
SELECT *, DemTo = SUM(OpenQty) OVER (PARTITION BY PartNum ORDER BY PriorityRank
ROWS UNBOUNDED PRECEDING)
FROM #Demand
), sup AS (
SELECT *, SupTo = SUM(SupplyQty) OVER (PARTITION BY PartNum
ORDER BY Priority, SupplyDate, SupplyKey
ROWS UNBOUNDED PRECEDING)
FROM #Supply
)
SELECT d.JobNum, d.MtlSeq, s.SupplyType, s.SupplyKey, s.SupplyDate,
AllocQty = LEAST(d.DemTo, s.SupTo)
- GREATEST(d.DemTo - d.OpenQty, s.SupTo - s.SupplyQty)
FROM dem d
JOIN sup s ON s.PartNum = d.PartNum
AND d.DemTo - d.OpenQty < s.SupTo -- demand starts before supply ends
AND s.SupTo - s.SupplyQty < d.DemTo; -- supply starts before demand ends
Each output row is a peg: this many units of this supply go to this job line. Totals per line, shortage, late days and the “covered by” text are simple aggregates of the pegs. LEAST and GREATEST need SQL Server 2022 or Azure SQL; on older versions use a CASE expression.
Before switching, we tested the set-based query against a line-by-line replica of the old cursor logic on 400 randomly generated scenarios, including fractional quantities and both priority modes. It produced identical allocations in all 400. The rewrite changed the speed and the transparency, not the business rule.
The Power BI kit-readiness report
The report is where the allocation becomes a daily decision. It opens at kit level and lets planners drill to a single peg in two clicks.
| Page | Question it answers | Key content |
|---|---|---|
| Kit Status | Which kits can we build, and which are at risk? | Kits complete, parts fully on hand, issued and on-time percentages; one row per kit marked On Time, Not On Time or Not On PO |
| Kit Details | Why is this kit not ready? | Every line of one kit: required, issued, allocated, short, and the job or PO covering it with its date |
| Shortage | What must purchasing buy or expedite? | Only lines with a shortage, with open PO quantity and due date beside them |
| Time Phase | When does supply land against need? | Need dates against PO and job dates by program line |
| Quality Hold | Is stock blocked by quality? | Parts in discrepancy review (DMR) or awaiting receiving inspection |
A kit’s status follows a strict order: if any line is Not On PO the kit is Not On PO; otherwise, if any line is late, the kit is Not On Time; otherwise it is On Time. One red line is enough to stop a build, so one red line colours the kit.
Slicers for program line, category, job, part and supply source let the same report serve the production meeting, purchasing and management. Two “data as of” cards, one for the Epicor extract and one for the report refresh, tell users how fresh the numbers are.
Lessons from five years in production
Reviewing the original build after five years turned up seven problems. None had crashed anything; each was quietly changing numbers. They are common in allocation code, so they are worth checking in any similar system.
- Received quantity counted twice. PO releases offered their full quantity even after some units were received and already in stock. A release for 10 with 6 received gave on hand 6 + PO 10 = 16 “available”; the truth is 6 + 4 = 10. A job needing 14 showed as covered when it was 4 short.
- Join fan-out. Joining material lines to stock by part only, without the warehouse, returned one row per warehouse, so a part stocked in two warehouses doubled every demand line. Fix: sum stock to one row per part before joining.
- Integer variables for decimal quantities. 2.5 m of tubing or 0.75 kg of sealant were truncated to 2 and 0. Fix:
decimal(18,4)everywhere. - Overlapping query branches. Two branches of the source view both picked up jobs with no product line, so their material demand was allocated twice.
- One source per line. Each allocation step overwrote the previous source, so a line covered by stock plus two POs showed only the last PO. Fix: a pegging ledger with one row per slice.
- Hard-coded business values. Company codes, program IDs, job exclusions and line-name mappings were typed into four places: two procedures, a view and the report. Fix: small configuration tables maintained in one place.
- Non-atomic refresh. The reporting table was truncated and reloaded while users were reading it, so a refresh at the wrong second showed nothing. Fix: build in staging, publish inside one short transaction, log every run, and keep the previous result if any check fails.
The report had smaller issues of its own. Two status measures compared against "OnTime" while the data said "On Time", so they always returned zero. Auto date/time had created 17 hidden date tables. And shortage logic existed both in SQL and in a DAX calculated column, so the two could disagree. Business rules now live only in SQL.
What it changed for the business
- Production builds the kits that are ready and does not start ones that will stall halfway.
- Purchasing works from the net shortage per part, not gross demand, and knows which PO release to expedite for which job.
- Planning sees late-but-covered lines weeks ahead, while there is still time to move a PO or re-sequence jobs.
- Management tracks kit readiness per program line from one report instead of asking each team.
Frequently asked questions
What is supply pegging in manufacturing?
Pegging links each unit of supply (stock, a job or a purchase order release) to the specific demand it will serve, such as a job material line. Unlike a part-level shortage list, it shows which job gets which supply, how much, and when it arrives.
Can Epicor Kinetic show which PO will cover a job material line?
Epicor holds the demand (JobMtl), stock (PartWhse) and open supply (JobProd, PORel), but its standard views do not commit supply to specific job lines. A pegging engine that reads those tables and allocates supply in need-date order produces that answer.
Why replace a cursor-based allocation with a set-based query?
Laying demand and supply on the same cumulative quantity line and joining the overlaps gives the same allocation in one statement. It runs faster, is easier to test, and keeps every slice when one line is covered by several sources.
How do you avoid double-counting received PO quantities?
Offer only the unreceived part of each PO release and job as future supply. Anything already received is already in on-hand stock, so counting the full release quantity overstates supply.
The takeaway
The technique is not tied to one ERP. Any system with demand lines, stock and open supply can use the same three steps: lay demand and supply on a cumulative line, join the overlaps, and keep every slice as a peg. Combined with honest supply quantities and an atomic refresh, it turns Epicor’s raw numbers into a plan people trust.
Related: quantity-range allocation in an Epicor pack slip report, which uses the same overlap idea, and automating PO deposit payment schedules.