Splunk Search

Append number by value to results

dataisbeautiful
Communicator

Hi Splunkers

I'm looking for a way to append a column with an ID based on the value of another field.

Base search gives this

 

index=transactionlog sourcetype=transaction earliest=-1h@h latest=@h
| table _time Item Count

 

datetime=2025-03-10T08:59:59 Item=Apple Count=8
datetime=2025-03-10T08:59:45 Item=Banana Count=2
datetime=2025-03-10T08:58:39 Item=Apple Count=5
datetime=2025-03-10T08:58:25 Item=Coconut Count=1
datetime=2025-03-10T08:57:36 Item=Banana Count=2

 

 

_timeItemCount
...Apple8
...Banana2
...Apple5
...Coconut1
...Banana2


I'd like something that gives this

_timeItemCountID
...Apple81
...Banana22
...Apple51
...Coconut13
...Banana22

 

The ID is local, based only on the results. Each unique item is numbered.

 

I've tried streamstats count but this doesn't give the desired results.

 

Thanks all

 

Labels (2)
0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust
| streamstats dc(Item) as ID
| eventstats min(ID) as ID by Item

View solution in original post

ITWhisperer
SplunkTrust
SplunkTrust
| streamstats dc(Item) as ID
| eventstats min(ID) as ID by Item

dataisbeautiful
Communicator

@ITWhisperer That's exactly what I was after, thank you.

 

Thanks also @gcusello @meetmshah 

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @dataisbeautiful ,

good for you, see next time!

Ciao and happy splunking

Giuseppe

P.S.: Karma Points are appreciated by all the contributors 😉

0 Karma

dataisbeautiful
Communicator

@gcuselloI've updated my post with the base search and raw data

@meetmshahthe ID is not in the raw data, it is something I am adding only at time of search

I'm not interested in maintaining an ID between times, it's going to be used in a visualisation on a deashboard.

 

 

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @dataisbeautiful ,

in other words, you need to add a progressive number to your results, is it correct?

if this is your requirement, please try this:

index=transactionlog sourcetype=transaction earliest=-1h@h latest=@h
| eval counter=1
| accum counter AS ID
| fields - counter
| table _time Item Count ID

Ciao,

Giuseppe

0 Karma

dataisbeautiful
Communicator

Hi @gcusello 

Thanks for your suggestion, I've run this

 

index=transactionlog sourcetype=transaction earliest=-1h@h latest=@h
| eval counter=1
| accum counter AS ID
| fields - counter
| table _time Item Count ID

 

It gives the output

_timeItemCountID
...Apple81
...Banana22
...Apple53
...Coconut14
...Banana25

 

This isn't what I'm after as Apple doesn't have a single ID now.

I'd like a single ID per unique value of Item, not a row counter ID. Hope that makes sence.

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @dataisbeautiful ,

ok, please try this:

index=transactionlog sourcetype=transaction earliest=-1h@h latest=@h
| join type=left Item 
     [ search index=transactionlog sourcetype=transaction earliest=-1h@h latest=@h
     | stats count BY Item
     | eval counter=1
     | accum counter AS ID
     | table Item ID ]
| table _time Item Count ID

Ciao.

Giuseppe

gcusello
SplunkTrust
SplunkTrust

Hi @dataisbeautiful ,

could you share your search and a sample of your data (both using "Add/Edit Code Sample" button not a screenshot)?

Ciao.

Giuseppe

0 Karma

meetmshah
SplunkTrust
SplunkTrust

Hello @dataisbeautiful, You can just add the other field name afte the by clause?

can you give the current search which you are using and confirm if the ID field that you want to add is in the events itself?

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.
Get Updates on the Splunk Community!

.conf25 Global Broadcast: Don’t Miss a Moment

Hello Splunkers, .conf25 is only a click away.  Not able to make it to .conf25 in person? No worries, you can ...

Observe and Secure All Apps with Splunk

 Join Us for Our Next Tech Talk: Observe and Secure All Apps with SplunkAs organizations continue to innovate ...

What's New in Splunk Observability - August 2025

What's New We are excited to announce the latest enhancements to Splunk Observability Cloud as well as what is ...