2024: Week 12 - Solution



Solution by Tom Prowse and you can download the workflow here



Step 1 - Course Dates

First we need to calculate when the course starts and ends then build out a table with each of the months in-between.

To do this we need to create the course end date to add on the course length from the start date: 

Course End

DATE(DATEADD('year',[Course Length (years)]-1,[Course Start]))


We can then use the Start and End dates to create a new row for each of the months in-between using the New Rows step. 



Then finally we can calculate the total amount per year for each of the loan types by using an aggregation step where we group by Loan Type, Amount per Year, and Year: 



At this stage our table will look like this: 



Step 2 - Loan Dates

Now we want to calculate the dates for when the loan has been received. First we can create a date based on the year of study and the 1st September: 

Loan Received
MAKEDATE([Year],9,1)

Then we want to calculate the number of months since the course has started and April 2024. We can use a datediff calculation for this: 

Months Since Course Start 
DATEDIFF('month',[Loan Received],MAKEDATE(2024,4,1))

Then we want to include the type of loan (Undergraduate or Postgraduate). This isn't included in our initial tables, but it is included in the table name in our input. To include the table name in our workflow we can add a Union step with only one input and this will create the Table Names field for us.

Once we have the table name we can parse out the type of loan from this string. We can do this by splitting anything after the last '/' using a custom split: 



This will give us the loan type so we just need to remove the word 'loan' from each of the strings which we can do with another split but this time it's on a ' ' (space) and the first occurrence: 




Alternatively you can use a Replace calculation here as well. 

Once we have the clean Loan Type string we can rename and remove the fields so that our table looks like this: 



Step 3 - Combine Repayments

Next we want to combine our current workflow with the repayments table. First we need to calculate the Interest on a monthly basis so we divide by 12: 

Monthly Interest
[Interest]/12

Then we can calculate the % Repayment over Threshold as a decimal by dividing by 100: 

% Repayment over Threshold 
[% Repayment over Threshold]/100


Once we have calculated these we can join to our original workflow using an inner join on the 'Loan' field: 



Step 4 - Amount Owed

Finally we can calculate the repayments that each person owes. First we can calculate the Amount + Interest using the calculation from the requirements: 

Amount + Interest 
[Amount per year]*power(1+[Monthly Interest],[Months since course start])


After this we can calculate the totals by using an aggregation tool where we group by Monthly Interest, Repayment Threshold, & % Repayment over Threshold, and calculate the Sum of Amount per Year and Amount + Interest: 


After renaming Amount per Year to Total Borrowed and Amount + Interest to Total Borrowed + Interest we can then calculate the repayments using the salary thresholds. 

First we need to create a parameter so that the user can input their salary. This needs to be a whole number with a list of a 30, 35, & 40 




Then using this parameter we can calculate the earnings over threshold: 

Earnings above threshold 
([Parameters.Salary (£k)]*1000)-[Repayment Threshold]


We can then use this calculated field to calculate the Monthly Repayment: 

Monthly Repayment 
ROUND(([Earnings above threshold]*[% Repayment over Threshold])/12,2)

Then we can use this to calculate the Total Borrowed + Interest 

Total Borrowed + Interest 
ROUND([Total Borrowed + Interest]-[Monthly Repayment],2)


Then finally the Interest to be added next month: 

Interest to be added next month 
ROUND([Total Borrowed + Interest]*[Monthly Interest],2)

We can then remove the additional fields that we no longer need and our output should look like this (when 30 is selected in the parameter): 



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 & @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

2023: Week 2 - International Bank Account Numbers

How to...Handle Free Text