Splunk Search

How to do the division of 2 values in the same field

Julia1231
Communicator

Hi,

I have a table like this:

id       value

1            12

2             10

I want to do this calculation by splunk: (10/12)*100% (means value  of second id / value of the first id)*100%

How do I do, please?

0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust

Calculations are done between fields in events so the first thing you need to do is get both values into the same event. There a number of ways to do this. If you only have two events, try this:

| stats list(value) as value
| eval percent=100*mvindex(value,1)/mvindex(value,0)

View solution in original post

Julia1231
Communicator

It works well,

Thank you!

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Calculations are done between fields in events so the first thing you need to do is get both values into the same event. There a number of ways to do this. If you only have two events, try this:

| stats list(value) as value
| eval percent=100*mvindex(value,1)/mvindex(value,0)

Julia1231
Communicator

Hi @ITWhisperer ,

What can I do when there are more than 2 values, please?

id       value

1            12

2             10

3              15

4              20

 

Thanks

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

mvindex uses index into the multivalue field, starting at 0, so if you wanted to calculate the percentage for id 3 and 4 you would do something like this

| stats list(value) as value
| eval percent=100*mvindex(value,3)/mvindex(value,2)
0 Karma

Julia1231
Communicator

@ITWhisperer 
When doing it, I have this error: Error in 'eval' command: Type checking failed. '*' only takes numbers.

And I have to define 2 variable to store each value and it works.... Do you know why?

| stats list(value) as value
|eval n= mvindex(value,3)
|eval m= mvindex(value,2)
|eval percent=100*n/m

 

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

mvindex is probably returning a string so either do it the way you are or use the tonumber function

| stats list(value) as value
|eval percent=100*tonumber(mvindex(value,3))/tonumber(mvindex(value,2))
0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Community Content Calendar, September edition

Welcome to another insightful post from our Community Content Calendar! We're thrilled to continue bringing ...

Splunkbase Unveils New App Listing Management Public Preview

Splunkbase Unveils New App Listing Management Public PreviewWe're thrilled to announce the public preview of ...

Leveraging Automated Threat Analysis Across the Splunk Ecosystem

Are you leveraging automation to its fullest potential in your threat detection strategy?Our upcoming Security ...