Splunk Search

How can I set bin _time value based on different fields?

whitefang1726
Path Finder

I want to run a search query but the _bin span value will change based on the field values.
Example:

Instead of using this, 

index=main sourcetype=test (host=hostnameA OR hostnameB OR hostnameC) 
| bin _time span=1h
| stats count by _time, host

for hostnameA -> I want the span value to be every 10m
for hostnameB -> every 30m
for hostnameC -> every 1h

Thanks!

Labels (1)
0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

All bin is doing is rounding down _time to the nearest span. You can do this yourself with a simple bit of maths. Here is a run-anywhere example, using gentimes to create some event, and random to assign hosts to the events, then calculating the interval between the start and each event time, rounding this down to the nearest span (dependent on the host), and adding that back to the start time. This aligns to the bins to the start time. You could do something similar if you want to align to the end time, or if you wanted even the middle time.

| gentimes start=-1 increment=10s 
| rename starttime as _time 
| fields _time
| eval host=mvindex(split("ABC",""),random() %3)


| eval span=case(host="A",10*60,host="B",30*60,host="C",60*60)
| eventstats min(_time) as start
| eval bucket=floor((_time-start)/span)*span
| eval _time=start+bucket
| stats count by _time host
0 Karma

whitefang1726
Path Finder

Nice, that works, I usually used append to add fields , never thought it would work on events as a rows. Thanks, big help @manjunathmeti !

0 Karma

manjunathmeti
Champion

You're welcome! Please upvote and accept the answer.

0 Karma

manjunathmeti
Champion

hi @whitefang1726,

You can count events for each host separately and use append to combine them.

index=main sourcetype=test host=hostnameA 
| bin _time span=10m
| stats count by _time, host
| append 
    [ search index=main sourcetype=test host=hostnameB
    | bin _time span=30m 
    | stats count by _time, host] 
| append 
    [ search index=main sourcetype=test host=hostnameC 
    | bin _time span=1h 
    | stats count by _time, host]

 

If this reply helps you, a like would be appreciated.

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!

Why Splunk Customers Should Attend Cisco Live 2026 Las Vegas

Why Splunk Customers Should Attend Cisco Live 2026 Las Vegas     Cisco Live 2026 is almost here, and this ...

What Is the Name of the USB Key Inserted by Bob Smith? (BOTS Hint, Not the Answer)

Hello Splunkers,   So you searched, “what is the name of the usb key inserted by bob smith?”  Not gonna lie… ...

Automating Threat Operations and Threat Hunting with Recorded Future

    Automating Threat Operations and Threat Hunting with Recorded Future June 29, 2026 | Register   Is your ...