2022: Week 32 - Solution

Solution by Tom Prowse and you can download the workflow here.


This week we looked at a classic problem for anyone who is lucky enough to own property... how much is left on a property mortgage? We want to do this for each of our stores so lets look at how we can solve the challenge.

Step 1 - Months to Repay

The first step is to calculate how many months we have remaining to repay the mortgage in full. This consists of a few calculated fields as we need a start date, how much is being paid, and then to calculate the number of months until the end date.

Today
MAKEDATE(2022,8,10)

This creates a 'today' field based on 10th August 2022. In a real life situation we could just use the TODAY() function as this will update based on the actual date.

Monthly Capital Repayment
[Monthly Payment]*([% of Monthly Repayment going to Capital]/100)

This calculates how much capital is being paid off each month.

Months to Repay
CEILING([Capital Repayment Remaining]/[Monthly Capital Repayment])

This calculates how many months are remaining based on how much the monthly payments are. We have rounded this up using the ceiling function.

Then using the above calculations we can calculate the expected date that we will repay the mortgage in full. 

End Date
DATEADD('month',[Months to Repay]-1,[Today])

We need to make sure this is a date field as well.

At this stage our table should look like this:


Step 2 - Row for Each Month

Next we want to create a row for each month that the mortgage will run for (from today to end date). We can use the New Rows functionality within Tableau Prep to help with this. 

Within the setup of the New Rows, we want to add rows from a range of two fields, where Today <= End Date, each increment is 1 month, and the values should be copied from the previous row.


After creating the rows we can remove the additional fields that we now longer need so we should be left with a table that has Store, Monthly Capital Repayment, Capital Repayment Remaining, and Monthly Payment Date:


Step 3 - Amount Remaining

Now we have a row for each month with the monthly payment, we can use this to calculate how much is remaining for each of the stores and also overall.

The next step is to use a self-join so that we can do a running total based on each of the months. First we want to create a new clean step, then we can use this to join back to the step before using an inner join where Monthly Payment Date (from the new Clean Step) >= Monthly Payment Date (from the original field, we can rename this duplicate date so we don't get confused). 

Our workflow should look like this: 

And the join should look like this:


Notice how the number of rows has massively increased due to use join on all future months. This can be solved by using an aggregation to bring this back to a single row per month, and also calculating the running total. 

In the aggregation we want to group by Monthly Payment Date & Store, then Sum Monthly Capital Repayment and then Avg Capital Repayment Remainder. 


After the aggregation we should be back down the 613 rows and our table should look like this:


Step 4 - Remaining & Outstanding Capital

The final steps for this challenge is to calculate the Remaining Capital to Repay and the Capital Outstanding Total. We can do this by using the following calculations:

Remaining Capital to Repay
[Capital Repayment Remaining]-[Monthly Capital Repayment]

As our table is now set up in the correct way, we can just subtract one from the other.

Capital Outstanding Total


We used a fixed LOD here as we are working across both stores, so need to sum both months together whilst also maintaining the granularity of the table.

After removing the unneeded fields, we should now be ready to output our data that looks like this: 


You can download the full output here

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