Summary: This thread explains how to use DQL to calculate averages across multiple time-shifted metrics in timeseries queries. It includes syntax examples and practical use cases for trend analysis.
So, I have a DQL Query that returns the current metrics and then the previous 7, 14, and 21 days for that metric. I want to average those 3 previous weeks of metrics together to get a “baseline average” of what is “normal” for this metric. (Yes, I know Dynatrace does seasonal baseline using anomaly detection, but I’m not a fan of the way they do it and would much rather prefer this method).
My problem is, I can’t figure out how to average the three previous metrics into a single average value.
The end result I’m looking for is a chart that has just two lines on it. The current metrics value (which I’ve labelled “curLogins”) and a line that is the average of the three timeshifted metrics (I’ve labelled each of those with “last#Logins”.
Here is the DQL that returns the current login metric value and the three time shifted values for that same metric:
timeseries curLogins = avg(custom.db.query, rate: 1m), by: { query_name }, interval:1m<br>| filter query_name == "Login Count"<br>| append [<br>timeseries last7Logins = avg(custom.db.query, rate: 1m), by: { query_name }, interval:1m, shift:-7d<br>| filter query_name == "Login Count"<br>]<br>| append [<br>timeseries last14Logins = avg(custom.db.query, rate: 1m), by: { query_name }, interval:1m, shift:-14d<br>| filter query_name == "Login Count"<br>]<br>| append [<br>timeseries last21Logins = avg(custom.db.query, rate: 1m), by: { query_name }, interval:1m, shift:-21d<br>| filter query_name == "Login Count"<br>]
Easy enough and this results in a chart that looks like this:
So, now I just need to get those 3 “last#Logins” metric values averaged together into a single “baselineLogins” line. If this was SQL, I’d have no problem, but me and DQL are just not jiving, and I have a hard time with anything other than the simplest of queries.
I thought I could just add this to the end of it:
| fieldsRemove query_name<br>| fieldsAdd baselineAvg = (last7Logins[] + last14Logins[] + last21Logins[])/3<br>| fieldsRemove last7Logins, last14Logins, last21Logins
But for some reason that results in a chart that has only a single line and a legend that has curLogins listed 4 times…:
Can somebody please educate me on what I’m doing wrong here? It seems like it should be pretty simple. Add the 3 values together and divide by 3 to get their average. But that doesn’t appear to work here…


