2023: Week 10 - Solution

 

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


Building on our challenge from last week, we want to take this a step further and allow users to find out their balance, even on days that they don't make a transaction. 

Step 1 - Balance Each Day

First, we need to calculate the order in which the transactions were made and the total value for each of the days.

To calculate the total transaction value we can use a LOD where we group by Account Number and Balance Date and then Sum the Transaction Value. 


From here we can then order the transactions using a Rank calculation where we group by Account Number and then Rank the Balance Date Ascending and Transaction Value Descending. 


From here, we then only want to take the single transaction from each day so we can identify the latest transaction (max Order) from each account number and balance date combination.


Using this max order, we can filter where Max Order = Order, so that we only have a single, latest transaction for each day. 

After tidying some fields, we want to rename the Total Transaction Value to Transaction Value and the table should look like this: 


Step 2 - Next Transaction

Next we need to identify the 'gap' in days between transactions so that we can scaffold the missing days. 

For this we want to calculate the transaction order for each Account Number using a Rank calculation: 


We then want to bring the next transaction date onto the same row as the current transaction so that we can use the dates to scaffold the data later in the workflow.

To do this want want to use a self-join, so need to create a separate clean step where we remove the Transaction Value and Balance fields. We can then -1 from the Transaction Order field, so that it aligns with the order of the previous transaction. 

Finally, we want to -1 from each of the days to create an upper bound for each of the dates. We do this as we don't want to include the next transaction date within the scaffold because there's already data for this within the table.

Balance Date Upper Bound 
DATE(DATEADD('day',-1,[Balance Date]))

After the calculation, we can remove the Balance Date and the new clean step table should look like this: 


We can now create the self-join to align the current and previous orders using a Left-Inner Join on Account Number and Transaction Order.


Our new table now looks like this:


The last action we need to take at this stage is to ensure that are no null dates within the Balance Date Upper Bound field. If there are nulls we want to replace them with the latest date in the upper bound field. 

Therefore we first need to identify the latest date using a LOD: 


Then use an IF statement to replace the nulls: 

Balance Date Upper Bound 
IF ISNULL([Balance Date Upper Bound])
THEN [Max Balance Date]
ELSE [Balance Date Upper Bound]
END

Our table should now look something like this: 


Step 3 - Scaffold Table

Now we have prepared the table we are now ready to scaffold the missing dates. We can utilise the New Rows step within Tableau Prep to do this easily. 

Within the New Rows step we want to add rows in the range where Balance Date <= Balance Date Upper Bound where we're adding a single day. We also need to make sure that the balance is carried over from the previous row. 

As a result we should start to generate some rows that look like this: 


Then finally we need to ensure that the transaction value is null on the days that no transaction was made. 

Transaction Value 
IF [Balance Date]=[Date] THEN [Transaction Value]
ELSE NULL
END

When we remove the unneeded fields our table should now look like this: 


Step 4 - Date Selection Parameter

The final task for this week is to create a date parameter so that the user can select a date to show the balances on this day.

First, we need to duplicate the Date field and then convert it to a string. We can then create a parameter that is a string with a list of dates that you want the user to select. Unfortunately, there is no option to auto-fill these dates based on a field like you can in Tableau Desktop and no Date option so that's why we're using a string!


From here we can use this parameter within a filter where [Parameters.Select a date]=[Date-1]

Then remove both date fields and add the parameter into the Output so that the table name is 'Balance as of <Select a Date>'. This will change the name of the output based on the parameter selected by the user: 


Our final table should look like this where 2023-02-01 is selected: 


You can download all the output from 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