guide
guide8 min read

Null spike in a key column how to investigate: a six-step worked example

A runbook with SQL for finding the first bad load, the upstream change behind it, and a test that catches the next one

To investigate a null spike in a key column, find the first load where the null rate changed, then check whether that load changed at the source, in a transform, or in a join. Do it in that order, because the first bad load is the only timestamp that points to a cause. The steps below use SQL you can adapt to any warehouse, and a worked example (an illustrative one we built for this post, not a customer incident).

Key Takeaways

  • •A null rate that jumps on one date has a first bad load. Finding it turns "something is wrong with customer_id" into a specific run, commit, or source change.
  • •Compare null rates at each layer (raw, staging, mart). The layer where the rate first differs from the layer before it is where the cause lives.
  • •Casts that return null on failure (TRY_CAST and similar) are a common way a source format change becomes a null spike without any error.
  • •dbt's not_null test fails on a single null. If you want a threshold instead, configure severity with warn_if and error_if.
  • •Check for empty strings and placeholder values as well as true nulls, since a cast or a source change can introduce them.

Null spike in a key column how to investigate: set up the example

Here is the illustrative scenario. A mart table analytics.orders has customer_id as a join key. A dashboard owner reports that revenue by customer segment dropped on Tuesday, and a quick look shows that many orders have no customer. Upstream, raw.orders loads from an application database, stg_orders casts and renames columns, and analytics.orders joins to dim_customers. Every step below applies to that shape, and the same approach works for any key column such as user_id, account_id, or sku.

One framing before the steps: a spike that is a few percent and one that is 40 percent usually have different causes. Small, gradual rises tend to be a source quality problem. Sharp step changes tend to be a deploy, a schema change, or a join. Step 1 tells you which one you have.

Step 1: Bound the spike by day and by load

Start with the null rate per day in the affected table. Use row_count rather than rows, since rows is a reserved word in some warehouses.

select date_trunc('day', loaded_at) as load_day, count(*) as row_count, count(*) - count(customer_id) as null_count, round(100.0 * (count(*) - count(customer_id)) / count(*), 2) as null_pct from analytics.orders group by 1 order by 1 desc limit 14;

count(customer_id) counts only non-null values, so the difference from count(*) is the null count. It will not catch empty strings or placeholders like 'N/A' or '0', so run a second check: select sum(case when trim(customer_id) = '' then 1 else 0 end) as empty_strings from analytics.orders;. If your column is numeric, the same idea applies to sentinel values such as -1.

In our example the null rate is flat near zero through Monday and jumps to about 38 percent on Tuesday's load. A step change with a clean edge points toward a deploy, a schema change, or a join, not slow decay. Now narrow it from days to loads: repeat the query grouped by load_id or by hour to find the first load that crosses the line.

Step 2: Compare each layer on the first bad load

Run the same null count against raw, staging, and the mart, filtered to the first bad load. You are looking for the first layer where the rate differs from the layer before it.

select 'raw' as layer, count(*) as total, sum(case when customer_id is null then 1 else 0 end) as nulls from raw.orders where loaded_at >= '2026-11-24 00:00' union all select 'staging', count(*), sum(case when customer_id is null then 1 else 0 end) from staging.stg_orders where loaded_at >= '2026-11-24 00:00' union all select 'mart', count(*), sum(case when customer_id is null then 1 else 0 end) from analytics.orders where loaded_at >= '2026-11-24 00:00';

Result patternWhere the cause livesNext step
Nulls already high in rawThe source or the ingestion jobStep 3
Raw is clean, staging is highA cast, rename, or filter in the staging modelStep 4
Staging is clean, mart is highA join or a late-arriving dimensionStep 5

In the example, raw has about 0.1 percent nulls and staging has about 38 percent. The cause is between those two layers, so staging is where we look.

Step 3: If raw is already high, check the source change

When nulls arrive in raw, the question becomes what changed upstream. Check for a column rename (the old name now loads as null while a new one carries the value), a new nullable path in the application, or a change to the extraction query. Compare the column list in raw against yesterday's. A diff of information_schema.columns snapshots is enough, and so is a schema-change log if you keep one. Then ask the source team whether anything shipped on the first bad load date.

Also check whether the load is partial. A job that timed out partway through can leave rows with only some columns populated. Compare the row count for that load against the same weekday last week before assuming the source changed.

Step 4: If staging is the first bad layer, look for failing casts

This is the case in our example, and it is the one people miss. Many warehouses offer a cast that returns null instead of raising an error when the input does not parse, such as TRY_CAST in Snowflake and SQL Server (see the Snowflake TRY_CAST docs). That is useful for tolerant ingestion, but it also turns a source format change into silent nulls.

Suppose staging does try_cast(customer_id as integer) as customer_id, and Tuesday's deploy begins sending IDs like 'C-10492' for a new customer type. Find the values that fail to cast:

select customer_id as raw_value, count(*) as occurrences from raw.orders where try_cast(customer_id as integer) is null and customer_id is not null group by 1 order by 2 desc limit 20;

If the output is a list of values with a new prefix, you have your root cause: the source started sending a format the cast cannot handle. The fix is a decision for the owners of the key: widen the staging type to a string, map the new format, or reject the rows loudly instead of nulling them. Also check any case when and coalesce logic in the same model for the same pattern, and any new where filter that drops rows before a left join.

Step 5: If the mart is the first bad layer, check the join

When staging is clean and the mart is not, the nulls come from the join. A left join to a dimension produces nulls on the dimension side when keys do not match. Find orphans directly:

select o.order_id, o.customer_id from staging.stg_orders o left join staging.stg_customers c on o.customer_id = c.customer_id where c.customer_id is null limit 50;

Two causes are common here. One is a late-arriving dimension: the order loaded before its customer row did, so the match appears on the next run. The other is a key type or case mismatch introduced upstream, such as integer on one side and string on the other, or uppercase versus lowercase UUIDs. Check whether the orphans resolve on the next load. If they do, you have a timing problem, and the right fix is to order the loads or tolerate the lag explicitly.

Step 6: Add a test with a threshold you chose

Once you know the cause, encode it so the next spike is caught on the day it happens. In dbt, the not_null data test fails if any single row has a null. That is the right behavior for a primary key. For a column where a few nulls are tolerated, use the severity config with warn_if and error_if thresholds, such as warning above zero and erroring above a count you set from your own baseline. That way a small drift warns and a step change fails the run.

Rule-based frameworks such as Great Expectations can express the same idea as a fraction, for example an expectation that at least 99 percent of values are non-null. Whichever you use, set the threshold from the history you looked at in Step 1, not from a guess. Add a second test on the cast itself, such as an accepted-format check on the raw column, so a new ID format fails at the boundary where it appears.

Where agents fit in this runbook

Steps 1 through 5 are queries and comparisons that follow the same pattern every time, which makes them a good fit for an agent that can run them. We built the Incidents Agent to work this way: it can query the warehouse, compare layers, and look at lineage and logs through MCP tools. You can call it from Claude Code or Cursor, and the Claude Code MCP docs describe how any MCP server is connected. A question you might ask it is "Why did row counts drop 40% overnight?" or "Is the customer_id join safe after the migration?", and the answer should come back with the layer, the first bad load, and the query that shows it.

To show the shape, here is an illustrative sketch of an answer for our example scenario. It is written by hand to show the format we aim for, not recorded output from a real run: "customer_id null rate rose from 0.1% to 38% on the 2026-11-24 load. Raw is clean; the rise begins in staging.stg_orders. 94% of the new nulls come from values matching 'C-%' that fail try_cast(... as integer). First affected load: 2026-11-24 02:00 UTC." A human still decides whether to widen the type or reject the rows. The agent's job is to get you from the alert to that decision without an hour of query writing. Our Quality Agent covers the earlier step of watching null rates so the alert fires on the first bad load, and the two are designed to chain through MCP.

We covered how to run the swarm from your editor in our other posts, and the OSS edition is read-only by default, so these investigations do not change your warehouse. Pro adds writes and approval workflows for the fix itself.

Frequently Asked Questions

How do I tell a source problem from a transformation problem?

Count nulls in raw, staging, and the mart on the first bad load, as in Step 2. If raw is already high, the source or ingestion is responsible. If raw is clean and a later layer is high, the transformation or join is responsible.

Why did the null rate rise without any pipeline errors?

Because most null-producing operations do not raise errors. Failing casts that return null, left joins with unmatched keys, and filters applied before a join all succeed. This is why tests on the output column matter more than job success status.

Should a not_null test have a threshold?

For a primary or join key, no: any null is a defect, and dbt's default behavior of failing on a single null is correct. For a column where some nulls are expected, set warn_if and error_if so ordinary drift warns and a step change fails.

Can I run this investigation from Claude Code or Cursor?

Yes. Every Data Workers agent is an MCP server, so any MCP-compatible client can call it, as described in the MCP specification. You can also run every query in this post yourself from the editor against your own warehouse.