2024: Week 44 - Solution
Solution by Tom Prowse and you can download the workflow here.
Step 1 - Sold & Returned
First we want to tidy the dates a item was sold or returned so that the return date isn't before a sale date.
We can identify any issues by creating an issue flag:
Issue?
[Date Sold]>=[Date Returned]
Then using this to recalculate the Order or Return date:
Order Date
IF [Issue?]
THEN [Date Returned]
ELSE [Date Sold]
END
Return Date
IF [Issue?]
THEN [Date Sold]
ELSE [Date Returned]
END
We can then remove the Issue?, Date Sold, & Date Returned fields.
Finally, we can calculate how many days it took to return an item by using a DateDiff function:
Days to Return
DATEDIFF('day',[Order Date],[Return Date])
And also calculate what the value of the returned items are:
Return Value
IF [Days to Return]<=60
THEN [Price ]
ELSEIF [Days to Return]<=100
THEN round([Price ]*0.5,2)
ELSEIF [Days to Return]>100
THEN 0
END
At this stage our table looks like this:
Step 2 - Calculate Output Metrics
There are various different metrics that are required for the output, but we need to do some cleaning beforehand in order to calculate these.
First we can focus on the returned items and their value. Once we've created a new clean step we can use a filter to retain only the returned item (Exclude Nulls from the Return Date field). Then we can calculate the totals using an aggregate step.
Within the aggregate we want to group by Return Date, then Sum the Return Value and Number of Rows field, and also find the Avg of the Days to Return field:
We can then tidy up the fields by renaming Return Value to Total Returns Value, Number of Rows to Number of Returns, and rounding the Avg Days to Return to 2 decimal places (ROUND([Days to Return],2))
We can then calculate the revenue using a new branch before we filtered for the Returned values. This time we want an aggregate step where we group by Order Date and Sum the Price:
We can then round this value and rename the field to Revenue:
Revenue
ROUND([Price ],2)
Finally we can combine the Returns and Revenue branches by using a join where Return Date = Order Date and we return all of the rows from the orders Revenue branch:
After renaming and removing any additional fields we are ready to output our data:
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!