So- Just a quick example on how to execute a trendline for the percentage change on a chart.
This can be useful for predicting future change, and allows you to more easily at a glance determine the current trend of your data, when it may not be clear.
Lets start with some base data.
![](https://i0.wp.com/xtremeownage.com/wp-content/uploads/2020/03/1_Base-Data.png?fit=1024%2C268&ssl=1)
In the above data, the goal is to display the percentage change week to week, as a easy-to-view trendline.
First- we need to calculate the percent change from one interval to the next. Thankfully- this is pretty easy with streamstats.
| streamstats current=f window=1 values(Value) as PValue | eval PctChange = ((Value-PValue)/PValue)*100 | fields - PValue
For an explanation- “streamstats current=f window=1 values(Value) as PValue” will store the previous value as new variable, “PValue”.
We can then use the new variable to calculate the percentage change. This leaves us with the following data.
![](https://xtremeownage.com/wp-content/uploads/2020/03/2_PctChange.png)
While, at this point, we could now enable an overlay for PctChange- it does not clearly display our trend. The goal, is for whoever looks at this chart, to be able to clearly see how the data is trending over time.
![](https://i0.wp.com/xtremeownage.com/wp-content/uploads/2020/03/3_Overlay.png?fit=1024%2C357&ssl=1)
To better accomplish the goal, we are going to utilize the trendline command.
The documentation for Trendline can be found here: Trendline
For this example, we are going to do a simple moving average, for at least 4 periods.
| trendline sma4(PctChange) as PctChangeTrend
This should be adequate for smoothing out the results.
![](https://i2.wp.com/xtremeownage.com/wp-content/uploads/2020/03/4_SmoothedData.png?fit=1024%2C148&ssl=1)
Lastly- I do recommend setting the intervals for your overlay to produce an easily distinguishable trend.
Here is my final query:
| inputlookup temp_1.csv | timechart span=1w sum(value) as Value | streamstats current=f window=1 values(Value) as PValue | eval PctChange = ((Value-PValue)/PValue)*100 | trendline sma4(PctChange) as PctChangeTrend | fields - PValue PctChange
![](https://i0.wp.com/xtremeownage.com/wp-content/uploads/2020/03/5_Final-1.png?fit=1024%2C179&ssl=1)