BigQuery SQL · Shopify Plus

Shopify Cohort Retention Analysis in BigQuery SQL

Copy-paste BigQuery SQL query to build monthly customer retention and cumulative revenue cohorts from Shopify orders—ready for Looker Studio dashboards.

Author
Alex Genovese
Updated
Updated

Executive Summary

A Shopify cohort retention query in BigQuery groups customers by their first completed order month, calculates the month offset (0, 1, 2… 12) for every subsequent order, and outputs both active buyer retention percentage and cumulative net revenue per acquired customer.

Alex GenoveseHarvard Business School · Stanford ML (99.21%) · ex-Google Cloud Partner Architect
  1. M0 → M12OutputMonthly retention % and cumulative LTV per cohort
  2. Looker StudioBI ReadyPlugs straight into a pivot heatmap table
  3. Shopify OrdersData SourceWorks with Fivetran, Airbyte or custom webhook tables
BigQuery SQL — Shopify Monthly Retention & Cumulative LTV CohortSQL
WITH orders AS (
  SELECT
    customer_id,
    order_id,
    DATE_TRUNC(DATE(created_at), MONTH) AS order_month,
    (subtotal_price - total_discounts - IFNULL(refunded_amount, 0)) AS net_sales
  FROM `project.shopify.orders`
  WHERE financial_status IN ('paid', 'partially_refunded')
    AND cancelled_at IS NULL
),
cohorts AS (
  SELECT customer_id, MIN(order_month) AS cohort_month
  FROM orders
  GROUP BY customer_id
),
activity AS (
  SELECT
    c.cohort_month,
    DATE_DIFF(o.order_month, c.cohort_month, MONTH) AS month_index,
    o.customer_id,
    o.net_sales
  FROM cohorts c
  JOIN orders o USING (customer_id)
),
cohort_size AS (
  SELECT cohort_month, COUNT(DISTINCT customer_id) AS initial_buyers
  FROM cohorts
  GROUP BY cohort_month
)
SELECT
  a.cohort_month,
  a.month_index,
  s.initial_buyers,
  COUNT(DISTINCT a.customer_id) AS active_buyers,
  ROUND(100 * SAFE_DIVIDE(COUNT(DISTINCT a.customer_id), s.initial_buyers), 2) AS retention_pct,
  ROUND(SUM(a.net_sales) / s.initial_buyers, 2) AS avg_revenue_per_buyer
FROM activity a
JOIN cohort_size s USING (cohort_month)
WHERE a.month_index BETWEEN 0 AND 12
GROUP BY 1, 2, 3
ORDER BY 1 DESC, 2 ASC;

Production Architecture

How it ships in production.

  1. 01

    Normalize Cancelled & Refunded Orders

    Filter cancelled_at IS NULL and subtract refunded_amount so retention reflects real kept orders.

  2. 02

    Index Every Order by Month Offset

    DATE_DIFF(order_month, cohort_month, MONTH) creates clean M0–M12 columns for Looker Studio pivot tables.

  3. 03

    Slice by Acquisition Channel or Discount Code

    Add first_order_discount_code or utm_source to compare full-price vs promo buyer retention.

Retail & Enterprise Realities

  • Works cleanly across multi-currency Shopify Plus stores when net_sales is converted to shop base currency at order timestamp.
  • Replaces static Shopify admin reports with customizable margin-level cohort analytics.
Verified Evidence

Shopify Plus AI & BigQuery Analytics Architecture

See how full-stack Shopify + BigQuery pipelines power retention, RAG recommendations and profit bidding.

Read full evidence

FAQ

Frequently asked questions

How do I visualize this BigQuery cohort query in Looker Studio?

Connect the saved BigQuery view to a Looker Studio Pivot Table with cohort_month as rows, month_index as columns and retention_pct as the heatmap metric.

Data & Analytics

Need these numbers on your own data?

Book a call. We look at where your data lives, build the queries and dashboards, and agree on which metrics drive decisions.

Book a call