Splunk Search

## How do you calculate the average across columns in a table?

Path Finder

Hello Splunkers

I tried a few of the suggested solutions, but none of them got me where I need to be, so i'm asking the larger group.

I'm trying to get the average across columns. Some of my columns contain null values, so i want to make sure that doesn't throw off my count, which is why i'm not simply adding each and dividing by the column number. With the data below, what's the best approach to getting an average across columns, with some having nulls. Sample data and results shown below. Thanks in advance

``````location  col1    col2         col3     col4     average_here
Florida      7    null_here       0        2            3
New York     3       0            0        2            1.25
Texas       10      23        null_here    3            12
``````
Tags (3)
1 Solution
Legend

@splunker1981 ,

Try this,

``````"Your base search to get fields"|addtotals|eval colcount=-1
|foreach col* [eval colcount=if(isnull(<<FIELD>>) OR <<FIELD>>=="",colcount,colcount+1)]
|eval avg=exact(Total/colcount)|fields - Total,colcount
``````

Added conditions for both null and empty string in `if(isnull(<<FIELD>>) OR <<FIELD>>==""` . If you have real null() in place, then you can remove `OR <<FIELD>>==""`

---
What goes around comes around. If it helps, hit it with Karma 🙂
Legend

@splunker1981 ,

Try this,

``````"Your base search to get fields"|addtotals|eval colcount=-1
|foreach col* [eval colcount=if(isnull(<<FIELD>>) OR <<FIELD>>=="",colcount,colcount+1)]
|eval avg=exact(Total/colcount)|fields - Total,colcount
``````

Added conditions for both null and empty string in `if(isnull(<<FIELD>>) OR <<FIELD>>==""` . If you have real null() in place, then you can remove `OR <<FIELD>>==""`

---
What goes around comes around. If it helps, hit it with Karma 🙂
Get Updates on the Splunk Community!

#### What's New in Splunk Cloud Platform 9.2.2403?

Hi Splunky people! We are excited to share the newest updates in Splunk Cloud Platform 9.2.2403! Analysts can ...

#### Stay Connected: Your Guide to July and August Tech Talks, Office Hours, and Webinars!

Dive into our sizzling summer lineup for July and August Community Office Hours and Tech Talks. Scroll down to ...

#### Edge Processor Scaling, Energy & Manufacturing Use Cases, and More New Articles on ...

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