DEV Community

Cover image for The migration found the old dashboard had been counting everything twice
Vijay Kumar
Vijay Kumar

Posted on Originally published at datafanalytics.com

The migration found the old dashboard had been counting everything twice

The first two posts in this series, about a delete+insert that wiped out three months of data and a merge that never deleted anything, were bugs in our own pipeline. This one went the other way. In this article I'll show you how reconciling a new dbt build against an old dashboard proved the old dashboard wrong, how we confirmed it without touching the legacy system, and the cheap checks that would have caught it on day one.

Some background

I'm an analytics engineer at a leading North American housewares company. Part of our migration is a set of market-share reports that ran on an old on-prem BI tool. The data comes from a syndicated retail-data provider as CSV exports, and the reports are rebuilt in Snowflake and dbt.

For every report we follow the same routine:

  1. Read the old load script, because it is the real specification.
  2. Profile every input file: rows, columns, date range, blanks, duplicates.
  3. Rebuild the logic in dbt.
  4. Reconcile the new numbers against the live legacy dashboard.

The first three reports tied out to the dollar and the unit for every category.

How it came up

These reports came to us after the colleague who looked after them left the organization, so part of the job was understanding reports nobody on the team had built. Reading the load script for one of them, I couldn't find any reason for something it did: it loaded two different weekly files, a "part 1" and a "part 2", and stacked them on top of each other. Each one was about 812,000 rows and 12 columns, a rolling 52 weeks of data, and nothing in the code explained why both were needed.

My first guess was wrong

That same report didn't tie to the new build at all. My first guess was that my new dbt build was wrong. That's usually the right first guess in a migration, since the old report is the established one and the new model is brand new.

So I went back to those two files. When I profiled them side by side, they were identical. Same rows, same values, same weeks. The only difference between the two files was the title line at the top. The provider had delivered the same 52 weeks twice under two different names, and the load script added both into the dashboard.

Two 'different' files, one dashboard, every number doubled

Confirming it without touching the old system

I didn't want to change anything in the legacy app to prove the point, so I queried the open app read-only for a single fiscal period and compared it with the file. The app's total divided by the file's total came out at exactly 2.000.

So the new dbt build was right, and for two weeks the dashboard had been showing twice the real figure.

A second problem in the same app

While I was in there, the same app had another issue. Several measures were filtered on a "total market" benchmark, but the load script dropped those benchmark rows before they ever reached the app. Those measures had nothing left to show, so they displayed 0 or a dash. Nobody had flagged either problem.

A gotcha in the provider's data

One more thing worth knowing if you work with syndicated retail data. You might expect the provider's "total market" figure to equal "rest of market" plus "this retailer". At a high level it does. At item level it doesn't, because the provider suppresses small values separately for each benchmark. So you can't rebuild one benchmark from the other two, and if a report needs the total, you have to keep the total rows.

What we did

When I raised the double count, the business owner asked me to diagnose the cause and come back with a solution that had tests behind it.

The new model takes one copy of the weekly data, so the doubling doesn't carry over into Snowflake, and we ran regression tests on it before signing it off. And for each legacy report, "reconciled" now means three things together: the input files profiled, the load script read, and the numbers matched. Matching the numbers alone would have meant copying the bug.

The checks that would have caught it

None of this needed anything clever. These are the checks I'd put in front of any pair of files that are supposed to be different.

Compare a hash of each file. In Snowflake, HASH_AGG gives one hash for a whole table. If two files that should differ come back with the same row count and the same hash, they're the same data.

select 'part_1' as file_name, count(*) as row_count, hash_agg(*) as content_hash
from raw.weekly_part_1
union all
select 'part_2', count(*), hash_agg(*)
from raw.weekly_part_2;
Enter fullscreen mode Exit fullscreen mode

Row-diff them. If the hash matches and you want to see it row by row, EXCEPT shows what's in one file and not the other. Zero rows both ways means the files are identical.

select count(*) as rows_only_in_part_1
from (
    select * from raw.weekly_part_1
    except
    select * from raw.weekly_part_2
);
Enter fullscreen mode Exit fullscreen mode

Test that the weeks don't overlap. If part 1 and part 2 are meant to cover different weeks, a singular dbt test can fail the build the day they don't:

-- tests/assert_weekly_parts_do_not_overlap.sql
select p1.week_ending
from {{ ref('stg_weekly_part_1') }} as p1
inner join {{ ref('stg_weekly_part_2') }} as p2
    on p1.week_ending = p2.week_ending
group by 1
Enter fullscreen mode Exit fullscreen mode

Any row returned is a week that appears in both files, and the test fails.

To sum up

Reconciling against a legacy report isn't only about proving the new pipeline right. Sometimes the old report is the one that's wrong, even if nobody has noticed yet. Profile the inputs as well as the outputs, read the old load script as part of the spec, and add cheap checks for files that are supposed to be different.

My own view is that a migration should fix legacy bugs right away, not reproduce them first. If you copy a bug into the new system to make the numbers match, you've just moved it somewhere newer.

This is the third post in my series about failures in the data stack where everything is green and the numbers are still wrong.

If your team is looking to implement AI solutions, especially on top of data you need to be able to trust, you can book a free 15-minute AI Quick Audit or message me on LinkedIn.

Top comments (0)