Your data stack

Data modelling: when it's worth doing, and what it fixes

Data modelling earns its place once you have a warehouse and people writing SQL against it. It cuts wasted compute and time spent rediscovering joins. You write the cleaning steps once, in dbt, in three layers: staging, intermediate and marts.

By Ansh Agrawal3 min readUpdated

Data modelling only makes sense once two things are true. You have a Data warehouseA separate database built for analysis rather than for running the product. It is where every source lands together, which is what makes it the number you trust when two tools disagree.Glossary, one central database where all your data lands, covered in the earlier chapters. And people are actually writing SQL against it, to do the analysis that Product analyticsThe analysis of what users do inside your product. The test is whether you can point at a row of data and name the user who did it.Glossary tools like Mixpanel cannot do.

That usually means you are already scaling. You have a lot of data, a lot of different tables, and a lot of different tools that each hold different information. When you do get there, data modelling is one of the most important things you will do.

What problem does data modelling solve?

The data sitting in your backend database, and the data coming in from third party tools, is messy.

Every time someone queries it to get something meaningful, two things go wrong.

  • You waste compute. Every question runs against the raw data from scratch, so you pay for more computing power in your warehouse.
  • You waste time. Someone has to figure out the joins, figure out which columns make sense, and deal with the same user ID being named differently in every table.

That is what data modelling fixes, and that is what dbt is for. dbt lets you write your cleaning steps once, in SQL, and rerun them automatically every time new data arrives.

You use dbt to clean your data in three layers, one on top of the other: staging, then intermediate, then marts. Each layer is built from the one below it, which is the structure dbt's own guide to structuring a project recommends. At the end, anyone on the team has clean data they can use.

What do the staging, intermediate and mart layers look like in practice?

Start with your messy tables. Naming is all over the place. The same user ID has a different name in every table, and half the columns nobody uses.

Staging: You take all of those tables and clean them up. Rename some things, remove the columns nobody needs. Every table is now a clean view, which is a saved query that looks and behaves like a table. That is your staging layer.

Intermediate: Here you join the meaningful tables together. Say there are three or four tables that all talk about the same feature. You want that in one table instead of joining four every time, so you do it here once.

If you had 100 tables in staging, the intermediate layer might come down to 40 tables that people actually use. Those 40 still carry all the context of the original 100. Nobody is joining anything by hand any more.

Raw data is 100 messy tables with the same user ID named differently everywhere; staging keeps 100 tables, renamed with unused columns removed and one clean view per table; intermediate joins related tables once into 40, so nobody joins by hand; marts are a handful of tables with one row per user or company and the key numbers added up.

These 40 are the only tables anyone queries when they need data. They are clean, and everything is there, instead of the 100 you had before.

Marts: There is one more layer on top. Marts are summary tables. Each one has one row per thing you care about, like a user or a company, with the key numbers already added up so you get quick answers.

Take a user mart. You can see everything about a user in one place. When did they sign up? When did they purchase? How many times have they purchased? How much have they spent? What features have they used?

Every user gets one row.

One row in a user mart with the columns user_id, signed_up, first_purchase, purchases, total_spent and features_used: user u_10482 signed up on 12 March, first purchased on 19 March, has made 4 purchases worth $240 in total and has used 3 features. Everything about a user is in one row, with no joins and quick answers.

If you are a B2B SaaS product you will have a company mart and probably a user mart. A health score can be a mart. If you have a feature and you want its data rolled up per user, in a format you use often for analytics, that becomes a mart too.

Marts are usually built on top of intermediate tables.

LayerWhat you do thereBuilt from
StagingRename things and remove the columns nobody needs, so every table becomes a clean viewYour raw tables
IntermediateJoin the tables that talk about the same thing once, so 100 staging tables might come down to 40Staging
MartsOne row per thing you care about, like a user or a company, with the key numbers already added upUsually intermediate

Who builds your data models?

The layers need someone who knows which questions the business keeps asking. That means an analytics engineer, whose job is turning raw data into clean, ready-to-use tables, or whoever already owns your warehouse.

What do you get out of data modelling?

Anyone can query any of your tables easily, because the data is clean and ready.

You can set up tests in dbt. These are automatic checks that run every time the data refreshes. If data goes stale and stops updating, or anything else goes off, dbt flags it for you, instead of you realising months later that something has gone wrong.

Data flows from raw sources (backend, analytics, ads) to staging clean views, to intermediate tables joined once, to marts with one row per thing, and on to BI and AI tools for dashboards and answers. A dbt test runs after staging, after intermediate and after marts, every time the data refreshes.

And the marts get you answers quickly. That turns out to be one of the most important parts of using your data with AI, which we will get to in the chapter Analytics in the age of AI.

Common questions

When should you start data modelling?

Once you have a data warehouse and people are actually writing SQL against it for analysis your product analytics tool can't do. Before those two things are true, modelling is work without a payoff.

What problem does data modelling solve?

Raw data from your backend and third-party tools is messy. Every question run against it from scratch wastes compute and forces someone to work out the joins again. Modelling does that work once.

What are staging, intermediate and mart tables?

They are the three layers you build in dbt, each from the one below. Staging cleans and renames the raw tables, intermediate joins the tables that belong together, and marts hold one row per user or company with the key numbers already added up.

Who should build your data models?

An analytics engineer, whose job is turning raw data into clean, ready-to-use tables, or whoever already owns your warehouse. They need to know which questions the business keeps asking.

What are dbt tests?

Automatic checks that run every time the data refreshes. If data goes stale or anything else goes off, dbt flags it, instead of you realising months later that something has gone wrong.