Splunk Search

using EVAL to calculate daily average with subsearch

etotman
Explorer

I'm trying to calculate a daily average using the eval command. The search below produces two numerical fields Total and Total2, but the eval command at the end does not produce a result. There is no dailyavg variable. What did I do wrong?

source="report.csv" earliest=-27d@h latest=-26d@h | eventstats sum(SpaceUsed_Total) as Total | append [search source="report.csv" earliest=-1d | eventstats sum(SpaceUsed_Total) as Total2] | eval dailyavg = (Total2-Total)/26 | top dailyavg

Tags (3)
0 Karma
1 Solution

acharlieh
Influencer

The issue is you wind up with no events with both Total and Total2.

source="report.csv" earliest=-27d@h latest=-26d@h | eventstats sum(SpaceUsed_Total) as Total
| append [search source="report.csv" earliest=-1d | eventstats sum(SpaceUsed_Total) as Total2]
| eval dailyavg = (Total2-Total)/26 | top dailyavg

The first line will add a Total field to every event. You'll then add events from the second search, and put Total2 on each of those. Then when dailyavg is calculated you there are no events with both, so dailyavg is null. I'm not quite sure if I follow your logic, but you may want to try:

source="report.csv" earliest=-27d@h latest=-26d@h | stats sum(SpaceUsed_Total) as Total
| append [search source="report.csv" earliest=-1d | stats sum(SpaceUsed_Total) as Total2]
| stats first(*) as * | eval dailyavg = (Total2-Total)/26

View solution in original post

acharlieh
Influencer

The issue is you wind up with no events with both Total and Total2.

source="report.csv" earliest=-27d@h latest=-26d@h | eventstats sum(SpaceUsed_Total) as Total
| append [search source="report.csv" earliest=-1d | eventstats sum(SpaceUsed_Total) as Total2]
| eval dailyavg = (Total2-Total)/26 | top dailyavg

The first line will add a Total field to every event. You'll then add events from the second search, and put Total2 on each of those. Then when dailyavg is calculated you there are no events with both, so dailyavg is null. I'm not quite sure if I follow your logic, but you may want to try:

source="report.csv" earliest=-27d@h latest=-26d@h | stats sum(SpaceUsed_Total) as Total
| append [search source="report.csv" earliest=-1d | stats sum(SpaceUsed_Total) as Total2]
| stats first(*) as * | eval dailyavg = (Total2-Total)/26
Get Updates on the Splunk Community!

Splunk Observability for AI

Don’t miss out on an exciting Tech Talk on Splunk Observability for AI!Discover how Splunk’s agentic AI ...

Splunk Enterprise Security 8.x: The Essential Upgrade for Threat Detection, ...

Watch On Demand the Tech Talk on November 6 at 11AM PT, and empower your SOC to reach new heights! Duration: ...

Splunk Observability as Code: From Zero to Dashboard

For the details on what Self-Service Observability and Observability as Code is, we have some awesome content ...