Home

Data eng · Analytics

NYC Taxi Analytics with dbt and Snowflake

GitHub ↗Live docs and lineage ↗

git pushor pull requestGitHub Actionsfresh Ubuntu runner · 1 month of TLC datadbt build38 nodes · 31 tests · red if one failsbuilds and tests everything below, in lineage order--target DuckDB (default, CI) · Snowflaketested againstraw.yellow_tripsParquet · 41.2M rowsraw.taxi_zonesCSV · 265 zonesstg_yellow_tripsviewstg_taxi_zonesviewsnap_taxi_zonessnapshot · SCD Type 2int_trips_enrichedephemeraldim_zonestablepayment_typesseedfct_tripsincrementalagg_daily_zone_revenuetable
Every push runs the whole graph in CI, from raw files on the left to marts on the right. The same models build on DuckDB or Snowflake; dbt works out the order from the references between models, and runs each model's tests before building anything downstream.

The problem

Moving data is half the job; the other half is modelling it so the numbers can be trusted. Raw trip records are full of values that look wrong, and every one forces a decision: delete it, fix it or keep it and say why. Get those decisions wrong and every report built on top quietly inherits the mistake. This project takes 41M New York taxi trips, all of 2024, and models them in dbt, with each decision tested, documented and visible in a lineage graph. The same models run on DuckDB on my laptop and on Snowflake.

How I approached it

  1. 01

    Sources. Declared twelve monthly TLC trip Parquet files and the official taxi zone lookup as dbt sources. The source file name is kept on every row, for lineage and for the incremental load, and columns are matched by name so a later file with an extra column still combines cleanly.

  2. 02

    Staging. One view per source that only renames to snake_case and casts types. No joins and no business logic, so staging stores no second copy of the raw data.

  3. 03

    Profile. Built and profiled the project on the first three months (9.5M trips) before scaling up. Queried the data for every pattern that looked wrong: negative fares, null passenger counts, trips dated 2002, dropoffs before pickups. Each one ended in a documented decision to keep, flag or remove.

  4. 04

    Intermediate. An ephemeral model joins every trip to the zone lookup twice, for pickup and dropoff, derives duration, speed and tip percentage through a safe_divide Jinja macro, and adds data-quality flags. It removes only what is physically impossible; with deduplication, that is 1,628 rows across the year.

  5. 05

    Marts. A star schema: a trip-level fact with a surrogate key (TLC data has no trip ID) and deduplication, a zone dimension, and a daily revenue-by-borough table. Revenue totals keep refunds so they net out; averages use only trips that pass every flag.

  6. 06

    Incremental. Made the 41M-row fact table incremental, so each run loads only new TLC file months instead of rebuilding the whole year, and re-running never duplicates rows. Scaling from three months to twelve meant dropping nine files in; no model SQL changed.

  7. 07

    History. Added a payment-type seed from the TLC data dictionary, an SCD Type 2 snapshot of the zone lookup that keeps every past version of a zone, and a source freshness check that stops a run when raw data goes stale.

  8. 08

    Snowflake. Ported the project to Snowflake as a second dbt target. Raw files go into an internal stage and load with COPY INTO, which records each row's file name and load time. dbt connects as a key-pair service user with a least-privilege role. DuckDB-only SQL was swapped for dbt's cross-database macros and two adapter.dispatch macros of my own, so there is still one version of every model.

  9. 09

    Test and ship. 31 data tests across every layer, from keys and accepted values to a check that the revenue table reconciles to the fact table, all passing on the full year. GitHub Actions runs dbt build on a fresh machine against real TLC data on every push, and the docs site with column descriptions and the lineage graph is published on GitHub Pages.

The hard part

Choosing the incremental watermark. The obvious filter, load trips picked up after the latest one already in the table, loses data for good: some trips that start minutes before midnight on the last day of a month are published in the next month's file, so by the time they arrive the watermark has already passed them. Filtering on the source file's month instead loads each file whole, late trips included. When I added April to December on top of the first quarter, the incremental table matched a full rebuild row for row: 41,168,092 trips either way.

Results

  • Negative fares were refunds, not errors

    731,006 rows carried negative fares, −$16.7M across 2024. In the first quarter, 74% of them exactly mirrored an earlier positive charge, so they are reversals. Filtering fares to zero or above, as my Spark project did, keeps the charge and drops the refund, overstating 2024 revenue by about $16.7M. Here they are kept and flagged.

  • A failing test became a business finding

    Payment type 0 failed an accepted-values test. It turned out to be the TLC's code for flex-fare trips: 4.1M trips and $101M in fares in 2024, and the only trips that never record a passenger count. The test now expects a passenger count only when the trip is not flex fare.

  • One codebase, identical results on two warehouses

    The same check query (row counts, revenue and every flag count) matched exactly on DuckDB and Snowflake. The 41M-row fact table builds in about 65 seconds on an X-Small Snowflake warehouse, against 13 minutes on my laptop. Not a fair benchmark, since the laptop was spilling to disk, but it shows where the work belongs at this size.

    Full build of the 41M-row fact table
    DuckDB, laptop805 s
    Snowflake, X-Small65 s
  • Adding a month is about 4× faster than a rebuild

    On the first quarter, loading a new month incrementally took 11.4 seconds against 47 for a full rebuild. On the full year a rebuild takes about 13 minutes, so the gap only grows.

    Time to add the March file
    Full rebuild47.0 s
    Incremental11.4 s
  • Rules written on three months held for the year

    96.1% of trips pass every quality flag and feed the averages, the same share on the full year as on the first quarter, with no rule changes. Everything that carries revenue still counts toward totals.

Future improvements

Next is using Snowflake's real load timestamp for the freshness check instead of pickup time, then 2025 and 2026 data in Snowflake, including the new congestion fee column and a mart comparing trips before and after New York's congestion pricing. Smaller steps: slim CI that builds only changed models, a lookback window to pick up corrected months, and point-in-time joins from trips to the zone history.