Splunk Search

How to dynamically pass the current and last month in search?

avni26
Explorer

Hi,

I have field as Jan , Feb ,Mar .... Dec with values for each month and I want to take sum of current month and last month.
I was truing to first check the current month and last month using eval like below and this eval gives me the current month name(Jul) , last month as Jun

eval curr_mon=strftime(now(), "%b")

eval last_mon= strftime(relative_time(now(),"-mon"),"%b")

Now I have field names
Name     id            Jan Feb Mar Jul Jun Aug
xyz      123A         10    20  20    25   30   15
ABC    145B          20    15  30   25   30    20

Want to take sum of current month and last month field with stats, I tried like below but got no results;

stats sum('curr_mon') as current_total sum('last_mon') as last_total 

I want this search to automatically show the current month's sum, and not using a static value in the stats sum expression.

Please suggest.

Labels (3)
0 Karma

to4kawa
Ultra Champion
| makeresults
| eval _raw="Name,id,Jan,Feb,Mar,Jul,Jun,Aug
xyz,123A,10,20,20,25,30,15
ABC,145B,20,15,30,25,30,20"
| multikv forceheader=1
| table Name,id,Jan,Feb,Mar,Jul,Jun,Aug
| rename COMMENT as "this is sample"
| rename Name as _Name , id as _id
| stats sum(*) as *
| eval tmp=1
| untable tmp month sums
| eval cur_mon=strftime(now(),"%b") ,last_mon=strftime(relative_time(now(),"-1mon"),"%b")
| eval current_total=if(month=cur_mon,sums,NULL), last_total=if(month=last_mon,sums,NULL)
| stats values(current_total) as current_total values(last_total) as last_total
0 Karma
Get Updates on the Splunk Community!

Built-in Service Level Objectives Management to Bridge the Gap Between Service & ...

Wednesday, May 29, 2024  |  11AM PST / 2PM ESTRegister now and join us to learn more about how you can ...

Get Your Exclusive Splunk Certified Cybersecurity Defense Engineer Certification at ...

We’re excited to announce a new Splunk certification exam being released at .conf24! If you’re headed to Vegas ...

Share Your Ideas & Meet the Lantern team at .Conf! Plus All of This Month’s New ...

Splunk Lantern is Splunk’s customer success center that provides advice from Splunk experts on valuable data ...