Skip to main content
Bird & Bear
Menu
← All articles

BigQuery Working Examples: AI.KEY_DRIVERS

bigquery ai analytics sql
BigQuery Working Examples: AI.KEY_DRIVERS

We’ve already covered AI.KEY_DRIVERS reaching General Availability in BigQuery at the end of September. This piece is the practical follow-up: a worked example you can run yourself, against a dataset built to show both what the function is good at, and a real limitation worth knowing about before you trust its top row.

BigQuery’s AI.KEY_DRIVERS function takes a metric that changed and works out which data segments are statistically responsible for the change. You call it like any other table-valued function, directly in SQL. No model to train first, no separate tool to open.

The scenario

We built a synthetic e-commerce orders dataset that tells a specific story: a two-week tracking issue that quietly under-counts orders from Paid Social on mobile, then gets fixed. It’s the kind of problem this practice gets called in to find in real client data, just compressed into something you can load and query in an afternoon.

Setup instructions, the full column list, and the story the data tells are in our dataset setup guide. You’ll need the table loaded as bq_ai_demo.ecommerce_orders before running the query below.

The query

This compares revenue during the affected two weeks (2026-07-13 to 2026-07-26) against an equal-length window immediately before it, and asks AI.KEY_DRIVERS which combination of channel, device, region, and product category explains the difference.

WITH order_segments AS (
  SELECT
    channel,
    device_category,
    region,
    product_category,
    revenue,
    IF(order_date >= '2026-07-13', TRUE, FALSE) AS during_incident
  FROM `bq_ai_demo.ecommerce_orders`
  WHERE order_date BETWEEN '2026-06-29' AND '2026-07-26'
)
SELECT
  drivers,
  metric_interest,
  metric_reference,
  difference,
  relative_difference,
  unexpected_difference,
  contribution
FROM AI.KEY_DRIVERS(
  (SELECT * FROM order_segments),
  metric_col => 'revenue',
  interest_label_col => 'during_incident',
  dimension_cols => ['channel', 'device_category', 'region', 'product_category'],
  top_k => 10
)
ORDER BY contribution DESC;

A few points on the parameters. metric_col has to be summable. Revenue qualifies; a rate or ratio doesn’t, and needs a different approach. interest_label_col is a boolean column splitting your rows into the group you’re asking about (TRUE) and everything else (FALSE). dimension_cols is the list of columns the function is allowed to combine when looking for a segment. top_k caps how many ranked results come back. The ORDER BY contribution DESC is there to make the sort order explicit in the output — the function already ranks its results this way internally, but SQL doesn’t guarantee that order survives downstream without you asking for it.

What each column in the result means

  • drivers: The segment being described: either all (the whole comparison, with no dimension filter) or one or more dimension=value pairs.
  • metric_interest / metric_reference: The summed metric (revenue, here) for that segment, in the group you flagged as TRUE via interest_label_col versus the group left as FALSE. In our query that’s “during the incident” versus “the two weeks before it.”
  • difference: metric_interest minus metric_reference. The raw, unadjusted change in pounds.
  • relative_difference: difference divided by metric_reference. The percentage change, which is what actually tells you how severely a segment moved, independent of its size.
  • unexpected_difference: The part of this segment’s difference that isn’t already accounted for by its parent/overlapping segments. For a segment nested under another (like a two-dimension combination drilled from a single-dimension one), this is the bit of extra movement that combination adds beyond what the broader segment alone already explains.
  • contribution: In practice, this is ABS(difference), and it’s what BigQuery’s contribution analysis (the engine behind AI.KEY_DRIVERS) sorts its output by, largest first. It’s a measure of raw dollar impact, not of how unusual or severe a segment’s change was relative to its own size.

That last point matters more than it looks like it should, because of what actually happens when you run this.

What the result shows

Run the query above, and the top rows look like this (we ran it ourselves against this exact dataset to check):

Rankdriversdifferencerelative_difference
1all−3,743.17−8.58%
2region=United Kingdom−3,021.98−13.70%
3device_category=mobile, region=United Kingdom−2,610.07−25.57%
4device_category=desktop−2,090.84−11.21%
5device_category=mobile−1,511.62−7.77%
6product_category=Outerwear−1,378.19−13.02%
7channel=Paid Search−1,263.07−11.88%

AI.KEY_DRIVERS example query

Notice what’s missing: channel=Paid Social combined with device_category=mobile, the exact segment we planted the tracking break in, isn’t in the top 7. It isn’t even in the top 10 as a combined segment at all. channel=Paid Social on its own shows up further down the list (rank 8, around a −15% relative difference), but the specific Paid Social + mobile interaction never gets generated as a candidate segment in this run.

That’s not a mistake in the query or the data. We checked directly:

SELECT
  IF(order_date >= '2026-07-13', 'during_incident', 'reference_window') AS period,
  SUM(revenue) AS revenue
FROM `bq_ai_demo.ecommerce_orders`
WHERE order_date BETWEEN '2026-06-29' AND '2026-07-26'
  AND channel = 'Paid Social'
  AND device_category = 'mobile'
GROUP BY period;

AI.KEY_DRIVERS missing biggest relative change in the dataset

That returns roughly £1,474 during the incident versus £2,970 in the reference window, a 50% drop, by far the most severe relative change of any segment in the dataset. The planted anomaly is genuinely there. AI.KEY_DRIVERS just didn’t surface it, because of how it ranks and generates candidates.

Why the biggest row isn’t always the real story

contribution is just ABS(difference), sorted largest first. The United Kingdom is half of all traffic in this dataset by design, so over any given two-week window, ordinary sampling noise in a segment that big produces a larger raw-pound swing than a genuine, severe problem confined to one channel-and-device combination that’s a much smaller slice of total revenue. The function isn’t wrong to rank it that way, that is what “contribution” means, but it means the top row answers “which segment moved the most money,” not “which segment is actually broken.”

There’s a second limitation behind this specific result: AI.KEY_DRIVERS doesn’t exhaustively test every combination of every dimension you give it. It drills one level deeper from whichever single segment ranks top overall, here that’s region=United Kingdom, drilled by device, which is how device_category=mobile, region=United Kingdom made the list. It never tried combining channel with device_category directly, because channel didn’t win the first round.

In practice, this means you can’t just trust row one. Two things actually work:

  1. Look at relative_difference, not row position, for any segment that does show up. A segment with a modest contribution but a severe relative_difference is worth a second look even if it’s near the bottom of the list.
  2. Verify a suspected segment directly, the way we just did above. If you already have a hypothesis (a channel, a device, a region someone’s flagged), a two-line GROUP BY query answers it more reliably than scanning down a ranked list hoping the function tried that exact combination.

That second point is really the same discipline this whole series keeps coming back to. AI.KEY_DRIVERS is a genuinely fast way to screen a lot of candidate segments at once and a reasonable first pass when you have no hypothesis yet. It isn’t a substitute for checking the specific thing you suspect, and treating its top row as a verdict rather than a lead is exactly the kind of mistake “AI-ready” data practice is supposed to prevent.

All articles in this series

Newsletter

Occasional notes on data, tracking & AI-readiness

No spam, no growth-hacking. Just an email when there's something worth reading.