2021: Week 32 - Solution

 

Solution by Tom Prowse and you can download the workflow here


This week we continued with the Excel theme and focussed on the SUMIF function within Tableau Prep. 

Step 1 - Flight Details

After inputting the data the first task is to form the correct flight name. This is a combination of the Departure and Destination fields so we need to bring these together within a string calculation: 

Flight
[Departure]+ ' to '+[Destination]

We can then remove the departure and destination fields and then calculate the number of days between the flight and the sales date. 

Days until flight
DATEDIFF('day',[Date],[Date of Flight])

Our table looks like this:



Step 2 - Calculate Sales Based on Conditions

Within this step we first need to classify if the sales were less or more than 7 days until the scheduled flight date. To do this we want to use the IF function to write some IF statements.

First is the Less than 7 days: 

Sales less than 7 days until the flight
IF [Days until flight] <7 THEN [Ticket Sales] END

Next is more than or equal to 7 days:

Sales 7 days or more until the flight
IF [Days until flight] >= 7 THEN [Ticket Sales] END

Our table now has the ticket sales value or null based on when the ticket was purchased: 


The next step we need to complete is take each of the individual rows and then aggregate these so we get the total sales for each flight and class. We want to calculate both the Sum and the Avg, therefore we need to duplicate both of our less than 7 and more than 7 sales fields (so we can use them twice in the aggregate tool). 

The aggregate tool is setup so that we group by Flight and Class, and then bring back the Sum & Avg for both of the less than 7 and more than 7 days fields: 


After the aggregate our table should look like this:



The final step is to round our Avg totals to 0 decimals using the round function: 

Avg. daily sales less than 7 days until the flight
ROUND([Sales less than 7 days until the flight-1],0)

Avg. daily sales 7 days or more until the flight
ROUND([Sales 7 days or more until the flight-1],0)

After removing the unnecessary fields we are ready to output our data that should look like this:



The full output can be found 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

2024: Week 1 - Prep Air's Flow Card

2023: Week 1 The Data Source Bank

How to...Handle Free Text