When calculating the totals per quarter or per year, we just need the numbers in the last month of the period, namely the number in the last month of the quarter, or the numbers in the last month of the year. This requires special calculations for both subtotals and grand totals.
Tableau Zen Master Jonathon Drummey has written profoundly on this topic in 2012 which served as my source of inspirations. In his blog, he showed how to manipulate the subtotals and grand totals.
In his examples, he showed how to use right most dimension for the custom calculation of both subtotals and grand totals. The aggregation result in his example is fairly simple. In our case, we found it not enough to give correct results for both totals.
In our solution, we used all the dimensions and derived a formula that allows to calculate subtotals and grand totals differently.
Note that we used both Month and Quarter Min()/Max() to set conditions. For quarterly subtotals, we used
- Min(Month) != Max(Month) and Min(Quarter) = Max(Quarter)
- Min(Month) != Max(Month) and Min(Quarter) != Max(Quarter)
The workbook for the above solution can be found here.
PS.
Note that the subtotals and grand totals seem to work using any linear aggregations. But there are cases where complex aggregations may not work such as ratio SUM()/SUM(). You will see AGG() as aggregation function in Tableau.
this is extremely helpful especially when creating accounting related workbooks in tableau.
ReplyDeletethis calculation does not seem to work if I start grouping my years.
For example, if I have years 2016 2017 2018 2019 and 2020. I want to show data for 2 current years and group the other years into "Prior Years".
so my calculated field will be something like:
if YEAR(TODAY()) - YEAR([Month]) >= 2 then "Prior Years" ELSE STR( YEAR([Month])) END
For Prior year, how do i show the latest value? in this case, the value for 2018-12-31 in my view?
Send me a sample workbook if you can, if the issue still persists.
DeleteI want this type of calculation for finding active users based on termination date...plz help me out!!
ReplyDeleteSee if this works for you http://vizdiff.blogspot.com/2016/05/counting-active-customers.html
Delete