Splunk Search

Deriving a date based upon past # of months

bcanfiel83
Engager

Hi All,

I have a somewhat unusual requirement (at least to me) that I'm trying to figure out how to accomplish. In the query that I'm running, there's a column which displays a number representing the number of months, i.e.: 24, 36, 48, etc. 

What I'm attempting to do is take that number and create a new field which takes today's date and then subtracts the number of months to derive a prior date.

For example, if the # of months is 36, then the field would display "08/29/2021" ; essentially the same thing that this is doing: 

https://www.timeanddate.com/date/dateadded.html?m1=8&d1=29&y1=2024&type=sub&ay=&am=36&aw=&ad=&rec=

I'm not exactly sure where to begin with this one, so any help getting started would be greatly appreciated.

Thank you!

Labels (1)
0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust
| eval previous_time=relative_time(now(),"-".months."mon")

You would have to be careful around leap years and if the months is not a multiple of 12.

If you know the months is always going to be a multiple of 12, you could do this instead

| eval previous_time=relative_time(now(),"-".floor(months/12)."y")

View solution in original post

ITWhisperer
SplunkTrust
SplunkTrust
| eval previous_time=relative_time(now(),"-".months."mon")

You would have to be careful around leap years and if the months is not a multiple of 12.

If you know the months is always going to be a multiple of 12, you could do this instead

| eval previous_time=relative_time(now(),"-".floor(months/12)."y")

bcanfiel83
Engager

This is great, thank you!! 

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

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

What Is Splunk? Here’s What You Can Do with Splunk

Hey Splunk Community, we know you know Splunk. You likely leverage its unparalleled ability to ingest, index, ...

Level Up Your .conf25: Splunk Arcade Comes to Boston

With .conf25 right around the corner in Boston, there’s a lot to look forward to — inspiring keynotes, ...

Manual Instrumentation with Splunk Observability Cloud: How to Instrument Frontend ...

Although it might seem daunting, as we’ve seen in this series, manual instrumentation can be straightforward ...