Preppin' Data 2020: Week 12 challenge solution
Here is my solution for Peppin’ Data 2020, Week 12. This challenge was about calculating weekly sales figures for products based on total sales numbers.
Original challenge and data set on Preppin’ Data’s blog.
Step 1: Filtering out products that didn’t sell
This challenge had 3 separate tables: ‘Total Sales’, ‘Percentage of Sales’, and a lookup product table. The ‘Total Sales’ table shows weekly sales for all products sold during a particular week, but doesn’t have a weekly breakdown of sales per product per size. This lower level of detail should be calculated using the percentages for each products and sizes sold during a particular week.
The original input for this challenge is one Excel workbook with 3 tabs for each table described above. Once we connect to this Excel workbook in Tableau Prep Builder, we see three separate Input steps in the canvas area.
Let’s start with the ‘Percentage of Sales’ table, and filter out all products that didn’t sell, i.e. the products that made 0% of total sales. We can do it directly in the Input step by clicking Filter values option in the data preview pane, and writing a simple calculation:
Step 2: Extracting size information from the lookup table
Next we need to extract the size details from the Product field in the other Input step, a lookup table, to be able to join it later with the ‘Percentage of Sales’ table. Product ID and the size details per product were erroneously concatenated. To extract these details we need to create a new calculated field Size:
To complete this step, we need to delete the size details from the Product field by creating a new calculated field called Product:
Step 3: Joining the ‘Percentage of Sales’ and lookup table
Now that we have common fields in both tables, we need to bring the Join step, and join the ‘Percentage of Sales’ and lookup table on the Product and Product ID fields, as well as on the Size fields. Remove the Product ID and now duplicated Size fields after the join.
Step 4: Creating Year Week Number field
As the final output requires the date to be in the Year Week Number format (for example, ‘202002’), we need to convert the existing Week Commencing field into that format. To do so, we need to find a week number from the Week Commencing field that is currently in the ‘dd/mm/yyyy’ format. There are two ways to go about it, depending on the Tableau Prep Builder’s version you are using.
If you are using the 2020.1.4 version or later, then you can simply click on the three-dot icon in the Week Commencing field’s header, and select Convert Dates > Week Number. That will bring the number of the week as an integer.
If you are using an earlier version, you can get the same result by writing a calculated field as follows:
Now we need to change the data type for the field with the week number (in my case it’s the Week Commencing field) to String, and create a new calculated field called Year Week Number.
As the length of the requited Year Week Number format should be 6 digits, we need to add a zero in front of any week number that is one digit, for example, 2. The calculated field checks whether a week number consists of one digit using the LEN function, and if the condition returns true, appends a zero to the year.
We can remove the Week Commencing and Product fields here as we won’t need them in further calculations.
Step 5: Cleaning the Scent field in the ‘Total Sales’ table
Before we can join the ‘Percentage of Sales’ and ‘Total Sales’ tables, we need to clean the Scent field in the ‘Total Sales’ table as now it has random spaces. To fix this, we need to click on the three-dot icon in the Scent field’s header, and select Clean > Remove all spaces. The only scent which actually has two words in its name is Tea Tree, so we need to create a calculated field Scent to update the existing Scent field:
Remember to change the data type of the Year Week Number field to String as it will be used in the Join step.
Step 6: Joining the ‘Percentage of Sales’ and ‘Total Sales’ tables
Now we can bring a new Join step, and join the ‘Percentage of Sales’ and ‘Total Sales’ tables on the Scent and Year Week Number fields.
We can remove the now duplicated Scent and Year Week Number fields before moving on to the final step.
Step 7: Calculating weekly sales per product per size
To conclude this challenge, we need to add a new Cleaning step and create a calculated field called Sales:
The ROUND function here rounds the final number to two decimal points.
We need to remove the Total Scent Sales and Percentage of Sales fields, and our final output is ready:
And here is the completed flow for this challenge which you can download from my GitHub repository: