Splunk Search

How to get today's and last 7 day's average in single query

spoolunk
Engager

I have a query
index=errors earliest=@d latest=now |stats count(ErrorCode) as ErrorCountForToday by host

I would like this query to show me error count for today (as doing already) and the average error count for the last 7 days . so that I can compare how much increase decrease happened.

Currently we are using a input csv which populates a file with 7 days average data and we pick it up from there.

Let me know if that is achievable from the query.

Thanks,

Tags (1)
0 Karma
1 Solution

woodcock
Esteemed Legend

Like this:

index=errors earliest=-7d@d latest=now
| timechart span=1d count(ErrorCode) AS ErrorCount BY host
| untable _time host count
| eval start_of_today = relative_time(now(), "@d") 
| eval when= if((_time >= start_of_today), "Today", "Last Week")
| chart avg(count) AS count BY when

View solution in original post

woodcock
Esteemed Legend

Like this:

index=errors earliest=-7d@d latest=now
| timechart span=1d count(ErrorCode) AS ErrorCount BY host
| untable _time host count
| eval start_of_today = relative_time(now(), "@d") 
| eval when= if((_time >= start_of_today), "Today", "Last Week")
| chart avg(count) AS count BY when

spoolunk
Engager

Hello.

Thanks for your answer. But when I am running this, I am only seeing today's data, not the average of 7 days.

0 Karma

woodcock
Esteemed Legend

You are quite correct; there was a flaw in my original answer. Try the updated answer.

0 Karma

sayanidasgupta
Explorer

what is the updated query, could you please tell

0 Karma

woodcock
Esteemed Legend

I edited the answer so the one that you see is the fixed one.

0 Karma

sayanidasgupta
Explorer

Thanks much @woodcock.
Actually I am looking something like below -
Single Table containing -
stats count by DID TN - for today
avg count for last 7 day by DID and TN
deviation of today vs. Last 7 day avg count.

e.g.

DID TN Today Last7Dayavg Deviation - Today vs Last 7 day avg
123 A 230 330 -100
124 B 90 70 20

0 Karma

sayanidasgupta
Explorer

Could you please help, could not able to do this for a long.......

0 Karma

woodcock
Esteemed Legend

Ask a new question and @-me.

0 Karma

spoolunk
Engager

Thanks, this is working now. 🙂

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.
Get Updates on the Splunk Community!

Thanks for the Memories! Splunk University, .conf25, and our Community

Thank you to everyone in the Splunk Community who joined us for .conf25, which kicked off with our iconic ...

Data Persistence in the OpenTelemetry Collector

This blog post is part of an ongoing series on OpenTelemetry. What happens if the OpenTelemetry collector ...

Introducing Splunk 10.0: Smarter, Faster, and More Powerful Than Ever

Now On Demand Whether you're managing complex deployments or looking to future-proof your data ...