Splunk Search

How to dynamically subtract two last column values?

chrisboy68
Contributor

Hi, have  SPL that generates months of data. I want subtract just the last two columns. The fields will change month to month, so I can't hard code.

Given the below sample, how can I get lastMonthDiff without hardcoding the field values? Thank you! Chris

 

 

 

| makeresults 
| eval "2202-01"=1 
| eval "2202-02"=2 
| eval "2202-03"=5 
| eval "2202-04"=4 
| append 
    [| makeresults 
    | eval "2202-01"=4 
    | eval "2202-02"=5 
    | eval "2202-03"=7 
    | eval "2202-04"=3
        ] 
| append 
    [| makeresults 
    | eval "2202-01"=5 
    | eval "2202-02"=2 
    | eval "2202-03"=7 
    | eval "2202-04"=9
        ] 
| fields - _time |foreach *  [eval lastMonthDiff = '2202-03' - '2202-04'] 

 

 

 

Labels (1)
0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust
| foreach *
    [| eval diff=if(isnull(previous),null(),previous-'<<FIELD>>')
    | eval previous='<<FIELD>>']

View solution in original post

ITWhisperer
SplunkTrust
SplunkTrust
| foreach *
    [| eval diff=if(isnull(previous),null(),previous-'<<FIELD>>')
    | eval previous='<<FIELD>>']

chrisboy68
Contributor

Awesome, thank you!

0 Karma
Get Updates on the Splunk Community!

Updated Team Landing Page in Splunk Observability

We’re making some changes to the team landing page in Splunk Observability, based on your feedback. The ...

New! Splunk Observability Search Enhancements for Splunk APM Services/Traces and ...

Regardless of where you are in Splunk Observability, you can search for relevant APM targets including service ...

Webinar Recap | Revolutionizing IT Operations: The Transformative Power of AI and ML ...

The Transformative Power of AI and ML in Enhancing Observability   In the realm of IT operations, the ...