Splunk Search

Find the number of days between the earliest and latest event.

kimberlytrayson
Path Finder

I need help with an employee travel analysis report.
I have an index containing information about employee office check-ins in various countries. Events have fields Employee, Time, Country

For example

John Doe, 2023-11-15T20:05:31.000+00:00, France
...
John Doe, 2023-11-18T10:00:31.000+00:00, France
...

John Doe, 2023-11-20T10:05:31.000+00:00, United States
...

John Doe, 2023-11-25T20:05:31.000+00:00, United States

 

At the end I would like to get the result showing duration in days between first checkin and last checkin per employee per country


John Doe, 3d, France
John Doe, 5d, United States

Tags (1)
0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

It would help to know what you've tried already so we don't suggest something that doesn't work.

A thing to remember is Splunk can't compare timestamps in text form - they must be converted to epoch (integer) form, first.

| makeresults format=csv data="Employee, Time, Country
John Doe, 2023-11-15T20:05:31.000+00:00, France
John Doe, 2023-11-18T10:00:31.000+00:00, France
John Doe, 2023-11-20T10:05:31.000+00:00, United States
John Doe, 2023-11-25T20:05:31.000+00:00, United States"
``` Above creates demo data.  Delete IRL ```
``` Convert Time to epoch ```
| eval eTime=strptime(Time, "%Y-%m-%dT%H:%M:%S.%3N%:z")
``` Find the lowest and highest time for each employee/country pair ```
| stats min(eTime) as start, max(eTime) as end by Employee,Country
``` Calculate the duration in days.  Add "d" suffix for display ```
| eval duration=round((end-start)/86400,0) . "d"
| table Employee duration Country
---
If this reply helps you, Karma would be appreciated.

View solution in original post

richgalloway
SplunkTrust
SplunkTrust

It would help to know what you've tried already so we don't suggest something that doesn't work.

A thing to remember is Splunk can't compare timestamps in text form - they must be converted to epoch (integer) form, first.

| makeresults format=csv data="Employee, Time, Country
John Doe, 2023-11-15T20:05:31.000+00:00, France
John Doe, 2023-11-18T10:00:31.000+00:00, France
John Doe, 2023-11-20T10:05:31.000+00:00, United States
John Doe, 2023-11-25T20:05:31.000+00:00, United States"
``` Above creates demo data.  Delete IRL ```
``` Convert Time to epoch ```
| eval eTime=strptime(Time, "%Y-%m-%dT%H:%M:%S.%3N%:z")
``` Find the lowest and highest time for each employee/country pair ```
| stats min(eTime) as start, max(eTime) as end by Employee,Country
``` Calculate the duration in days.  Add "d" suffix for display ```
| eval duration=round((end-start)/86400,0) . "d"
| table Employee duration Country
---
If this reply helps you, Karma would be appreciated.

tscroggins
Influencer

I often simplify time deltas using stats range():

| stats range(eval(strptime(Time, "%Y-%m-%dT%H:%M:%S.%3N%:z"))) as duration by Employee Country
| fieldformat duration=round(duration/86400, 0)."d"
``` or ```
``` | fieldformat duration=tostring(duration, "duration") ```
``` for Splunk-normalized [D+]HH:MM:SS display ```

Get Updates on the Splunk Community!

Accelerating Observability as Code with the Splunk AI Assistant

We’ve seen in previous posts what Observability as Code (OaC) is and how it’s now essential for managing ...

Integrating Splunk Search API and Quarto to Create Reproducible Investigation ...

 Splunk is More Than Just the Web Console For Digital Forensics and Incident Response (DFIR) practitioners, ...

Congratulations to the 2025-2026 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...