Splunk Search

Count values changes only when value in a column changes

nabeel652
Builder

Hello wonderful Splunk community,


I have some data where I want count to change only when status changes:

Status   Count
-------------------
Online      1
Online      1
Online     1
Break      2
Break       2
Online       3
Online       3
Lunch       4
Lunch        4
Lunch       4
Offline     5
Offline    5

Any help appreciated. 

Labels (5)
0 Karma
1 Solution

gcusello
SplunkTrust
SplunkTrust

Hi @nabeel652 ,

ok, please try this:

<your_search>
| autoregress status as status_old p=1
| table _time status status_old
| eval NO=0
| foreach NO
     [ eval NO=if(status=status_old,NO,NO+1)]
| accum NO

that I tested (and runs) in this way:

| makeresults | eval _raw= "Online      1"
| append [ | makeresults | eval _raw= "Online      1"]
| append [ | makeresults | eval _raw= "Online     1"]
| append [ | makeresults | eval _raw= "Break      2"]
| append [ | makeresults | eval _raw= "Break       2"]
| append [ | makeresults | eval _raw= "Online       3"]
| append [ | makeresults | eval _raw= "Online       3"]
| append [ | makeresults | eval _raw= "Lunch       4"]
| append [ | makeresults | eval _raw= "Lunch        4"]
| append [ | makeresults | eval _raw= "Lunch       4"]
| append [ | makeresults | eval _raw= "Offline     5"]
| append [ | makeresults | eval _raw= "Offline    5"]
| rex "^(?<status>\w+)"
| autoregress status as status_old p=1
| table _time status status_old
| eval NO=0
| foreach NO
     [ eval NO=if(status=status_old,NO,NO+1)]
| accum NO

Ciao.

Giuseppe

View solution in original post

gcusello
SplunkTrust
SplunkTrust

Hi @nabeel652 ,

if you already extracted the status field, you could try something like this:

<your_search>
| autoregress status as status_old p=1
| table _time status status_old
| where NOT status=status_old

Ciao.

Giuseppe

0 Karma

nabeel652
Builder

Thank you for the reply 🙂

I was able to achieve the same with


| streamstats reset_on_change=true count by Activity
| where count==1

But I want a count field that just increments when it senses a change in status.
so I can do my 
| stats earliest(_time) as startTime, latest(_time) as endTime by status, count
or something like that...

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @nabeel652 ,

did you tried with accum?

<your_search>
| autoregress status as status_old p=1
| table _time status status_old
| where NOT status=status_old
| eval NO=1 
| accum NO

Ciao.

Giuseppe

0 Karma

nabeel652
Builder

Thank you

I don't want to omit any records. This sort of gives me the required results but records are missing which I don't want. I want same number of rows after the solution is applied. 

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @nabeel652 ,

ok, please try this:

<your_search>
| autoregress status as status_old p=1
| table _time status status_old
| eval NO=0
| foreach NO
     [ eval NO=if(status=status_old,NO,NO+1)]
| accum NO

that I tested (and runs) in this way:

| makeresults | eval _raw= "Online      1"
| append [ | makeresults | eval _raw= "Online      1"]
| append [ | makeresults | eval _raw= "Online     1"]
| append [ | makeresults | eval _raw= "Break      2"]
| append [ | makeresults | eval _raw= "Break       2"]
| append [ | makeresults | eval _raw= "Online       3"]
| append [ | makeresults | eval _raw= "Online       3"]
| append [ | makeresults | eval _raw= "Lunch       4"]
| append [ | makeresults | eval _raw= "Lunch        4"]
| append [ | makeresults | eval _raw= "Lunch       4"]
| append [ | makeresults | eval _raw= "Offline     5"]
| append [ | makeresults | eval _raw= "Offline    5"]
| rex "^(?<status>\w+)"
| autoregress status as status_old p=1
| table _time status status_old
| eval NO=0
| foreach NO
     [ eval NO=if(status=status_old,NO,NO+1)]
| accum NO

Ciao.

Giuseppe

nabeel652
Builder

I was able to do it by some other way but your solution is cleaner and elegant. Thanks for the help 

0 Karma
Get Updates on the Splunk Community!

Exporting Splunk Apps

Join us on Monday, October 21 at 11 am PT | 2 pm ET!With the app export functionality, app developers and ...

Cisco Use Cases, ITSI Best Practices, and More New Articles from Splunk Lantern

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

Build Your First SPL2 App!

Watch the recording now!.Do you want to SPL™, too? SPL2, Splunk's next-generation data search and preparation ...