← All articles
Feature engineering
Data engineering
Data leakage

Point-in-time correct features: what 'as of' really means

· 7 minute read · Pivot-al

The usual way to build a first training table is to take a list of outcomes and join it to the customer table. The query runs and returns one row per example, and the model trains. The table is also wrong, because the customer table describes customers as they are today and the outcomes happened months ago.

A feature is point-in-time correct when its value in a training row is what the system would have computed at the moment of that prediction, which is often different from what the warehouse says now. This article covers what that requires from your data, and what the joins look like.

What "as of" means

Each training row has a prediction time: the moment the model would have been asked for a score. A feature is "as of" that time if it uses only information that existed and was available to you then.

Two questions follow for every feature:

  • Had the underlying events happened before the prediction time?
  • Had your systems recorded them before the prediction time?

The first is about event time and the second is about ingestion time. Most teams think about event time and forget ingestion time.

Get either wrong and you have temporal leakage, which is the standard reason a model scores well offline and fails live. The symptoms and detection checks are in why a 0.94 AUC model goes near-random in production.

Why overwriting tables destroys training history

Operational databases are built to answer "what is true now". When a customer upgrades, an UPDATE replaces plan = 'starter' with plan = 'growth'. The old value is gone, and if that table is replicated to the warehouse nightly with a full refresh, the warehouse loses it too.

For an application this is correct, but it destroys what training data needs. You want to know what plan the customer was on last February, when the prediction would have been made, and the answer no longer exists anywhere.

What makes this worse is that the outcome you are predicting often triggers the overwrite. A churned account gets its status set to cancelled, its seats set to zero, its owner reassigned. Join to that table and every churned example carries values that were written by its own churn.

History that was never kept cannot be recovered. The earliest date your honest training data can begin is the day you start keeping it.

Three ways to keep history

Snapshots

Copy the whole table on a schedule, usually daily, and stamp each copy with the snapshot date.

  • Simple to build and easy to reason about.
  • Storage grows with table size times number of days, though partitioned columnar storage makes this tolerable for most dimension tables.
  • Resolution is limited to the schedule. Changes between snapshots are invisible.
  • You only have history from the day the snapshots began.

To use a snapshot as of a prediction time, take the latest snapshot completed before that time.

Event logs

Record every change as an immutable, append-only event: plan_changed, seat_added, ticket_opened. This can come from product instrumentation or from change data capture on the operational database.

  • This is the most complete form, since any past state can be rebuilt by replaying events up to a time.
  • Aggregates over arbitrary windows, such as logins in the 28 days before prediction, come naturally.
  • It depends on the event stream being trustworthy: stable schemas, no silent drops, sensible handling of duplicates and late events.

Slowly changing dimensions

A type 2 slowly changing dimension keeps one row per version of an entity, with valid_from and valid_to columns. A customer who changed plan twice has three rows.

  • Compact, because rows are only added when something changes.
  • Simple to join as of a time, as shown below.
  • Usually derived from snapshots or change capture. If it is derived from daily comparisons, it inherits daily resolution, and valid_from is when the change was detected, not when it happened. For leakage purposes that is the safe direction of error.

The three can be used together. A common arrangement is an event log for behavior, a type 2 dimension for entity attributes, and snapshots for anything awkward.

Two clocks: event time and ingestion time

Every record should carry both.

  • Event time: when the thing happened in the world.
  • Ingestion time: when the record landed in your platform.

They differ more than people expect. Mobile clients buffer events offline and third-party sources deliver in batches. A failed pipeline is rerun three days later, or a vendor restates last month's numbers.

If training uses event time only, a late event appears in a feature window that, on the day, would have been missing it. The model trains on a more complete picture than serving will ever have. Filtering on ingestion time as well reproduces what was available on the day.

Ingestion time also lets you measure your real data delay. If the 95th percentile lag between event and ingestion is 30 hours, then a feature called "activity in the last 24 hours" is mostly empty at serving time, whatever it looks like in a backfilled training set.

The wrong join

-- Wrong: every example gets today's customer state
SELECT
  l.customer_id,
  l.prediction_time,
  l.churned_within_60d,
  c.plan,
  c.seat_count,
  c.account_status,
  c.lifetime_logins
FROM churn_labels l
JOIN customers c
  ON c.customer_id = l.customer_id;

Nothing in this query refers to time except the label. plan and seat_count are current values. account_status is the outcome under another name. lifetime_logins includes every login after the prediction, and for churned customers, the lack of them.

The as-of join

For entity attributes, join to the version that was valid at prediction time:

SELECT
  l.customer_id,
  l.prediction_time,
  l.churned_within_60d,
  h.plan,
  h.seat_count
FROM churn_labels l
LEFT JOIN customer_history h
  ON h.customer_id = l.customer_id
 AND h.valid_from <= l.prediction_time
 AND (h.valid_to > l.prediction_time OR h.valid_to IS NULL);

For behavioral aggregates, bound the window on both sides and filter on both clocks:

SELECT
  l.customer_id,
  l.prediction_time,
  COUNT(e.event_id) AS logins_28d
FROM churn_labels l
LEFT JOIN events e
  ON e.customer_id = l.customer_id
 AND e.event_type = 'login'
 AND e.event_time >= l.prediction_time - INTERVAL '28 days'
 AND e.event_time <  l.prediction_time
 AND e.ingested_at <  l.prediction_time
GROUP BY l.customer_id, l.prediction_time;

For "latest value before" lookups on irregular data, rank and keep the most recent row:

SELECT customer_id, prediction_time, health_score
FROM (
  SELECT
    l.customer_id,
    l.prediction_time,
    s.health_score,
    ROW_NUMBER() OVER (
      PARTITION BY l.customer_id, l.prediction_time
      ORDER BY s.event_time DESC
    ) AS rn
  FROM churn_labels l
  LEFT JOIN health_scores s
    ON s.customer_id = l.customer_id
   AND s.event_time < l.prediction_time
) ranked
WHERE rn = 1;

Some engines, including DuckDB, ClickHouse, and Snowflake, offer a native ASOF JOIN that does this more efficiently, and pandas has merge_asof. The logic is the same: for each left row, the latest right row at or before a given time.

Three details that cause trouble:

  • Strict versus inclusive bounds. Decide whether an event at exactly the prediction time counts, and apply the same rule everywhere. Strictly before is the safer default.
  • Time zones. A date compared with a timestamp hides an implicit midnight in some time zone. Store timestamps in UTC and be explicit.
  • Staleness. An as-of join will happily return a value from two years ago. Add a maximum age, or expose the age of the value as its own feature.

What a point-in-time feature layer needs

Getting this right takes four things. A feature store is one way of packaging them, and you can have all four without buying one.

  • Event time on every record. Without a timestamp there is no point-in-time correctness. For mutable entities, that means history in one of the three forms above.
  • Ingestion time on every record. Set by the platform on arrival, never by the source, and never rewritten by a backfill.
  • As-of joins as the only way to build training sets. Ideally a shared function or macro, so that nobody hand-writes the time predicate and gets it subtly wrong.
  • A dated label table. One row per entity and prediction time, with the outcome and the window it was measured over. It is the left side of every join above. If it does not exist, build it first. See your label is a product decision nobody made.

One more property is worth designing for: the same feature definition should produce both the training value and the serving value. If training features come from SQL in the warehouse and serving features from separate application code, the two will drift apart. Logging the features used at serving time, and comparing them against the offline computation for the same entity and time, is the most direct test of whether your point-in-time logic is right.

Temporal integrity is one dimension of data readiness. The others are in data readiness for machine learning.

Check your own data

If you already have a training table, the leakage check will flag suspected leakage, label, and drift problems from a CSV sample, and the data never leaves your browser. If you would like to talk through how your warehouse keeps history, or does not, the teardown clinic is a free 30-minute live session with a senior engineer. The Data & Model Feasibility Teardown covers the feature layer as part of a full assessment.