Saturday, May 23, 2020

Getting the Last Date/Value/Variance in Tableau

There are probable half a dozen ways to do it, under different circumstances. Each technique may have its own advantages.

Here is my favorite technique, which is
1) independent of date dimension. 
2) can be used from a secondary data source.
3) applicable in labels, references and annotations.

It has served me well in a number of designs. Just want to share the techniques here with you.

Here are the main steps for getting them.

Last Date

1.Create Top 1 set in Date dimension. Name the set as Last Date.
One may get the last date using Level of Details instead of Set. But then you have to be very careful of date filters.

2.To get the Last Order Date (as a Date type):

Last Value

To get the Last Value:
Note that this is at the row level or record level. May need aggregation in other calculations. Being able to treat data at row level provides us the greatest flexibility.

Last Variance

To get the Last Variance, we need to create the 2nd Last Date set first.

- Create a set in Date dimension. Name the set as the 2nd Last Date
- To get the 2nd Last Value: If 2nd Last Date Then Sales End

- To get the Last Variance, Sum(Last Value)-Sum(2nd Last Value)

- To get the Last Variance%: Last Variance/Sum(2nd Last Value)

Last Month/Value/Variance

We can apply the same technique to calculate the last Month/Value/Variance for which we need to first create the dimension of custom date in month:
Then we can create the Last Month/Value/Variance formulas.

The above is for the case without dimension. If there is Date dimension in view, the technique can be based on table calculations. But that is a different topic.
Feel free to download the workbook here.

No comments:

Post a Comment