Splunk Search

How to Subtract column values in a table and create new filed with dynamic column names

Vignesh-107
Path Finder

Hi Team,

I have a splunk search which results in the below table...

 Col1Col2Col3Col4
Row1XXXX
Row2XXXX
Row3XXXX

 

The Col* is dynamic based the time value here its set to 4 month. Each column represent a column with the values from 0-99.

 Jan20Feb20Mar20Apr20
Row10834
Row29975
Row38171

 

I want check Col2 - Col1 and if the Col2 value is less than Col1 value it should create a new colum and with values like Increasing ,Decreasing, Nothing.

Expecting the result

 Jan20Feb20Comp_of_Feb_Minus_JanMar20Apr20Comp_of_Apr_Minus_Mar
Row108Increased34Increased
Row299Nothing changed75Decrease
Row381Decrease71Decrease

 

Labels (1)
Tags (1)
0 Karma
1 Solution

renjith_nair
Legend

Try adding this to your existing search

"your search"

| eval count_1=1
| eval prev_1=0
| foreach *
[ eval mod_1=count_1%2
| eval Comp_of_<<FIELD>>_Minus_{PREV_COL_1}_{mod_1}=case('<<FIELD>>' - prev_1 > 0,"Increased",'<<FIELD>>' - prev_1 < 0,"Decreased",1=1,"Nothing Changed")
| eval prev_1='<<FIELD>>'
| eval count_1=count_1+1
| eval PREV_COL_1="<<FIELD>>"]
| rename *_0 as *
| fields - *_1

 

Here is a  run anywhere example implementing the same logic

| makeresults 
| eval "1Jan20"=1 
| eval "2Feb20"=2 
| eval "3Mar20"=5 
| eval "4Apr20"=4 
| append 
    [| makeresults 
    | eval "1Jan20"=4 
    | eval "2Feb20"=5 
    | eval "3Mar20"=7 
    | eval "4Apr20"=3
        ] 
| append 
    [| makeresults 
    | eval "1Jan20"=5 
    | eval "2Feb20"=2 
    | eval "3Mar20"=7 
    | eval "4Apr20"=9
        ] 
| fields - _time 
| eval count_1=1 
| eval prev_1=0 
| foreach * 
    [ eval mod_1=count_1%2 
    | eval Comp_of_<<FIELD>>_Minus_{PREV_COL_1}_{mod_1}=case('<<FIELD>>' - prev_1 > 0,"Increased",'<<FIELD>>' - prev_1 < 0,"Decreased",1=1,"Nothing Changed") 
    | eval prev_1='<<FIELD>>' 
    | eval count_1=count_1+1 
    | eval PREV_COL_1="<<FIELD>>"]  
| rename *_0 as *
| fields - *_1
---
What goes around comes around. If it helps, hit it with Karma 🙂

View solution in original post

Vignesh-107
Path Finder

Thanks Renjith_Nair,

But i am getting the results like below,

RowsMar20Apr20Apr20-Mar20Mar20-RowsFeb20-col1
Row1083decreasedIncreased
Row2997decreasedNothing Changed
Row3817Increaseddecreased

 

0 Karma

Vignesh-107
Path Finder

Thank you so much renjith_nair. But i am  looking result like below

I am looking the results like 

 Feb20Mar20Apr20Apr20-Mar20Mar20-Feb20Feb20-Jan20
Row1083decreasedIncreasedNothing Changed
Row2997decreasedNothing ChangedNothing Changed
Row3817Increaseddecreaseddecreased



0 Karma

renjith_nair
Legend

Ok, in your first example , you had difference between alternative months and hence the search was formed.

Try this and let me know what changes you need

| makeresults 
| eval "1Jan20"=1 
| eval "2Feb20"=2 
| eval "3Mar20"=5 
| eval "4Apr20"=4 
| append 
    [| makeresults 
    | eval "1Jan20"=4 
    | eval "2Feb20"=5 
    | eval "3Mar20"=7 
    | eval "4Apr20"=3
        ] 
| append 
    [| makeresults 
    | eval "1Jan20"=5 
    | eval "2Feb20"=2 
    | eval "3Mar20"=7 
    | eval "4Apr20"=9
        ] 
| fields - _time 
| eval count_temp=1 
| eval prev_temp=0 
| foreach * 
    [| eval Comp_of_<<FIELD>>_Minus_{PREV_COL_temp}=case('<<FIELD>>' - prev_temp > 0,"Increased",'<<FIELD>>' - prev_temp < 0,"Decreased",1=1,"Nothing Changed") 
    | eval prev_temp='<<FIELD>>' 
    | eval count_temp=count_temp+1 
    | eval PREV_COL_temp="<<FIELD>>"]  
| rename *_0 as *
|fields - *temp*,*Minus_
---
What goes around comes around. If it helps, hit it with Karma 🙂
0 Karma

renjith_nair
Legend

Try adding this to your existing search

"your search"

| eval count_1=1
| eval prev_1=0
| foreach *
[ eval mod_1=count_1%2
| eval Comp_of_<<FIELD>>_Minus_{PREV_COL_1}_{mod_1}=case('<<FIELD>>' - prev_1 > 0,"Increased",'<<FIELD>>' - prev_1 < 0,"Decreased",1=1,"Nothing Changed")
| eval prev_1='<<FIELD>>'
| eval count_1=count_1+1
| eval PREV_COL_1="<<FIELD>>"]
| rename *_0 as *
| fields - *_1

 

Here is a  run anywhere example implementing the same logic

| makeresults 
| eval "1Jan20"=1 
| eval "2Feb20"=2 
| eval "3Mar20"=5 
| eval "4Apr20"=4 
| append 
    [| makeresults 
    | eval "1Jan20"=4 
    | eval "2Feb20"=5 
    | eval "3Mar20"=7 
    | eval "4Apr20"=3
        ] 
| append 
    [| makeresults 
    | eval "1Jan20"=5 
    | eval "2Feb20"=2 
    | eval "3Mar20"=7 
    | eval "4Apr20"=9
        ] 
| fields - _time 
| eval count_1=1 
| eval prev_1=0 
| foreach * 
    [ eval mod_1=count_1%2 
    | eval Comp_of_<<FIELD>>_Minus_{PREV_COL_1}_{mod_1}=case('<<FIELD>>' - prev_1 > 0,"Increased",'<<FIELD>>' - prev_1 < 0,"Decreased",1=1,"Nothing Changed") 
    | eval prev_1='<<FIELD>>' 
    | eval count_1=count_1+1 
    | eval PREV_COL_1="<<FIELD>>"]  
| rename *_0 as *
| fields - *_1
---
What goes around comes around. If it helps, hit it with Karma 🙂
Get Updates on the Splunk Community!

Announcing Scheduled Export GA for Dashboard Studio

We're excited to announce the general availability of Scheduled Export for Dashboard Studio. Starting in ...

Extending Observability Content to Splunk Cloud

Watch Now!   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to leverage ...

More Control Over Your Monitoring Costs with Archived Metrics GA in US-AWS!

What if there was a way you could keep all the metrics data you need while saving on storage costs?This is now ...