Reputation: 95
is there a window calculation that I can use to find the year over year difference in sales?
Upvotes: 2
Views: 1836
Reputation: 570
The easiest solution is to create a new dimension using the 'Lookup' window function.
Right-click on the Data pane and choose 'Create Calculated Field'
Name your calculated field (for example, 'YOY Diff')
Enter the following formula:
(LOOKUP(ZN(SUM([Sales])),0) - LOOKUP(ZN(SUM([Sales])),-1))/ABS(LOOKUP(ZN(SUM([Sales])),-1))
Save the calculated field. Right click and select 'Compute Using' = [Date] field and choose the 'Year' option
Save the calculated field and drag it into the Columns bar at the top next to the 'Year' dimension
This should add a column to your table next to each year with that year's 'YOY Diff' for each corresponding month.
Here's a quick explanation of the calculated field formula:
The LOOKUP window function takes the format LOOKUP(expression, [offset]) and returns the value of the expression with an offset relative to the current row. In this case, our expression is SUM[Sales], so we look at the current value (say, 2018) - the previous value (2017) and divide that difference by the previous value (2017).
The ZN function replaces NULL values with zeroes to avoid errors.
The ABS function ensures the % difference will be positive.
Upvotes: 2