Splunk Search

Product of a Column

kalverra
Engager

I'm trying to find a simple way to calculate the product of a single column, e.g.

value_a
0.44
0.25
0.67

Ideally, I could use something like this:

| stats product(value_a)

 But this doesn't seem possible.

Labels (1)
0 Karma
1 Solution

kalverra
Engager

Thank you everyone that replied with helpful comments. I however discovered a math trick with logarithms to accomplish this far more succinctly.

| eval log_value = ln(value_a)
| stats sum(log_value) AS log_sum
| eval product = exp(log_sum)

View solution in original post

kalverra
Engager

Thank you everyone that replied with helpful comments. I however discovered a math trick with logarithms to accomplish this far more succinctly.

| eval log_value = ln(value_a)
| stats sum(log_value) AS log_sum
| eval product = exp(log_sum)

tscroggins
Champion

Now you're Splunking! Watch out for approximation errors!

2 * 3 * 4 = 24
exp(ln(2) + ln(3) + ln(4)) ~= 23.999999999999993

0 Karma

yuanliu
SplunkTrust
SplunkTrust

Another approach is to (almost) use stats:-) as you original proposed.  With a little help of foreach.

 

| stats values(value_a) as value_a
| eval product = 1
| foreach value_a mode=multivalue
    [eval product = product * <<ITEM>>]

 

Here is an emulation you can play with and compare with real data:

 

| makeresults format=csv data="value_a
0.44
0.25
0.67"
``` data emulation above ```

 

The above search gives this result

value_a
product
0.25
0.44
0.67
0.074
Tags (1)
0 Karma

tscroggins
Champion

I would use list() instead of values() to prevent removal of duplicates and wrap the product in exact() to prevent rounding errors:

| makeresults format=csv data="value_a
0.44
0.25
0.67
0.44" 
| stats list(value_a) as value_a
| eval "product(value_a)"=1
| foreach value_a mode=multivalue [ eval "product(value_a)"=exact('product(value_a)' * <<ITEM>>) ]
| table "product(value_a)"

 =>

product(value_a)
0.032428
0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Correct, you can't multiply down columns, but you can with rows, so depending on how many events and columns you want the product of, you could do something like this

| makeresults format=csv data="value_a,value_b,otherValue
0.44,1,10
0.25,2,20
0.67,3,30"
| appendpipe
    [| transpose 0
    | eval product = 1
    | foreach row*
        [eval product = product * '<<FIELD>>']
    | eval {column} = product
    | stats values(value_a) as value_a values(value_b) as value_b]
0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Index This | What travels the world but is also stuck in place?

April 2026 Edition  Hayyy Splunk Education Enthusiasts and the Eternally Curious!   We’re back with this ...

Discover New Use Cases: Unlock Greater Value from Your Existing Splunk Data

Realizing the full potential of your Splunk investment requires more than just understanding current usage; it ...

Continue Your Journey: Join Session 2 of the Data Management and Federation Bootcamp ...

As data volumes continue to grow and environments become more distributed, managing and optimizing data ...