1. Date time formatting in Tableau
In this first demo, we will be focusing on the Citibike ridership statistics. Suppose we want to understand the ridership patterns over the week.
Looking at our dataset, we know that we have the start_time here, but this isn’t broken down into the hours or weekdays granularity. Let’s see how we can do this using Tableau’s drilldown and calculated fields.
Firstly, let’s navigate to our worksheet titled drilldown where we have a blank canvas.
Dragging in the [Start_Time] field to our rows, we can convert this to an hour simply by clicking the downward arrow, navigating to more, and choosing hour.
Similarly, we’ll again drag Start_Time, but to our columns where this time, we’ll choose weekday.
With our table viz set up, we’re now ready to calculate the number of rides occurring throughout the week.
Create a new calculated field called ridership_count.
We’ll drag in the RideID field and the count() function to give us what we need.
Dragging this to our table viz, we can see where the rides are concentrated, but it’s lacking some color. We’ll again drag the ridership_count to our color and choose the square option.
From this, it’s clear that 8AM and 5PM appear to be the most popular ridership times.
Now let’s recreate this but using calculated fields.
Create a calculated Field called hour grain where we’ll now use the DATEPART function.
DATEPART here accepts two key arguments; our date granularity which is ‘hour’, and the date time column. We can see it is a valid calculation.
Now DATEPART will return our data in an integer format which means Tableau will automatically summate this. We don’t want this as we want the hours represented in a string format.
Let’s demonstrate this here so you can see this first-hand.
See? This isn’t what we want. Now let’s remove the hour grain field from our canvas and modify this.
Modifying our hour grain field, we’ll add STR in front of the expression. We now have our hour in a textual format. This can be easily done by typecasting our calculated field we’ve called ‘Hour grain’.
Now let’s move to our weekday conversion for our columns.
This time, we want to use DATENAME().
Let’s create another calculated field called weekday_grain using the DATENAME() function.
Again, we’ll specify the weekday argument as the first argument, and the start_time as our second argument.
Now with our table viz ready, we’ll simply drag in ridership_count() field into our table, drag this again into our color, and we’ve recreated the previous visualization.
2. Let's practice!
Now it’s your turn!