2024: Week 33 - Solution


Solution by Tom Prowse and you can download the workflow here


Step 1 - Student's Birthday

We first want to determine when each student's birthday for this school year. For this we can use the input from the 2022 Week 1 challenge and calculate this year's birthday: 

This Year's Birthday 
IF MONTH([Date of Birth]) >=9 
THEN MAKEDATE(2024,MONTH([Date of Birth]),DAY([Date of Birth]))
ELSE MAKEDATE(2025,MONTH([Date of Birth]),DAY([Date of Birth]))
END

We can then calculate their 'cake day' by duplicating this field and converting it to 'day of the week' using the in-built Tableau Prep features 



After renaming this field we can then calculate the cake day: 

Cake Day
IF [Birthday Day of Week] = 'Saturday' 
THEN DATE(DATEADD('day',-1,[This Year's Birthday]))

ELSEIF [Birthday Day of Week] = 'Sunday' 
THEN DATE(DATEADD('day',-2,[This Year's Birthday]))

ELSE [This Year's Birthday]
END

At this stage our table should look like this: 



Step 2 - Last School Day

Now we want to use the School Dates input to calculate the last school day for each pupil. First we want to make sure that the Starts & Ends fields are Date fields. We can then rename the Source Row Number to Term Order and create a flag for if they are in school or not. 

For this flag we want to identify each of the different terms and exclude any holidays. We can calculate this using a Regex calculation to identify if there are numbers in the string: 

In School? 
REGEXP_MATCH([Term],'\d')

We can then use this field to determine the last day in school: 

Last Day in School 
IF [In School?] = False THEN NULL 
ELSE [Ends]
END

Next we want to fill in the null values by using a Fill Down function to repeat the last day in school: 

Last Day in School Filled


At this stage our table look like this: 



Step 3 - Count Cakes

We can now start to count how many cakes are needed in each school day. 

First we need to combine the two workstreams by using an inner join where Cake Day >= Starts and Cake Day <= Ends. 

Then we can calculate if a cake is needed on each date: 

Cake Needed on Date 
IF [In School?] THEN [Cake Day]
ELSE [Last Day in School Filled]
END

We can then duplicate this field and convert it to day of the week so we have a Cake Weekday field. 

After this we can then aggregate our table where we group by Cake Needed on Date and Cake Weekday then we can Count Distinct on id to find how many cakes are needed in total: 



Once we rename the ID field we are then ready to output the data: 



You can view the output here.

After you finish the challenge make sure to fill in the participation tracker, then share your solution on Twitter or LinkedIn using #PreppinData and tagging @Datajedininja@JennyMartinDS14 & @TomProwse1

You can also post your solution on the Tableau Forum where we have a Preppin' Data community page. Post your solutions and ask questions if you need any help! 

Popular posts from this blog

2023: Week 1 The Data Source Bank

2024: Week 1 - Prep Air's Flow Card

How to...Handle Free Text