Cost of goods sold = beginning inventory + purchases - ending inventory.
That is the cost of the stock that left your business during the period. Gross profit is revenue minus COGS. Your COGS margin is COGS divided by revenue.
Each term has a precise meaning, and two of the three are valuations rather than facts.
| Term | What it means |
|---|---|
| Beginning inventory | The value of the stock you held on day one of the period. It is last period's ending inventory, unchanged |
| Purchases | The cost of the stock you added during the period. Not what you ordered, and not what you paid out: what arrived, at what it cost to get it there |
| Ending inventory | The value of the stock you still held on the last day. Usually a physical count, priced using a costing method |
| COGS | The direct cost of the goods sold in the period. A debit to the profit and loss, credited out of the inventory asset |
The two figures people usually want next:
- Gross profit = revenue - COGS
- Gross profit margin = (revenue - COGS) / revenue x 100
- COGS margin, sometimes called the COGS ratio = COGS / revenue x 100
So far this is the version on every accounting site, and it is correct. What almost none of them tell you is the part that decides whether the answer is any use: that formula is not a calculation of cost of goods sold. It is a subtraction that produces whatever is left over, and it will hand you a confident number in situations where the number is meaningless.
Here is the whole thing worked through, and then the three places it goes wrong.
A worked quarter, start to finish
One product, one quarter, round numbers you can check. This is a worked example, not a customer.
You started the quarter with 400 units valued at $12.00 each, so beginning inventory was $4,800. During the quarter one shipment arrived: 600 units that cost $9,600 to land, or $16.00 each. (Where that $16.00 comes from is its own section further down, and it is where most of the real errors live.)
At the end of the quarter you counted the shelves and found 250 units. Priced oldest-out-first, the 250 units still on hand are the most recent ones, so they are valued at $16.00 each: $4,000.
| Line | Units | Value |
|---|---|---|
| Beginning inventory | 400 | $4,800 |
| Plus purchases | 600 | $9,600 |
| Goods available for sale | 1,000 | $14,400 |
| Less ending inventory | 250 | $4,000 |
| Cost of goods sold | 750 | $10,400 |
Check it the other way and it ties. Oldest first, the 750 units that left were the 400 opening units at $12.00 ($4,800) and 350 of the new ones at $16.00 ($5,600). That is $10,400, the same figure, arrived at from the other direction.
Now the follow-on figures. If those 750 units sold at $32.00 each, revenue was $24,000.
- Gross profit = $24,000 - $10,400 = $13,600
- Gross profit margin = $13,600 / $24,000 = 56.7%
- COGS margin = $10,400 / $24,000 = 43.3%
Those two percentages add to 100, which is the only quick check worth doing on them.
The formula back-solves. It does not compute
Read the worked example again and notice what actually happened. Nothing in it measured the cost of a sale. You took everything that was available, subtracted what you could still see, and called the remainder cost of goods sold.
That is a specific accounting approach with a name: the periodic inventory method. The formula everyone publishes as "the COGS formula" is the periodic method written out. It is not a general law of accounting, it is one of two systems, and it is the one that does its arithmetic once, at period end, from a count.
The clearest evidence that this is a back-solve rather than a calculation is on the form the United States uses to report it. IRS Form 1125-A, titled Cost of Goods Sold, walks through eight lines. Line 1 is "Inventory at beginning of year". Line 2 is "Purchases". Line 6 is "Total. Add lines 1 through 5". Line 7 is "Inventory at end of year". And line 8, the answer, is "Cost of goods sold. Subtract line 7 from line 6." (Form 1125-A, Rev. November 2024, checked August 2026.)
There is no line anywhere on that form for the cost of a sale. COGS is defined as a difference, so anything that reduced your closing stock without being a sale is inside line 8 and cannot be separated from it. Theft, breakage, a unit that went out as a warranty replacement, a miscount, a pallet that was never scanned in: all of them land in cost of goods sold, and all of them look exactly like something a customer bought.
The form even hints at it. Line 9f asks whether there was "any change in determining quantities, cost, or valuations between opening and closing inventory", and asks you to attach an explanation if there was. That question exists because the answer changes the number, and nothing else on the form would reveal it.
So the honest description of the formula is this. It tells you how much inventory value left the business. It does not tell you why it left, which units left, or what any individual sale cost you.
Periodic or perpetual: which one are you actually running?
The alternative system does not use that formula at all. Perpetual inventory computes COGS at the moment of each sale, from the cost of the specific units that shipped, and updates the inventory balance as it goes. COGS is the running total of those individual costs. Closing inventory is then whatever the ledger says is left, and the physical count becomes a check on the ledger rather than the input to it.
| Periodic | Perpetual | |
|---|---|---|
| COGS is | Back-solved once, at period end | Computed per sale, as it happens |
| The stock count is | The input. Nothing works without it | The check. It confirms or contradicts the ledger |
| Shrinkage appears | Nowhere. It is absorbed into COGS | As a variance between expected and counted |
| Margin per order | Not available | Available, because each order has a cost |
| Effort | A count, and an hour of arithmetic | A cost record behind every receipt of stock |
Now the part that matters if you sell online. Shopify decrements stock on every order, all day, without being asked. Your quantity records are already perpetual and have been since the day you opened the store.
What is almost certainly still periodic is your cost record. Nobody attached a cost to any of those movements, so at quarter end you fall back on the formula: open the count, add up the supplier invoices, subtract, and post. That mismatch is the actual situation most stores are in. Half the system updates in real time and half of it updates four times a year, and the formula is the bridge people use to paper over the gap.
It is also why COGS moves for reasons nobody can name. The quantity side is precise to the unit. The cost side is a quarterly remainder, and a remainder absorbs everything you did not measure.
Two very different quarters, one identical COGS
This is the failure worth seeing in arithmetic rather than in the abstract. Same product, same opening stock, same shipment, same closing count.
Quarter A. You sold 750 units to customers. Nothing was lost. You counted 250 on the shelf.
Quarter B. You sold 700 units to customers. A further 50 units left the building some other way: a damaged carton nobody wrote off, a return that was never put back, two units that walked. You counted 250 on the shelf, because the 50 are not there to count.
Put both through the formula:
- Quarter A: $4,800 + $9,600 - $4,000 = $10,400
- Quarter B: $4,800 + $9,600 - $4,000 = $10,400
Identical. Not approximately, exactly. The formula cannot distinguish a quarter where everything sold from a quarter where you lost 50 units, because the only thing it looks at is what is left.
Follow it into the profit and loss and the damage becomes visible. Quarter B's revenue is 700 x $32.00 = $22,400, not $24,000. Reported against the full $10,400, gross profit is $12,000 and the margin is 53.6%.
But $10,400 was never the cost of what you sold. Say the 50 missing units came from the newer receipt at $16.00: that is $800 of inventory loss, and the true cost of the 700 units sold is $9,600 (400 at $12.00, plus 300 at $16.00). Presented properly, cost of sales is $9,600, gross profit is $12,800, the gross margin is 57.1%, and there is a separate $800 line telling you something is going wrong in the warehouse.
The formula gave you neither. It gave you a gross margin about three and a half points worse than the truth, with no clue attached, in a quarter you will spend arguing about supplier prices. That is what "COGS moved and we do not know why" is, most of the time.
A perpetual system does not fix your warehouse. It does something narrower and more useful: it prices the sales separately from everything else, so the difference between what the ledger expected and what the count found is a number on its own, not a rounding into cost of sales.
What actually belongs in purchases?
Back to the $16.00. Purchases is the input people get wrong most often, and for an importer it is wrong by a wide margin, in both directions at once.
Purchases is not what you paid the supplier. It is the cost of bringing the goods to the point where they are ready to sell. In the worked example the 600 units break down like this:
| Charge | Amount | In purchases? |
|---|---|---|
| Supplier invoice, 600 units | $8,400 | Yes |
| Freight and clearance | $840 | Yes. It is part of what the units cost to get here |
| Customs duty | $360 | Yes. You never get it back, so it is a cost |
| Recoverable import tax | $960 | No. You claim it back, so it is cash tied up, not cost |
| Total in purchases | $9,600 | $16.00 a unit |
Both halves of that matter. Leave the $1,200 of freight and duty out and your unit cost is $14.00, your COGS for the quarter is understated, and your closing stock is undervalued on the balance sheet. That is the error most importers are carrying, and it is the whole subject of what landed cost includes.
The other half is the one bookkeepers get caught by. If your border charges a recoverable import tax (GST, VAT, or whatever your border charges), you pay it on the way in and claim it back on your next return. In Australia that is 10% of the customs value plus transport plus duty, which on this shipment is $960. Put that $960 into purchases and the unit cost becomes $17.60. Run the same formula: closing stock becomes $4,400, COGS becomes $10,960, and you have overstated your cost of sales by $560 and your inventory asset by $400. The two add to $960, which is money you are about to get back from the tax office and have just also claimed as an expense.
Customs duty is the opposite case and the reason the two are so easily confused: both are charged at the border by a government, and only one of them comes back. If you import into the United States there is no recoverable line at all. Duty and fees are paid and kept, so there is nothing to strip out and nothing to claim. (How to code it so you can still claim it.)
Two smaller ones, because they are the most common questions about this input. Outbound shipping, what it costs you to send the parcel to the customer, is not part of purchases and not part of COGS. It is a selling cost. Inbound freight capitalises into the goods; outbound freight is spent on getting rid of them. And labour belongs in COGS only if it went into making the product, which is why the IRS form carries a separate line 3 for "Cost of labor". If you buy finished goods and resell them, your packing staff are an operating expense.
Same inputs, two answers
There is one more thing the formula does not settle, and it is worth $400 in the worked example alone.
Ending inventory is 250 units. Which 250? You held 400 units that cost $12.00 and bought 600 that cost $16.00, and the units on the shelf do not have prices written on them. The answer depends on the costing method you apply, and the accounting standards call these cost formulas rather than opinions: IAS 2 requires either first-in, first-out or weighted average cost for items that are ordinarily interchangeable (IAS 2 Inventories, checked August 2026).
| From the same inputs | FIFO | Weighted average |
|---|---|---|
| Unit cost applied | $12.00 until the old layer runs out, then $16.00 | $14,400 / 1,000 = $14.40 for everything |
| Ending inventory, 250 units | $4,000 | $3,600 |
| Cost of goods sold | $10,400 | $10,800 |
| Gross profit on $24,000 | $13,600 | $13,200 |
Same stock, same sales, same count, $400 of difference and a different closing balance sheet. Neither is wrong. They answer different questions, and the one that matters commercially is what your next unit costs to replace: the average says $14.40, which is true of none of the units you are holding, while FIFO says the ones you are about to ship cost $12.00 and everything after that costs $16.00. (FIFO or average cost, and when the gap actually matters.)
All three inputs are valuations
Step back and look at what the formula is made of. Beginning inventory is a valuation. Purchases is a valuation. Ending inventory is a valuation. Not one of the three is a fact you can read off a document, and all three depend on the same thing: what you decided a unit cost.
Which is where this stops being an accounting exercise. Every receipt of stock arrives at a different price, and your store keeps one. One field, one number, overwritten each time you update it, with no record of what it used to be or which units it applied to. Run the formula off that field and every input is priced at whatever you last typed, including the units that arrived before you typed it.
The freight invoice makes it worse, and nobody is at fault for that either. It turns up weeks after the container, often after the stock has started selling, so the cost you sold at was never the cost you paid, and the correction, if anyone makes one, lands in a period that had nothing to do with it.
What that calls for is not a better spreadsheet. It is a cost record with the same resolution as your quantity record: a dated cost per receipt of stock, drawn down in the order the units actually shipped, with the freight and duty landed inside it before the sale is priced.
That is what Landara does. It connects to a Shopify store, attaches a dated cost layer to every receipt of stock, draws those layers down oldest first as orders ship, and produces a ready-to-post Dr COGS / Cr Inventory journal for Xero or QuickBooks Online. Shopify stays in charge of quantity; Landara owns the cost side, and it never writes to your ledger by itself. A person reviews the entry and posts it.
The distinction the formula could not make is visible on the report. Sales COGS is one figure. Inventory adjustments, the losses and write-offs and corrections, are a separate figure with their own journal, deliberately kept out of cost of sales.

One more property worth naming, because it is the difference between a number and a record. A periodic figure can only be recomputed: you re-count, you re-add, and you hope you get the same answer. A cost ledger can be replayed. Every movement is still there, so what COGS is can be recalculated from the underlying events at any time, and compared against what was actually posted. Where they differ you raise a correcting journal rather than quietly rewriting a closed month. (What has to sit behind each line of that journal.)
And it is genuinely the wrong tool for some businesses. If you manufacture from raw materials, run several warehouses, or need demand forecasting, buy an inventory system. If you sell a handful of domestically bought products at a stable price, do one count a year and never see a freight invoice, the formula at the top of this page is all you need, and the rest of this article was about a problem you do not have.
Common questions
What is the formula for cost of goods sold?
Beginning inventory + purchases - ending inventory = cost of goods sold. Beginning inventory is what you held on day one, purchases is the landed cost of what arrived during the period, and ending inventory is what was still on hand at the close, valued using a costing method.
How do I calculate COGS without a stocktake?
You cannot, using this formula, because ending inventory is its only real input. The alternative is to stop using the formula and cost each sale as it happens, which is what a perpetual system does. Then the count becomes a check on the ledger rather than the thing the whole quarter depends on.
Does COGS include shipping?
Inbound, yes. Freight, duty and clearance on getting the stock to you are part of what the units cost, so they belong in purchases and flow into COGS as those units sell. Outbound shipping, what it costs to send the parcel to the customer, is a selling expense and does not belong in COGS.
Is cost of goods sold the same as cost of sales?
In a business that sells physical products they are used interchangeably, and both mean the direct cost of what was sold. Some businesses use cost of sales more broadly, to include direct delivery costs that never sat in inventory, so it is worth checking what a particular chart of accounts means by it before comparing two companies.
Why did my COGS change when nothing changed?
Under the periodic formula, three things move it and only one of them is sales: a different closing count, a different valuation of that count, or stock that left the business without being sold. All three arrive as a single number, which is why the question is so hard to answer after the fact.
Does Shopify calculate COGS for me?
Shopify tracks quantity precisely and reports on the cost per item you have typed into each product, which is a single field holding one number. That is enough for a rough margin report and not enough for a journal, because the units you sold this month may have arrived on two shipments that cost different amounts. (Valuing Shopify inventory for the balance sheet.)
What is a good COGS margin?
There is no useful universal figure, because it varies by category, channel and how much of your fulfilment cost sits above the gross profit line. The comparison worth making is your own, over time, on a cost basis that has not changed between the two periods you are comparing.
Stop Back-Solving Your Cost of Goods Sold
Connect your store and Landara costs every order against real FIFO layers, freight and duty included, then hands your bookkeeper a ready-to-post journal.
See how it works