'Tableau: How to calculate YoY retention (current year value divided by sum of previous year's values)

As per the screenshot (below), I would like to calculate in Tableau the percentage of a current value (this year's "RETURNING" total) versus the sum of a previous year's totals (previous year's "NEW" + "RETURNING" total).

sample calculation

My thinking is that I could:

  1. Add the "Total" of [DistinctPersons] for each [Level] (using a LOD, but this returns an "aggregate of aggregates" error); then …
  2. For each "RETURNING" value in [New or Returning], divide the current year's "RETURNING" value by the previous year's calculated "Total" values.

I know this probably would involve some combination of LODs and window functions but, because the measure is already an aggregate result, it's a bit trickier than I thought.



Solution 1:[1]

If you remove Year from the columns shelf and New or Returning from the row shelf, you do the following:

(IF ATTR([Year]) = 2022 AND ATTR([New or Returning]) = 'RETURNING' THEN [DistinctPersons] END)
-
(IF ATTR([Year]) = 2021 THEN [DistinctPersons] END)

Since [DistinctPersons] is already aggregated, the ATTR() is required around the other fields.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bernardo