How to Calculate Moving Sum in Tableau but Not for All Prior Months

Tableau provides various options to calculate table level data with different levels of aggregation and analysis. Calculations are available for a variety of scenarios like Difference From, Percent Difference From, Percent From, Percent of Total, Rank, Percentile, Running Total, Moving Calculations. Each of them with their configurable parameters provides the user unprecedented amounts of flexibility. But even with such robustness there can be cases where we want to create a view with our own customization which is not supported right away.

One such business case if often when we want to apply all the above functions on our data but want to exclude or include certain data values per our criterion. Say we have a scenario where we want to have a Moving Sum, but only want to include months 3, 6 and 9 in our view. This can be done by using a Calculated field like below. Here, we are calculating the Moving Sum of the Furniture Product Sub-Category only for the months 3, 6 and 9.

If you want to take this a step further and add more dynamic logic so that it automatically detects the prior 3rd, 6th and 9th month from the current month, it can be done like below. The below logic detects the prior months values based on a calculated field, and returns the result for the current month.