I was working with a client recently and they wanted to create a calculation in Tableau allowing them to compare performance this month to date versus the same time period last month.
Let’s say we are comparing sales from this month to the previous month. We’ll start by creating a calculation to return sales for just this month:
We need to return sales anywhere where the year and month are the same as the month of today. The line DAY([Order Date]) <= DAY(TODAY()) might not be necessary, but it is a safeguard in case data for future dates finds its way into the dataset.
Next we’ll create a calculation to return sales for the same time period last month:
Notice the use of the DATEDIFF() function. This necessary because if you ask Tableau to return sales for the previous month of the same year, this calculation won’t work in January when there is no previous month for the same year. The DATEDIFF() function calculates the total difference in months between the date field and today.
To test that my calculation is working properly, I often pull the fields into a text table to ensure data is only being returned where I want it to be:
Next we can create a calculated field to compare month to date sales versus previous month to date sales:
After applying a %-style number formatting to the previous calculation, we can examine how sales are doing this month compared to the same time period last month:
Need more help? Please contact us at firstname.lastname@example.org.