2020: Week 37 - Solution

 


Solution by Tom Prowse and you can download our solution here

This week we looked at the classic question, that can be a bit tricky in Tableau, about how to calculate working days. Working days traditionally exclude any weekends and bank holidays and are used in various business reports, especially with anything around productivity and scheduling. 

For this challenge we wanted you to take a custom start date and work out how many working days it has been since that date. In the original challenge post, Jenny had selected a start date as 1st April 2019, so we'll continue with that date in this solution post as it was the date I started at The Information Lab as well!

Step 1 - How Many Days?

The first step is to calculate how many days there have been since your selected start date. For this we will create a 'Today' field to use as our end date (this can be any date that you want to use). 

Today
Today()

Now we have a start and end date, we can calculate how many days are between the two:

How Many Days?
DATEDIFF('day',[Start Date],[Today])

We should now have the following three fields:


Step 2 - Fill in Dates

The next step is to create a row for each of the different dates between the start and end date. To do this we are going to use the scaffold table and join this to our original workflow. We use the following join conditions: 


This has now created a running sequence from the start to end date, and this can then be used to calculate how many days there have been. To transfer the scaffold into the actual date, we use the following calculation:

Days Between
DATE(DATEADD('day',[Scaffold],[Start Date]))

We now have a row for each of the dates between our start and end date: 


As with the definition of working days, we want to remove all of the weekends from our list of dates. To do this, we can calculate the weekday of each of the dates using this calculation: 

Weekday
DATENAME('weekday',[Day Between])

We can then select 'Saturday' and 'Sunday' from our Weekday list and exclude them from our table. 


We also want to remove any bank holidays from the list of dates. For this we will bring in the UK bank holidays table, and join to our original workflow. For this join we are completing an Outer Left Join where Day Between = UK Bank Holidays:


We use an Outer Left Join here as we want to include all of the fields from the left table (original workflow) but then want to exclude all of the values that match the right table (bank holidays). This join then acts as a filter and removes all of the bank holidays which we don't need!

The final task is to calculate how many days there have been once we have excluded both bank holidays and weekends, meaning we want to count how many rows we have left. 

Using an aggregate tool we can Group By Start Date & Today, then Count Distinct the Day Between field: 


We now have a single row of data - Start Date, End Date (Today), & Day Between (Working Days):


Step 3 - Remove Holidays

The final step this week is to remove any additional holiday days that we have taken off. We need to input the Holidays table, then use an aggregation to calculate the total days taken. Within the aggregation we don't want to group by anything, and want to Sum the Holidays field: 


After totalling, we want to join this onto our original workflow. As we currently don't have any common fields in which to join, we need to create a dummy join field. This can be anything, but in our case we have chosen a '1'. 

We need to create the join field on both the holidays branch and the original workflow, then use this within the join condition: 


After the join our table now looks like this: 


We can now subtract the number of holidays from our Day Between field and that gives us the total working days: 

Working Days
[Day Between]-[Holidays]

This is the final step, then we can clean the file and ready this for our Output: 


After you finish the challenge make sure to fill in the participation tracker, then share your solution on Twitter using #PreppinData and tagging @Datajedininja, @JennyMartinDS14, @JonathanAllenby & @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

2024: Week 1 - Prep Air's Flow Card

2023: Week 1 The Data Source Bank

How to...Handle Free Text