Splunk Search

How do you compare a previous average with the current average

henderz
New Member

Hello
I am trying to compare my average events in current month to previous 3 month average (per day [1,2,3...31]) based on _time

For example:
Considering that the current month is October (10). I am trying to compare the current count of random numbers that I have received on the 10/1 and 10/2 to the average of the counts that I have received on the 1st and 2nd of September(09) and August(08).

That's how i tried to do it:

`soc_events`

| eval mytime=strftime(_time, "%Y/%m/%d") | table mytime

| rex field=mytime "("?<Year>\d+)/(?<Month\d+)/(?<Day>\d+)")"

| stats count as Count by Year,Month,Day | sort Year,Month,Day

| eventstats last(Month) as Current_Month last(Year) as Current_Year | where Month!=CurrentMonth OR Year!=Current_Year

| stats avg(Count) as DayAveravge values(Month) as Months by Day

but it says syntax error in rex : missing terminator

Tags (2)
0 Karma

to4kawa
Ultra Champion

sample:

| gentimes start=08/01/20 end=11/01/20
| eval _time=starttime, Month=strftime(_time,"%m"), Days=strftime(_time,"%d")
| chart count by Days Month

recommend:

`soc_events`
| eval Month=strftime(_time, "%m"), Day=strftime(_time,"%d")
| chart count as Count by Month,Day 

result:

Day,08,09,10
----------
1,XX,YY,ZZ
2,XX,YY,ZZ
3,....

sorry, what's average?

0 Karma

MuS
SplunkTrust
SplunkTrust

Hi henderz,

please have a read about the timewrap command https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Timewrap#Examples

This SPL command provides options to achieve your use case.

Hope this helps ...

cheers, MuS

0 Karma

henderz
New Member

Hey thanks, for the reply
I edited my question could you see if you can help me now?

0 Karma

MuS
SplunkTrust
SplunkTrust

Hi henderz,

it says your regex is not correct and the reason are the " inside the regex:

 | rex field=mytime "("?<Year>\d+)/(?<Month\d+)/(?<Day>\d+)")"

use this instead:

 | rex field=mytime "(\"?<Year>\d+)/(?<Month\d+)/(?<Day>\d+)\")"

cheers, MuS

0 Karma

henderz
New Member

so i have tried it, the regex work but it didn't save the value in the new fields (Year, Month, Day)

0 Karma

MuS
SplunkTrust
SplunkTrust

Okay, give this a try:

| makeresults count=1000 
| eval _time=now() - random() 
| eval mytime=strftime(_time, "%Y/%m/%d") 
| table mytime 
| rex field=mytime "(?<Year>\d+)/(?<Month>\d+)/(?<Day>\d+)" 
| stats count as Count by Year,Month,Day 
| sort Year,Month,Day 
| eventstats last(Month) as Current_Month last(Year) as Current_Year 
| where Month!=CurrentMonth OR Year!=Current_Year 
| stats avg(Count) as DayAveravge values(Month) as Months by Day

The regex did not really work, but I have no idea if the current result is what you expect ¯\_(ツ)_/¯

cheers, MuS

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Why Splunk Customers Should Attend Cisco Live 2026 Las Vegas

Why Splunk Customers Should Attend Cisco Live 2026 Las Vegas     Cisco Live 2026 is almost here, and this ...

What Is the Name of the USB Key Inserted by Bob Smith? (BOTS Hint, Not the Answer)

Hello Splunkers,   So you searched, “what is the name of the usb key inserted by bob smith?”  Not gonna lie… ...

Automating Threat Operations and Threat Hunting with Recorded Future

    Automating Threat Operations and Threat Hunting with Recorded Future June 29, 2026 | Register   Is your ...