Home / Sheets questions cookbook / Gross margin by product
Google Sheets · Question cookbook

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.

By · Published November 8, 2026

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

ABCDEFGHIJK
1DateOrder IDCustomerRegionChannelProductCategoryQtyUnit priceRevenueCost
22025-01-01SO-10001Tin Roof CaféNortheastSales repSip Lids (case of 1,000)Supplies654324198
32025-01-01SO-10001Tin Roof CaféNortheastSales repEspresso Roast 5 lbCoffee1068680370
42025-01-01SO-10002Foxglove CaféNortheastOnline storeSip Lids (case of 1,000)Supplies1545433
52025-01-01SO-10002Foxglove CaféNortheastOnline storeCold Brew Concentrate 1 galCoffee842336152
62025-01-01SO-10003Ember CaféMidwestOnline storeSip Lids (case of 1,000)Supplies454216132
…8,115 more rows
Orders

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).

Example on sample data
fx=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)
ABCDE
1ProductRevenueCostGross profitMargin
2House Blend 5 lb$367,474$201,518$165,95645.2%
3Espresso Roast 5 lb$323,544$176,046$147,49845.6%
412 oz Hot Cups (case of 1,000)$124,896$79,361$45,53536.5%
5Cold Brew Concentrate 1 gal$66,444$30,058$36,38654.8%
6Ethiopia Single Origin 5 lb$74,928$43,708$31,22041.7%
7Chai Concentrate 32 oz (case of 6)$56,948$29,712$27,23647.8%
8Oat Milk 32 oz (case of 12)$86,112$64,032$22,08025.6%
9Sip Lids (case of 1,000)$51,408$31,416$19,99238.9%
10Earl Grey Loose Leaf 1 lb$30,660$12,045$18,61560.7%
11Vanilla Syrup 750 ml (6-pack)$39,792$22,383$17,40943.8%
12Decaf Blend 5 lb$38,272$22,724$15,54840.6%
13Green Sencha 1 lb$18,208$7,966$10,24256.3%
14Burr Grinder$10,384$7,280$3,10429.9%
15Pour-Over Kit$5,251$2,419$2,83253.9%
16Milk Pitcher Set$1,938$855$1,08355.9%
17Total$1,296,259$731,523$564,73643.6%
SummaryOrders

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

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:

Example on sample data
fx=QUIRIZ("gross profit and gross margin by product in 2026, sorted by gross profit, with a total row", "table")
ABCDE
1ProductRevenueCostGross profitMargin
2House Blend 5 lb$367,474$201,518$165,95645.2%
3Espresso Roast 5 lb$323,544$176,046$147,49845.6%
412 oz Hot Cups (case of 1,000)$124,896$79,361$45,53536.5%
5Cold Brew Concentrate 1 gal$66,444$30,058$36,38654.8%
6Ethiopia Single Origin 5 lb$74,928$43,708$31,22041.7%
7Chai Concentrate 32 oz (case of 6)$56,948$29,712$27,23647.8%
8Oat Milk 32 oz (case of 12)$86,112$64,032$22,08025.6%
9Sip Lids (case of 1,000)$51,408$31,416$19,99238.9%
10Earl Grey Loose Leaf 1 lb$30,660$12,045$18,61560.7%
11Vanilla Syrup 750 ml (6-pack)$39,792$22,383$17,40943.8%
12Decaf Blend 5 lb$38,272$22,724$15,54840.6%
13Green Sencha 1 lb$18,208$7,966$10,24256.3%
14Burr Grinder$10,384$7,280$3,10429.9%
15Pour-Over Kit$5,251$2,419$2,83253.9%
16Milk Pitcher Set$1,938$855$1,08355.9%
17Total$1,296,259$731,523$564,73643.6%
Sheet1

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.

Frequently asked questions

What is the formula for gross margin in Google Sheets?
Gross margin = (revenue - cost of goods) / revenue. With revenue in B2 and cost in C2: =(B2-C2)/B2, formatted as a percent.
What is the difference between gross profit and gross margin?
Gross profit is dollars: revenue minus cost of goods. Gross margin is that profit as a percent of revenue. A product can have a high margin and still bring in little profit if it sells little.
Why does my average margin not match my total margin?
A plain average gives every product equal weight. The true margin weights each product by its revenue: total gross profit divided by total revenue.