Extract Last Value as Metric from Table Calculation in Tableau?

885 Views Asked by At

I have raw data in Tableau that looks like:

Month,Total
2021-08,17
2021-09,34
2021-10,41
2021-11,26
2021-12,6

And by using the following calculation

RUNNING_SUM( 
COUNTD(IF [Inserted At]>=[Parameters].[Start Date]
       AND [Inserted At]<=[End Date]
THEN [Id] ELSE NULL END
))

/

LOOKUP(RUNNING_SUM( 
COUNTD(IF [Inserted At]>=[Parameters].[Start Date]
       AND [Inserted At]<=[End Date]
THEN [Id] ELSE NULL END
)),-1)*100-100

I get

Month,My_Calc
2021-08,NULL
2021-09,200
2021-10,80.4
2021-11,28.3
2021-12,5.1

And all I really want is 5.1 (last monthly value) as one big metric (% Month-Over-Month Growth).

How can I accomplish this?

I'm relatively new to Tableau and don't know how to use calculated fields in conjunction with the date groupings aspect to express I want to calculate month-over-month growth. I've tried the native year-over-year growth running total table calculation but that didn't end with the same result since I think my calculation method is different.

3

There are 3 best solutions below

0
Alex Blakemore On

First a brief table calc intro, and then the answer at the end.

Most calculations in Tableau are actually performed by the data source (e.g. database server), and the results are then returned to Tableau (i.e. the client) for presentation. This separation of responsibilities allows high performance, even when facing very large data sets.

By contrast, table calculations operate on the table of query results that were returned from the server. They are executed late in the order of operations pipeline. That is why table calcs operate on aggregated data -- i.e. you have to ask for WINDOW_SUM(SUM([Sales)) and not WINDOW_SUM([Sales])

Table calcs give you an opportunity to make final passes of calculations over the query results returned from the data source before presentation to the user. You can for instance calculate a running total or make the visualization layout dynamically depend in part on the contents of the query results. This flexibility comes at a cost, the calculation is only one part of defining a table calc. You also have to specify how to apply the calculation to the table of summary results, known as partitioning and addressing. The Tableau on-line help has a useful definition of partitioning and addressing.

Essentially, table calcs are applied to blocks of summary data at a time, aka vectors or windows. Partitioning is how you tell Tableau how you wish to break up the summary query results into windows for purposes of applying your table calc. Addressing is how you specify the order in which you wish to traverse those partitions. Addressing is important for some table calcs, such as RUNNING_SUM, and unimportant for others, such as WINDOW_SUM.

Besides understanding partitioning and addressing very well, it is also helpful to learn about the functions INDEX(), SIZE(), FIRST(), LAST(), WINDOW_SUM(), LOOKUP() and (eventually) PREVIOUS_VALUE() to really understand table calcs. If you really understand them, you'll be able to implement all of these functions using just two of them as the fundamental ones.

Finally, to partially address your question:

You can use the boolean formula LAST() = 0 to tell if you are at the last value of your partition. If you use that formula as a filter, you can hide all the other values. You'll have to get partitioning and addressing specified correctly. You would essentially be fetching a batch of data from your server, using it in calculations on the client side, but only displaying part of it. This can be a bit brittle depending on which fields are on which shelves, but it can work.

Normally, it is more efficient to use a calculation that can be performed server-side, such as LOD calc, if that allows you to avoid fetching data only for client side calculations. But if the data is already fetched for another purpose, or if the calculation requires table calc features, such as the ability to depend on the order of the values, then table calcs are a good tool.

2
Wade Schuette On

However you do it, the % month-to-month change from 2021.11 (a value of 26) to the value for 2021.12 (a value of 6) is not 5.1%.

It's (( 6 - 26 ) / 26) * 100 = -76.9 %

0
Wade Schuette On

OK, starting from scratch, this works for me: ( I don't know how to get exactly the table format I want without using ShowMe and Flip, but it works. Anyone else? )

  1. drag Date to rows, change it to combined Month(Date)
  2. drag sales to column shelf
  3. in showme select TEXT-TABLES
  4. flip rows for columns using tool bar

that gets a table like the one you show above

Drag Sales to color (This is a trick to simply hold it for a minute ),
click the down-arrow on the new SALES pill in the mark card,
select "Add a table calculation", select Running Total, of SUM, compute using Table(down), but don't close this popup window yet.

click Add Secondary Calculation checkbox at the bottom select Percent Different From compute using table down relative to Previous

Accept your work by closing the popup (x).

NOW, change the new pill in the mark card from color to text

you can see the 5.1% at the bottom. Almost done.

Reformat again by clicking table in ShowMe and flipping axes.

click the sales column header and hide it

create a new calculated field label 'rows-from-bottom' formula = last() close the popup

drag the new pill rows-from-bottom to the filters shelf select range 0 to 0 close the popup.

Done.

For the next two weeks you can see the finished workbook here https://public.tableau.com/app/profile/wade.schuette/viz/month-to-month/hiderows?publish=yes