BigQuery SQL · Customer Segmentation

RFM Score Formula & BigQuery SQL Query for Ecommerce

Calculate Recency, Frequency and Monetary (RFM) customer segments directly in BigQuery using NTILE(5) quantiles on net revenue after returns. Ready-to-run SQL included.

Author
Alex Genovese
Updated
Updated

Executive Summary

In BigQuery, an ecommerce RFM score assigns each customer a 1–5 rank for Recency (days since last order), Frequency (completed order count) and Monetary value (net revenue after refunds) using NTILE(5). Combining R, F and M scores produces actionable Champions, Loyal, At-Risk and Hibernating segments.

Alex GenoveseHarvard Business School · Stanford ML (99.21%) · ex-Google Cloud Partner Architect
  1. 1–5 (NTILE)Quantile ScaleDynamic scoring calibrated to your store distribution
  2. Net RevenueRefund HygieneExcludes cancelled & returned orders from VIP tiers
  3. Scheduled SQLRuntimeRefreshes daily in BigQuery for pennies per run
BigQuery SQL — Net-Revenue RFM Scoring (NTILE 5)SQL
WITH base AS (
  SELECT
    customer_id,
    DATE_DIFF(CURRENT_DATE(), MAX(DATE(order_created_at)), DAY) AS recency_days,
    COUNT(DISTINCT order_id) AS frequency,
    SUM(net_revenue_after_returns) AS monetary
  FROM `project.ecommerce.orders`
  WHERE order_status = 'completed'
    AND DATE(order_created_at) >= DATE_SUB(CURRENT_DATE(), INTERVAL 365 DAY)
  GROUP BY customer_id
),
scored AS (
  SELECT
    customer_id,
    recency_days,
    frequency,
    monetary,
    NTILE(5) OVER (ORDER BY recency_days DESC) AS r_score,
    NTILE(5) OVER (ORDER BY frequency ASC) AS f_score,
    NTILE(5) OVER (ORDER BY monetary ASC) AS m_score
  FROM base
)
SELECT
  *,
  CONCAT(CAST(r_score AS STRING), CAST(f_score AS STRING), CAST(m_score AS STRING)) AS rfm_code,
  CASE
    WHEN r_score >= 4 AND f_score >= 4 AND m_score >= 4 THEN 'Champions'
    WHEN r_score >= 3 AND f_score >= 3 THEN 'Loyal'
    WHEN r_score <= 2 AND f_score >= 3 THEN 'At Risk VIP'
    WHEN r_score <= 2 AND f_score <= 2 THEN 'Hibernating'
    ELSE 'Potential Loyalist'
  END AS rfm_segment
FROM scored;

Production Architecture

How it ships in production.

  1. 01

    Filter to Completed Net Orders

    Always exclude refunded or cancelled orders—especially in fashion, where serial returners otherwise rank as top Monetary customers.

  2. 02

    Score with NTILE(5) Quantiles

    Order recency_days DESC so recent buyers earn 5, while frequency and monetary sort ASC so top buyers earn 5.

  3. 03

    Sync Segments to Klaviyo & Ads

    Export Champions as lookalike seeds in Meta/Google Ads and trigger automated win-back sequences for At Risk VIPs.

Retail & Enterprise Realities

  • Adjust the 365-day lookback window to 180 days for fast-moving beauty/supplements or 730 days for furniture and luxury.
  • Never discount Champions—reward them with early access and exclusive bundles instead of margin-destroying coupons.
Verified Evidence

See how RFM modeling works at enterprise scale

Full advisory architecture connecting BigQuery RFM, predictive CLV and lifecycle automation.

Read full evidence

FAQ

Frequently asked questions

Why use NTILE(5) instead of fixed euro thresholds for RFM?

Fixed thresholds break as AOV shifts with inflation or seasonality. NTILE(5) keeps segment proportions balanced automatically across cohorts.

Should RFM use gross sales or net revenue after returns?

Always net revenue after returns and discounts—otherwise high-return apparel shoppers pollute VIP lookalike audiences.

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