How to Calculate Gross Margin by Product in Google Sheets
Revenue, cost, gross profit and margin percent for every product, sorted by the profit each one actually brings in.
The short answer: QUERY the revenue and cost per product, subtract, and divide: margin is =(Revenue - Cost) / Revenue. In the sample data, overall 2026 gross margin is 43.6%; Earl Grey Loose Leaf 1 lb has the best margin (60.7%) but House Blend 5 lb makes the most gross profit ($165,956).
The sample data
| A | B | C | D | E | F | G | H | I | J | K | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | Date | Order ID | Customer | Region | Channel | Product | Category | Qty | Unit price | Revenue | Cost |
| 2 | 2025-01-01 | SO-10001 | Tin Roof Café | Northeast | Sales rep | Sip Lids (case of 1,000) | Supplies | 6 | 54 | 324 | 198 |
| 3 | 2025-01-01 | SO-10001 | Tin Roof Café | Northeast | Sales rep | Espresso Roast 5 lb | Coffee | 10 | 68 | 680 | 370 |
| 4 | 2025-01-01 | SO-10002 | Foxglove Café | Northeast | Online store | Sip Lids (case of 1,000) | Supplies | 1 | 54 | 54 | 33 |
| 5 | 2025-01-01 | SO-10002 | Foxglove Café | Northeast | Online store | Cold Brew Concentrate 1 gal | Coffee | 8 | 42 | 336 | 152 |
| 6 | 2025-01-01 | SO-10003 | Ember Café | Midwest | Online store | Sip Lids (case of 1,000) | Supplies | 4 | 54 | 216 | 132 |
| … | 8,115 more rows | ||||||||||
Sample data: a tab named Orders with 8,120 order lines from January 2025 to October 2026, one row per order line. Revenue is Qty × Unit price; Cost is the line's cost of goods (Qty × unit cost). Download orders.csv and import it (File → Import) to follow along. The business is fictional.
The classic way: QUERY revenue and cost, then a margin column
Column F is Product, J is Revenue and K is Cost (the line's cost of goods). In A1 of a new tab:
=QUERY(Orders!A:K, "select F, sum(J), sum(K), sum(J)-sum(K) where year(A) = 2026 group by F order by sum(J)-sum(K) desc label sum(J) 'Revenue', sum(K) 'Cost', sum(J)-sum(K) 'Gross profit'", 1)
QUERY can do arithmetic on its own totals, so sum(J)-sum(K) is gross profit, and ordering by it puts the biggest earners first. Type Margin in E1 and, in E2 copied down:
=IFERROR(D2/B2, "")
Format E as a percent. For the Total row, sum B, C and D, and divide the totals for the margin (=D17/B17).
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Product | Revenue | Cost | Gross profit | Margin |
| 2 | House Blend 5 lb | $367,474 | $201,518 | $165,956 | 45.2% |
| 3 | Espresso Roast 5 lb | $323,544 | $176,046 | $147,498 | 45.6% |
| 4 | 12 oz Hot Cups (case of 1,000) | $124,896 | $79,361 | $45,535 | 36.5% |
| 5 | Cold Brew Concentrate 1 gal | $66,444 | $30,058 | $36,386 | 54.8% |
| 6 | Ethiopia Single Origin 5 lb | $74,928 | $43,708 | $31,220 | 41.7% |
| 7 | Chai Concentrate 32 oz (case of 6) | $56,948 | $29,712 | $27,236 | 47.8% |
| 8 | Oat Milk 32 oz (case of 12) | $86,112 | $64,032 | $22,080 | 25.6% |
| 9 | Sip Lids (case of 1,000) | $51,408 | $31,416 | $19,992 | 38.9% |
| 10 | Earl Grey Loose Leaf 1 lb | $30,660 | $12,045 | $18,615 | 60.7% |
| 11 | Vanilla Syrup 750 ml (6-pack) | $39,792 | $22,383 | $17,409 | 43.8% |
| 12 | Decaf Blend 5 lb | $38,272 | $22,724 | $15,548 | 40.6% |
| 13 | Green Sencha 1 lb | $18,208 | $7,966 | $10,242 | 56.3% |
| 14 | Burr Grinder | $10,384 | $7,280 | $3,104 | 29.9% |
| 15 | Pour-Over Kit | $5,251 | $2,419 | $2,832 | 53.9% |
| 16 | Milk Pitcher Set | $1,938 | $855 | $1,083 | 55.9% |
| 17 | Total | $1,296,259 | $731,523 | $564,736 | 43.6% |
Computed from the sample CSV above. Cells formatted as currency and percent.
Don't average the margins. On the sample data, =AVERAGE(E2:E16) says 45.1%. The real margin is 43.6%: total gross profit over total revenue. Small, high-margin products like the Milk Pitcher Set count as much as House Blend in a plain average.
Gotchas
- Cost per line, not per unit. The Cost column has to be quantity × unit cost. If your sheet only has unit cost, add a column
=H2*UnitCostfirst, orsum(K)adds up unit prices. - Missing costs. A product with no cost shows a 100% margin. Check for blanks in the cost column before trusting the top of the list.
- Gross margin is before overheads. Shipping, payment fees and discounts are usually not in cost of goods. Operating expenses sit on top.
The one-cell way: =QUIRIZ
With the Quiriz add-on for Google Sheets, you type the question instead of building the formula. =QUIRIZ does not read the cells around it: it answers from the datasets in your Quiriz account. For this example, upload orders.csv as a dataset, or connect your Shopify store. You can also add a Google Sheet itself as a dataset that refreshes on a schedule. Then, in any empty cell:
=QUIRIZ("gross profit and gross margin by product in 2026, sorted by gross profit, with a total row", "table")
The "table" mode spills the answer down and to the right from that cell, header row first:
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Product | Revenue | Cost | Gross profit | Margin |
| 2 | House Blend 5 lb | $367,474 | $201,518 | $165,956 | 45.2% |
| 3 | Espresso Roast 5 lb | $323,544 | $176,046 | $147,498 | 45.6% |
| 4 | 12 oz Hot Cups (case of 1,000) | $124,896 | $79,361 | $45,535 | 36.5% |
| 5 | Cold Brew Concentrate 1 gal | $66,444 | $30,058 | $36,386 | 54.8% |
| 6 | Ethiopia Single Origin 5 lb | $74,928 | $43,708 | $31,220 | 41.7% |
| 7 | Chai Concentrate 32 oz (case of 6) | $56,948 | $29,712 | $27,236 | 47.8% |
| 8 | Oat Milk 32 oz (case of 12) | $86,112 | $64,032 | $22,080 | 25.6% |
| 9 | Sip Lids (case of 1,000) | $51,408 | $31,416 | $19,992 | 38.9% |
| 10 | Earl Grey Loose Leaf 1 lb | $30,660 | $12,045 | $18,615 | 60.7% |
| 11 | Vanilla Syrup 750 ml (6-pack) | $39,792 | $22,383 | $17,409 | 43.8% |
| 12 | Decaf Blend 5 lb | $38,272 | $22,724 | $15,548 | 40.6% |
| 13 | Green Sencha 1 lb | $18,208 | $7,966 | $10,242 | 56.3% |
| 14 | Burr Grinder | $10,384 | $7,280 | $3,104 | 29.9% |
| 15 | Pour-Over Kit | $5,251 | $2,419 | $2,832 | 53.9% |
| 16 | Milk Pitcher Set | $1,938 | $855 | $1,083 | 55.9% |
| 17 | Total | $1,296,259 | $731,523 | $564,736 | 43.6% |
Same figures as the formula result above, from the same sample data. The spilled cells are plain numbers, so you can format them, total them or chart them; here they are formatted as currency and percent. Column headings are written with each answer and may be worded differently.
The other modes: "verbose" (the default) answers in a sentence, "bullets" writes two to four points one per cell, and "notes" adds how the figure was calculated. Install Quiriz for Google Sheets and try the question on your own data.
Ask the question, get the table
Quiriz answers business questions in a Google Sheets cell, from your imported files, scheduled Google Sheets and connected tools. Free to start.
Install for Google Sheets →Free on the Google Workspace Marketplace. Or try it on sample data first, no signup.