Splunk Search

Get first value of a field after filtering its row out

pedropiin
Path Finder

Hi everyone.

I'm really new to Splunk, so I'm confused with what seems to be a simple problem. 
I'm using "where row_num > 1" to remove the first row of my search, as I need to calculate lots of metrics based on the whole data without this specific first row. But I'm also supposed to show the value of this first row in a specific field. 

My query is of the following structure:

my_search...
| eval val1=...
| sort val1
| streamstats count as row_num
| where row_num > 1
| stats avg(...) as metric1,
  max(...) as metric2,
  count(...) as metric3
...
| fields metric1, metric2, metric3

But I also need to output the value 'x' that is specifically on field 'y' on row 1. 

How would I do this? 

Thanks in advance

 

Labels (2)
0 Karma
1 Solution

livehybrid
SplunkTrust
SplunkTrust

Hi @pedropiin 

Im not 100% sure if I'm on the right page...but nevertheless have a look to see if this helps you 🙂

Ive used some sample data inline to help build out a working query for you to work with.

| makeresults 
| eval _raw = "[{\"name\":\"SampleMetric\",\"metric\":\"42\",\"xfield\":\"Mickey Mouse\"},{\"name\":\"SampleMetric\",\"metric\":\"17\",\"xfield\":\"Donald Duck\"},{\"name\":\"SampleMetric\",\"metric\":\"36\",\"xfield\":\"Goofy\"},{\"name\":\"SampleMetric\",\"metric\":\"29\",\"xfield\":\"Minnie Mouse\"},{\"name\":\"SampleMetric\",\"metric\":\"48\",\"xfield\":\"Pluto\"}]" 
| eval events=json_array_to_mv(_raw) 
| mvexpand events 
| eval _raw=events 
| fields _raw 
| spath 
| table name metric xfield
    ``` Sample data Prep work complete ``` 
| sort metric 
| streamstats count as row_num 
| eval xfield=IF(row_num==1,xfield,NULL()) 
| filldown xfield 
| where row_num > 1 
| stats values(xfield) as xfield,
    avg(metric) as metric1,
    max(metric) as metric2,
    count(metric) as metric3 
| fields xfield metric1, metric2, metric3

livehybrid_0-1739828929560.png

Please let me know how you get on and consider accepting this answer or adding karma this answer if it has helped.
Regards

Will

View solution in original post

livehybrid
SplunkTrust
SplunkTrust

Hi @pedropiin 

Im not 100% sure if I'm on the right page...but nevertheless have a look to see if this helps you 🙂

Ive used some sample data inline to help build out a working query for you to work with.

| makeresults 
| eval _raw = "[{\"name\":\"SampleMetric\",\"metric\":\"42\",\"xfield\":\"Mickey Mouse\"},{\"name\":\"SampleMetric\",\"metric\":\"17\",\"xfield\":\"Donald Duck\"},{\"name\":\"SampleMetric\",\"metric\":\"36\",\"xfield\":\"Goofy\"},{\"name\":\"SampleMetric\",\"metric\":\"29\",\"xfield\":\"Minnie Mouse\"},{\"name\":\"SampleMetric\",\"metric\":\"48\",\"xfield\":\"Pluto\"}]" 
| eval events=json_array_to_mv(_raw) 
| mvexpand events 
| eval _raw=events 
| fields _raw 
| spath 
| table name metric xfield
    ``` Sample data Prep work complete ``` 
| sort metric 
| streamstats count as row_num 
| eval xfield=IF(row_num==1,xfield,NULL()) 
| filldown xfield 
| where row_num > 1 
| stats values(xfield) as xfield,
    avg(metric) as metric1,
    max(metric) as metric2,
    count(metric) as metric3 
| fields xfield metric1, metric2, metric3

livehybrid_0-1739828929560.png

Please let me know how you get on and consider accepting this answer or adding karma this answer if it has helped.
Regards

Will

pedropiin
Path Finder

Hi @livehybrid 

It worked perfectly! 

Thank you so much

0 Karma
Get Updates on the Splunk Community!

[Puzzles] Solve, Learn, Repeat: Dynamic formatting from XML events

This challenge was first posted on Slack #puzzles channelFor a previous puzzle, I needed a set of fixed-length ...

Enter the Agentic Era with Splunk AI Assistant for SPL 1.4

  🚀 Your data just got a serious AI upgrade — are you ready? Say hello to the Agentic Era with the ...

Stronger Security with Federated Search for S3, GCP SQL & Australian Threat ...

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