2024: Week 37 - Solution
Solution by Tom Prowse and you can download the workflow here.
Step 1 - Home & Away
The first step is to calculate how many points the home and away teams both scored using the following calculations:
Home Points
if [Home Score] > [Away Score] then 3
elseif [Home Score] = [Away Score] then 1
else 0
end
Away Points
if [Away Score] > [Home Score] then 3
elseif [Home Score] = [Away Score] then 1
else 0
end
From here we can then split the workflow into separate branches for the home and away teams. Within the Home branch we can remove the Away Points and Away Team fields, rename Home Score to Goals For, Away Score to Goals Against, Home Points to Points, and Home Team to Team.
We can then calculate the goal difference:
Goal Difference
[Goals For]-[Goals Against]
We can then repeat this same process on a separate branch but this time for the Away team.
After repeating the process we should have two branches which we can union together so we have a single table:
Step 2 - Last 5 Games
The challenge this week is looking at the recent results for each of the teams. For this we want to calculate how many points each team has got from the last 5 games (4 previous and current matchday).
We can calculate this by using a Lookup function which involves a partition calculation:
5 Games Ago
{partition [Team] : {orderby [Matchday] asc : LOOKUP([Points],-4)}}
4 games ago
{partition [Team] : {orderby [Matchday] asc : LOOKUP([Points],-3)}}
3 games ago
{partition [Team] : {orderby [Matchday] asc : LOOKUP([Points],-2)}}
2 games ago
{partition [Team] : {orderby [Matchday] asc : LOOKUP([Points],-1)}}
1 game ago
{partition [Team] : {orderby [Matchday] asc : LOOKUP([Points],0)}}
This will then give us 5 fields that contain the points from the last 5 games. From here we can transform the shape of the data so that each of the columns are in a single row using a Columns to Rows pivot:
Then within the pivot we can tidy up the Pivot Names field by removing any letters and renaming to Last X Games. We can also rename the 'ago' field to Previous Results then use a calculated field to return the result instead of the points:
Previous Results
if [Previous Results] = 3 then 'W'
elseif [Previous Results] = 1 then 'D'
elseif [Previous Results] = 0 then 'L'
else '-'
END
From here we can then pivot the data back the other way so that each of the last x games are the column headers so we can use a rows to columns pivot to bring the result back into columns.
The reason for the 2 pivots is so that we can remove all of the additional text and results so we don't need to repeat 5 separate times.
At this stage our table should look like this:
Step 3 - Running Totals & Position
Now we have the separate fields we can add the last 5 results together to get the form:
Last 5 Games
[5 ]+[4 ]+[3 ]+[2 ]+[1 ]
Then we can remove these fields along with the Date and Source Row Number.
We can then calculate the latest result by adding the Goals For and Goals Against
Latest Results
str([Goals For])+'-'+str([Goals Against])
Now we have all of this information displayed in fields we can then calculate the running totals (similar to last week).
Points (RT)
Goal Difference (RT)
After we have calculated the running totals, we can also calculate the Position using a Rank calculated field:
Position
Finally we can remove any fields that are no longer needed and we can then output the data:
You can view the output here.
After you finish the challenge make sure to fill in the participation tracker, then share your solution on Twitter or LinkedIn 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!