Splunk Search

How to use 2 timeranges in a single search?

hulahoop
Splunk Employee
Splunk Employee

I'd like to provide a table where the event count for today and yesterday are displayed. For example, count by status for access logs:

status | today's count | yesterday's count
------------------------------------------
 404          10               13
 500          20               24
 503          15               10

Is this possible?

Tags (1)
0 Karma
1 Solution

gkanapathy
Splunk Employee
Splunk Employee

You know this is just a 2-day timechart. If you're not that picky about the table layout:

sourcetype="access_combined" earliest=-2d@d latest=@d | timechart span=1d count by status 

to which you can add:

... | eval dy=now() | bucket dy span=1d | eval dy=if(_time=dy,"today","yesterday")

or if you are picky, this is how can transpose:

sourcetype="access_combined" earliest=-2d@d latest=@d | chart count by status,_time span=1d

The column names will be wrong though, and they change every day, so you can't just use rename. But the above is equivalent to:

sourcetype="access_combined" earliest=-2d@d latest=@d | bucket _time span=1d | stats count by status,_time | xyseries status _time count

which you then can to expand to (to fix the names of the columns):

sourcetype="access_combined" earliest=-2d@d latest=@d  | bucket _time span=1d | stats count by status,_time | eval dy=now() | bucket dy span=1d | eval dy=if(_time=dy,"today","yesterday") | xyseries status dy count

And BTW, your "today" isn't today's count, it's yesterdays (midnight to midnight of the previous day), and your "yesterday" is two days ago. Adjust earliest and latest as appropriate.

View solution in original post

gkanapathy
Splunk Employee
Splunk Employee

You know this is just a 2-day timechart. If you're not that picky about the table layout:

sourcetype="access_combined" earliest=-2d@d latest=@d | timechart span=1d count by status 

to which you can add:

... | eval dy=now() | bucket dy span=1d | eval dy=if(_time=dy,"today","yesterday")

or if you are picky, this is how can transpose:

sourcetype="access_combined" earliest=-2d@d latest=@d | chart count by status,_time span=1d

The column names will be wrong though, and they change every day, so you can't just use rename. But the above is equivalent to:

sourcetype="access_combined" earliest=-2d@d latest=@d | bucket _time span=1d | stats count by status,_time | xyseries status _time count

which you then can to expand to (to fix the names of the columns):

sourcetype="access_combined" earliest=-2d@d latest=@d  | bucket _time span=1d | stats count by status,_time | eval dy=now() | bucket dy span=1d | eval dy=if(_time=dy,"today","yesterday") | xyseries status dy count

And BTW, your "today" isn't today's count, it's yesterdays (midnight to midnight of the previous day), and your "yesterday" is two days ago. Adjust earliest and latest as appropriate.

hulahoop
Splunk Employee
Splunk Employee

I guess I can use the appendcols command, something like:

sourcetype="access_combined" earliest=-1d@d | stats count as "today's count" by status | appendcols [search sourcetype=access_combined earliest=-2d@d latest=-1d@d | stats count as "yesterday's count" by status] | fields + status, "today's count","yesterday's count"

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!

May 2026 Splunk Expert Sessions: Security & Observability

Level Up Your Operations: May 2026 Splunk Expert Sessions Whether you are refining your security posture or ...

Network to App: Observability Unlocked [May & June Series]

In today’s digital landscape, your environment is no longer confined to the data center. It spans complex ...

SPL2 Deep Dives, AppDynamics Integrations, SAML Made Simple and Much More on Splunk ...

Splunk Lantern is Splunk’s customer success center that provides practical guidance from Splunk experts on key ...