14 Replies Latest reply on Aug 31, 2018 9:13 AM by Michael Tralli

# Calculation for previous month

I'm trying to create a calculation so that when you filter for a specific month, the viz will show two columns, sales for the selected month and sales for the previous month. Thank you.

• ###### 1. Re: Calculation for previous month

Hi Michael,

Depending on your data structure, you can do this with a parameter or with a dual axis by duplicating your data source. Here is an example by creating a date parameter called date:

Set this to true on your filters and then use the parameters to control the month.

Andrew

• ###### 2. Re: Calculation for previous month

I need to create something like this in the Viz where the prior month updates automatically when changing the current month. I've only been using Tableau for month so I'm not confident in creating parameters yet. Is there another way?

• ###### 3. Re: Calculation for previous month

Hi Michael,

Do you have a workbook that you could attach by chance?

You can also do this with action filters which works pretty well. I will work on getting an example put together and attach it today.

Andrew

• ###### 4. Re: Calculation for previous month

My data is coming from a remote server, not sure I have access to manipulate it. My assignment is to mimic an excel sales report like the last image I showed but in Tableau. Thank you, anything helps.

• ###### 5. Re: Calculation for previous month

Give this one a try, it is on the SuperStore data set.

I built a tab called All Months which contains all the order months, and Current tab and a previous tab. The actions are assigned to the All Months so that when you select a month (blue dot) it will add the filter to the Current Month and add another filter to the previous month tab. If this is what you are looking for I can walk you through the steps a little more clear.

Andrew

• ###### 6. Re: Calculation for previous month

That is actually exactly what I need!

• ###### 7. Re: Calculation for previous month

Great!

Here is how you would build this dashboard.

1. Create a new calculated field called 'Previous Month' - The DATETRUNC function will give you the month and year, the DATEADD function is adding 1 month to the order date. This may seem counter intuitive to add a month instead of subtract a month but since we are using this as an action filter, the All Months tab will cast the Month and Year and we will need to compare it to the next month. Example: February 2018 is selected on the All Months, it is added as a filter to for February 2018 (with the 1 month added in) and will give us the data for January 2018.

Caculation:

2. Add the Current, Previous, and All Months Sheet to your dashboard

3. Create the 2 Action Filters.

For the Current Month Filter, Your Source Sheet (what triggers to action) will be the All Months, Target Sheet will be Current Month, Filter will be Month(Order Date) = Month(Order Date)

For the Previous Month Filter, Your Source Sheet will be the All Months, Target Sheet will be Previous Month, Filter will be Month(Order Date) = Month(Previous Month)

Let me know if you need anything else!

Andrew

• ###### 8. Re: Calculation for previous month

After I've created the action, how do I apply it to the worksheets or how do I put the action into filters?

• ###### 9. Re: Calculation for previous month

Hi Michael,

After you have added the sheets to your dashboard and added the actions, they should be active.

If they are not working, make sure you have the "Run Actions on: Selected" in your action filters, and also make sure you used the Filter action instead of the URL or Highlight action. Also, verify that the target filter is mapped correctly.

• ###### 10. Re: Calculation for previous month

Yea I'm not sure. I've done exactly all of that and nothing happens. I noticed in your sample file the action was placed in filters but for me I don't see the created action anywhere.

• ###### 11. Re: Calculation for previous month

Can you take a screen shot of your actions page since you can't attach a workbook?

Andrew

• ###### 13. Re: Calculation for previous month

Hi Michael,

The only thing that looks 'odd' on that is the source of the top one is referencing 'bookerrds.cdk76...." is that the name of your sheet? Also, do you have the sheets all on the Dashboard 1 (Months)?

Andrew

• ###### 14. Re: Calculation for previous month

That source is from a mysql file.