# Timeseries regression - trend line

**URL:** <https://community.dynatrace.com/t/timeseries-regression-trend-line/54845>\
**Category:** DQL\
**Tags:** dql, timeseries\
**Created:** [February 2, 2026, 5:37pm UTC](https://community.dynatrace.com/t/timeseries-regression-trend-line/54845 "2026-02-02T17:37:47Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![J01am](https://avatars.discourse-cdn.com/v4/letter/j/dec6dc/32.png) [@J01am](https://community.dynatrace.com/u/J01am)\
**Post date:** [February 2, 2026, 5:37pm UTC](https://community.dynatrace.com/t/timeseries-regression-trend-line/54845/1 "2026-02-02T17:37:47Z")

</div>

Hi all,

I needed to display the trend of a time series using a mathematical regression. Since DQL doesn’t provide an out-of-the-box function for this today, I implemented it with the following DQL query.

_timeseries y=avg(dt.host.cpu.usage)_

_// N = nombre de points non-null_  
_| fieldsAdd n = arraySize(arrayRemoveNulls(y))_

_// Sommes pour la régression (on ignore les slots où y[] est null)_  
_| fieldsAdd sumY = arraySum(y),_  
_sumX = arraySum(iCollectArray(if(isNotNull(y[]), iIndex()))),_  
_sumXX = arraySum(iCollectArray(if(isNotNull(y[]), toDouble(iIndex()) \* toDouble(iIndex())))),_  
_sumXY = arraySum(iCollectArray(if(isNotNull(y[]), toDouble(iIndex()) \* toDouble(y[]))))_

_// Coefficients slope/intercept_  
_| fieldsAdd denom = (n \* sumXX - sumX \* sumX)_  
_| fieldsAdd slope = if(denom != 0, (n \* sumXY - sumX \* sumY) / denom)_  
_| fieldsAdd intercept = if(n != 0, (sumY - slope \* sumX) / n)_

_// Série trend (même longueur que y, nulls conservés aux mêmes positions)_  
_| fieldsAdd trend = iCollectArray(if(isNotNull(y[]), intercept + slope \* toDouble(iIndex())))_

_// Output: 2 courbes (y + trend)_  
_| fields timeframe, interval, y, trend_

Hopefully this is useful to others—and maybe it could even become an out-of-the-box DQL function in the future.

 ![image.png](https://global.discourse-cdn.com/dynatrace/original/3X/8/4/845de181cd207d88f1816a40e68763ed45d9382e.png)

Thanks
