Splunk Search

Place value in custom column if date is a certain month?

rkassabov
Path Finder

I am trying to populate a custom column if a date stamp (that has been converted to epoch) if that date stamp matches to a certain month, like so:

| eval month=strftime("Step Date Completed","%b")
| eval January=case(month=="jan",winRate)
| eval February=case(month=="feb",winRate)
| eval March=case(month=="mar",winRate)
| eval April=case(month=="apr",winRate)
| eval May=case(month=="may",winRate)
| eval June=case(month=="jun",winRate)
| eval July=case(month=="jul",winRate)
| eval August=case(month=="aug",winRate)
| eval September=case(month=="sept",winRate)
| eval October=case(month=="oct",winRate)
| eval November=case(month=="nov",winRate)
| eval December=case(month=="dec",winRate)

However, that doesn't seem to be working. How would I accomplish this? Note, each month above corresponds to a custom column.

Tags (3)
0 Karma
1 Solution

cmerriman
Super Champion

Based on your comments, you need to convert to epoch and then to a string with month. The format of your date field looks to be a string, not epoch. Also, try an if statement.

| eval month=strftime(strptime('Step Date Completed',"%Y-%m-%d %H:%M:%S.%6N"),"%b")
 | eval January=if(month="Jan",winRate,null())
 | eval February=if(month="Feb",winRate,null())
 | eval March=if(month="Mar",winRate,null())
 | eval April=if(month="Apr",winRate,null())
 | eval May=if(month="May",winRate,null())
 | eval June=if(month="Jun",winRate,null())
 | eval July=if(month="Jul",winRate,null())
 | eval August=if(month="Aug",winRate,null())
 | eval September=if(month="Sept",winRate,null())
 | eval October=if(month="Oct",winRate,null())
 | eval November=if(month="Nov",winRate,null())
 | eval December=if(month="Dec",winRate,null())

View solution in original post

cmerriman
Super Champion

Based on your comments, you need to convert to epoch and then to a string with month. The format of your date field looks to be a string, not epoch. Also, try an if statement.

| eval month=strftime(strptime('Step Date Completed',"%Y-%m-%d %H:%M:%S.%6N"),"%b")
 | eval January=if(month="Jan",winRate,null())
 | eval February=if(month="Feb",winRate,null())
 | eval March=if(month="Mar",winRate,null())
 | eval April=if(month="Apr",winRate,null())
 | eval May=if(month="May",winRate,null())
 | eval June=if(month="Jun",winRate,null())
 | eval July=if(month="Jul",winRate,null())
 | eval August=if(month="Aug",winRate,null())
 | eval September=if(month="Sept",winRate,null())
 | eval October=if(month="Oct",winRate,null())
 | eval November=if(month="Nov",winRate,null())
 | eval December=if(month="Dec",winRate,null())

rkassabov
Path Finder

That worked, thank you!

0 Karma

elliotproebstel
Champion

Two minor revisions might make the difference. First, you'll need to use single quotes around the field name 'Step Date Completed', or else the strftime function is going to try to evaluate against the literal string Step Date Completed. Second, the matches in your month checks will be case-sensitive, and Splunk is going to return month names with the first letter capitalized. Give this a try:

| eval month=strftime('Step Date Completed',"%b")
| eval January=case(month=="Jan",winRate)
| eval February=case(month=="Feb",winRate)
| eval March=case(month=="Mar",winRate)
| eval April=case(month=="Apr",winRate)
| eval May=case(month=="May",winRate)
| eval June=case(month=="Jun",winRate)
| eval July=case(month=="Jul",winRate)
| eval August=case(month=="Aug",winRate)
| eval September=case(month=="Sept",winRate)
| eval October=case(month=="Oct",winRate)
| eval November=case(month=="Nov",winRate)
| eval December=case(month=="Dec",winRate)

rkassabov
Path Finder

No luck, for reference here is what my date stamps look like before converting to epoch (step date completed): 2018-02-22 21:54:00.380000

0 Karma

elliotproebstel
Champion

Can you run this and tell me the output:

your base search that creates the field `Step Date Completed` as an epoch time
| eval month=strftime('Step Date Completed',"%b")
| table 'Step Date Completed' month

I can help troubleshoot with that.

0 Karma

Sukisen1981
Champion

Well , first of all %b returns stuff like this - %b=Abbreviated month name. (Jan, Feb, etc.)
You are doing exact string matching,, jan is not equal to Jan.

Try changing your first letters after == to caps.
Second, i don't think you need == a single = should work.
If you are still not getting the results, i would like to see samples of your Step Date Completed field

rkassabov
Path Finder

No luck, for reference here is what my date stamps look like before converting to epoch (step date completed): 2018-02-22 21:54:00.380000

0 Karma
Get Updates on the Splunk Community!

Welcome to the Splunk Community!

(view in My Videos) We're so glad you're here! The Splunk Community is place to connect, learn, give back, and ...

Tech Talk | Elevating Digital Service Excellence: The Synergy of Splunk RUM & APM

Elevating Digital Service Excellence: The Synergy of Real User Monitoring and Application Performance ...

Adoption of RUM and APM at Splunk

    Unleash the power of Splunk Observability   Watch Now In this can't miss Tech Talk! The Splunk Growth ...