In Parts 1 and 2, Derek Lein and I created conceptual and technical sketches of our proposed data model for Chin & Beard Suds Co. (CBSC), keeping their business question at the center of our modelling decisions:
“Could we have seen the downfall (closure) of our two stores in London coming based on our transactions and cost?”
With our planning and design complete, it's time to put our work into practice! In Part 3, we'll use SQL in Snowflake to clean and transform our data before bringing everything together in our final Tableau data model.
Cleaning and Transforming Our Data
With our ERD as a guide, we took on the SQL portion of our project in Snowflake, transforming our original tables into the four tables we identified in Part 2.
Stores Table
Since Store ID is the common link between our tables, we needed to make sure that it was consistent throughout our data model.
- Used
CAST()to convert Store ID into a string, as it serves as an identifier rather than a numerical measure. - Used
COALESCE()to replace null Store IDs with 0. - Used
DISTINCTto remove duplicate combinations of Store ID and Store Location.
Our goal was to create a reference table containing one record per store that we could use to connect our three fact tables.
Fixed Costs Table
This table required minimal transformations. We selected only the fields relevant to our analysis, standardized the data type of Store ID, and renamed the Date field to Year to reflect our intended reporting period.
This allows us to look at the different expenses associated with each store over time without bringing in unnecessary fields.
Employee Salaries Table
Our Employee Pay History table contained information on how much employees were paid, but it did not contain information on which stores they worked at. To bridge this gap, we performed a left join with the Employee Department History table using Employee ID.
In addition to the join, we also:
- Standardized the data types of Store ID and Employee ID.
- Replaced null Store IDs with 0.
- Converted Effective Date from a string into a proper date field.
- Renamed Pay Rate to Annual Pay.
This gives us a way to associate employee compensation records with store locations.
Transactions Table
This was arguably the most complex transformation of our project. While our Sales table contained transactional data, the Production Cost table contained the cost of producing each product and its recommended retail price (RRP).
As we discussed in Part 2, understanding granularity and cardinality is essential when joining tables. Simply joining these two tables on Product ID could cause our data to explode, creating multiple records for a single sales transaction.
To prepare our data for the join, we used Common Table Expressions (CTEs) to break down the transformation into smaller, more manageable steps.
1. Correcting our dates
The day and month values in the Production Cost table were switched. Using DATE_FROM_PARTS(), Derek corrected these dates so that we could accurately match production costs to our transactions.
2. Establishing date ranges
Using the LEAD() function, we identified when the next production cost record became effective for each product. From there, we then established a start and end date for each cost and price.
For the most recent record of each product, the latest transaction date was used as the end date.
3. Creating a calendar scaffold
We generated a calendar containing consecutive dates to use as a scaffold for our Production Cost table.
4. Scaffolding our Production Cost table
By joining our calendar to the established date ranges, we were able to fill in the gaps between production cost records. Essentially, each product now had a corresponding cost and retail price for each day within its established date range.
5. Joining our Sales and Production Cost tables
With our scaffolded data, we could finally perform a left join using both Product ID and Order Date. This allowed us to retain our sales transactions while matching them to the applicable production cost and retail price on the date of the sale.
By preparing our data before joining, we're able to preserve the granularity of our Sales table and avoid unintentionally duplicating transactions and inflating our numbers.
Bringing Everything Together in Tableau
With our four tables created in Snowflake, we can now move into Tableau and establish the relationships we outlined in our ERD.

Our final Tableau data model is fairly simple. One of our project requirements was to create a handover that any new member of CBSC’s data team could pick up and run with.
We wanted to make sure our model was easy to understand, reuse, and build upon as the business needs change. A data model doesn't need to be complex to be effective.
Final Thoughts
We spent a good portion of our time planning before we even wrote a line of SQL. Having our SunBeam model and ERD to refer back to gave us a clear direction when it came to cleaning, transforming, and connecting our data.
With our final data model, CBSC’s team can now explore historical sales and costs, compare the performance of stores that closed against those that remained open, and hopefully identify early warning signs that can inform future business decisions.
Data modelling is as much about understanding the business and its people as it is about understanding the data.
A good data model reflects the needs of the people it was built for.

