Splunk Search

## conditional average

Explorer

Hi

need to calculate the average based on a condition.

• testing=true vs testing=false  (lets say field A)
• field B has the values to average (milliseconds)
• field C has urls

something like this:

| stats avg(fieldB, when field A testing true) as trueV avg(fieldB, when field A testing false) as falseV by Field C

Goal is a table like this:

url  | average (true) | average( false).

Labels (1)
• ### stats

Tags (2)
1 Solution
SplunkTrust

Try something like this

``````| eval trueA=if(fieldA=true,fieldB,null())
| eval falseA=if(fieldA=true,null(),fieldB)
| stats avg(trueA) as trueV avg(falseA) as falseV by fieldC``````
SplunkTrust

Try something like this

``````| eval trueA=if(fieldA=true,fieldB,null())
| eval falseA=if(fieldA=true,null(),fieldB)
| stats avg(trueA) as trueV avg(falseA) as falseV by fieldC``````
Explorer

Thanks, that works. Now I understand this - this is making two separate 'fields' based on the condition, to avoid having the calculation either in the eval, or in the stats (which both don't work).

Get Updates on the Splunk Community!

#### The Splunk Success Framework: Your Guide to Successful Splunk Implementations

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

#### Splunk Training for All: Meet Aspiring Cybersecurity Analyst, Marc Alicea

Splunk Education believes in the value of training and certification in today’s rapidly-changing data-driven ...

#### Investigate Security and Threat Detection with VirusTotal and Splunk Integration

As security threats and their complexities surge, security analysts deal with increased challenges and ...