Splunk Search

Variable earliest/latest in main search

DATT
Path Finder

I have a working dashboard that displays a number of metrics and KPIs for the previous week.  Today, I was asked to expand that dashboard to include a dropdown of all previous weeks over the last year

 

Using this query I was able to fill in my dashboard dropdown pretty easily

| makeresults 
| eval START_EPOCH = relative_time(_time,"-1y@w1")
| eval END_EPOCH = START_EPOCH + (60 * 60 * 24 * 358)
| eval EPOCH_RANGE = mvrange(START_EPOCH, END_EPOCH, 86400 * 7)
| mvexpand EPOCH_RANGE

| eval END_EPOCH = EPOCH_RANGE + (86400 * 7)

| eval START_DATE_FRIENDLY = strftime(EPOCH_RANGE, "%m/%d/%Y")
| eval END_DATE_FRIENDLY = strftime(END_EPOCH, "%m/%d/%Y")
| eval DATE_RANGE_FRIENDLY = START_DATE_FRIENDLY + " - " + END_DATE_FRIENDLY

| table DATE_RANGE_FRIENDLY, EPOCH_RANGE
| reverse

Using this I get a dropdown with values such as 

10/07/2024 - 10/14/2024
09/30/2024 - 10/07/2024

 

And so on, going back a year.

Adding it to my search as a token has been more challenging though. Here's what I'm trying to do:

index=someIndex earliest=$token_epoch$ latest=$token_epoch$+604800

 Doing this I get "Invalid latest_time: latest_time must be after earliest_time."

 

I've seen some answers around here that involve running the search then using WHERE to apply earliest and latest.  I'd like to avoid that because the number of records that would have to pulled before I could filter on earliest and latest is in the many millions. 

I've also considered using the timepicker but my concern there is the users who use this dashboard will pick the wrong dates.  I'd like to limit that by hardcoding the first and last days of the search via the dropdown.

Is there a way to accomplish relative earliest and latest dates/times like this?

Labels (1)
0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust
index=someIndex
    [| makeresults
    | eval earliest=$token_epoch$
    | eval latest=earliest+604800
    | table earliest latest]

View solution in original post

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust
index=someIndex
    [| makeresults
    | eval earliest=$token_epoch$
    | eval latest=earliest+604800
    | table earliest latest]
0 Karma

DATT
Path Finder

This did the trick for me.  I've used subsearches elsewhere in my dashboards and reporting, just didn't to use it with makeresults as well.

0 Karma

inventsekar
SplunkTrust
SplunkTrust

Hi @DATT , pls check this one:

| makeresults
| eval latestEpoch=$token_epoch$ + 604800
| index=someIndex earliest=$token_epoch$ latest=latestEpoch

 

thanks and best regards,
Sekar

PS - If this or any post helped you in any way, pls consider upvoting, thanks for reading !
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!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

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

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...