Our Certified Tableau Trainers are asked a myriad of questions each day. In this series, they share some answers. 

Here’s a question that came out of a class from one of our other trainers:

“Student wants a running sum to restart every time the value drops to zero.”

How very interesting – a table calculation with a conditional scope. Let’s tackle this problem.

Scope & Direction

First, let’s do a quick review of scope and direction. Scope is how you partition the values in your chart for inclusion in the table calculation. Scope has three different values: table, pane and cell. Pane is only available when there are nested dimensions. In the case of multiple layers of nested dimensions, the scope of pane applies only to the last nested dimension.

In addition to scope, we also have direction. Direction is how the partition is addressed. Do we go down the scope or across the scope? Or in the case of undefined, do we consider all values in the scope. There are three basic types of direction: across, down and undefined. In certain situations, these directions can work together to zig-zag across the scope. This might be down and then across or across and then down.

There are advanced options with table calculations, such as choosing specific dimensions instead of scope and direction.

Running Sum

Let’s put this into practice with a simple table calculation. Let’s make up a simple data source in Excel to have our sales per day and then add a running sum to sales. Like so:

Simple data source

We are adding the daily sales values all the way down the table or in other words our table calculation is table (down).

Reset at Zero

Let’s modify our example and assume that we want to reset our calculation every time the daily sales hits zero. This is the running sum on days with consecutive sales. If it helps, it’d be the same as totaling the goals that Lionel Messi scored over his consecutive match scoring streak for instance.

Let’s solve it. I cannot use the running sum table calculation that Tableau would apply by default. If I drag that field over to the data pane and check out the new calculated field, I’d see this calculation:

Running Sum of Sales calculation

While I could build an IF/THEN clause to check for zero values, it wouldn’t reset the RUNNING_SUM function.

So instead, let’s build a custom table calculation in a calculated field:

Reset at Zero

The key here is resetting the running sum each time the IF/THEN logical operator encounters a zero. If it doesn’t, then it uses the PREVIOUS_VALUE function to carry forward the running sum to the current value. Let’s add this to our crosstab to see how it looks compared to the standard running sum table calculation:

New crosstab

Technically speaking, our scope and direction is still exactly the same. Within the logic of the calculated field, that’s where we apply our conditional usage of the running sum.

Great question! Let us know if you have any other ideas on how you can apply your table calculation conditionally in the comments section below.

More "Questions from Tableau Training"