I have a problem with sorting records in a bar chart.
My goal is to sort vertical bar chart by a difference between last year and this year value (from biggest positive difference/ or from biggest negative difference). To paint a picture:
my visual value is e.g. weight (Sum), I am grouping by Date (Year) - so I can see e.g. sum of weight this year vs last year
I try to sort by off-visual field which is weight difference calculated like this: periodOverPeriodDifference(sum({weight}), {Date}, YEAR)
This doesn’t do anything to the sorting. Can you tell me what I have been doing wrong and how should I approach this?
Do you want to sort simply by the biggest amount of change regardless of positive or negative or do you want to show biggest positive difference first and largest negative difference last (or vice versa)?
I would double check that your calculation is doing something relevant by adding your weight metric, date, and period over period calculation to a table to make sure the groupings are correct. If that data looks accurate then you could try wrapping your period over period calc in a rank function, and using the rank function for the sorting instead.
In the table looks like the rank is working, also I could set in the bar chart sorting by an off visual field by that didn’t work - just sorted alphabetically when I set the rank as the field. However I managed to do it - no idea why it works - but it does:
weight_delta = ifelse(
isNull(
sumIf({weight}, Year = maxOver(Year, [], PRE_AGG)) -
sumIf({weight}, Year = maxOver(Year, [], PRE_AGG) - 1)
),
0, // Neutral value, we have to subsitute NULLs with 0 or they show first when sorting by bottom
sumIf({weight}, Year = maxOver(Year, [], PRE_AGG)) -
sumIf({weight}, Year = maxOver(Year, [], PRE_AGG) - 1)
)
Then I encapsulate it with rank, and add a parameter to let users sort from biggest gain/biggest loss easily: