Skip to main content
Announcements
Accelerate Your Success: Fuel your data and AI journey with the right services, delivered by our experts. Learn More
cancel
Showing results for 
Search instead for 
Did you mean: 
cb5981
Contributor II
Contributor II

Code to calculate consecutive negative or positive monthly values

Hi all, 

Wondering if anyone has a "simple" way to create the score indicator below.

  1. Each month has a score assigned to it. 
  2. The ask is to then provide a score indicator as to how many months the score has been going up or down consecutively.  

Example:

jan feb mar apr may jun jul aug sep oct Score
31 38 38 56 20 99 100 98 96 94 -3

 

Above.... the Score of -3 indicates that for the past 3 months the score has been going down. 

i.e. from July onwards the trend has been consecutively down. (100 > 98 > 96 > 94)

  • If the Scores had been consecutively  going down since April the Score would be -6
  • If the scores had been going up since August, the score would be +2


In the code is there a way anyone can think of doing this?  I was thinking of using the previous function somehow but it starts to get messy quite soon so any thoughts are appreciated.

Thank you.

Labels (1)
2 Solutions

Accepted Solutions
hic
Former Employee
Former Employee

Try

Data:
Load
If(Trend=Peek(Trend),Peek(Score)+Trend,Trend) as Score,
*;
Load
If(Change=0,Peek(Trend),Sign(Change)) as Trend,
*;
Load
ID,
Month,
Value,
Value - Peek(Value) as Change
Resident SourceData
Order By Month;

Let Score = Peek('Score',-1,'Data');

Henric_Cronstrm_0-1650887205327.png

 

View solution in original post

3 Replies
hic
Former Employee
Former Employee

cb5981
Contributor II
Contributor II
Author

Thanks Henric, 

 

I've had a play around with the code here but still not 100% sure on how to implement counters if negative as well (it seems to only account for positive scores).  i.e. if months jan - mar were increasing, it would be +2, but then from Mar - Jun if they were decreasing I should see -3.

hic
Former Employee
Former Employee

Try

Data:
Load
If(Trend=Peek(Trend),Peek(Score)+Trend,Trend) as Score,
*;
Load
If(Change=0,Peek(Trend),Sign(Change)) as Trend,
*;
Load
ID,
Month,
Value,
Value - Peek(Value) as Change
Resident SourceData
Order By Month;

Let Score = Peek('Score',-1,'Data');

Henric_Cronstrm_0-1650887205327.png