Knowledge Management

Reference a calculated field within another calculated field

RWL01
Engager

Is it possible to reference a calculated field within another calculated field?

My original search query was:

| eval SourceServer=substr(source, 11, 6)
| eval SourceInstance=case(
     SourceServer=="AAA001", "PROD", 
     SourceServer=="AAA002", "PROD", 
     SourceServer=="BBB001", "UAT", 
     SourceServer=="BBB002", "UAT",
    1=1, "UNKNOWN")

My intention was to create two calculated fields. However, it doesn't look like SourceServer is processed before SourceInstance is, so all of the Events resolve to "UNKNOWN".

My alternative is to repeat the same logic for each option in the case statement which seems pretty inefficient.

SourceInstance

case(
     substr(source, 11, 6)=="AAA001", "PROD", 
     substr(source, 11, 6)=="AAA002", "PROD", 
     substr(source, 11, 6)=="BBB001", "UAT", 
     substr(source, 11, 6)=="BBB002", "UAT",
    1=1, "UNKNOWN")

When I try to chain them together in a single field, like the simplified example below I get the error "Encountered the following error while trying to update: Unexpected token"

| eval foo=4 | eval bar=foo+4

Is there anyway to accomplish the logic of my original query without having to repeat the first query for every option of the case statement in the second query?

Tags (1)
0 Karma

MuS
Legend

Hi RWL01,

there must be something wrong somewhere else, because both examples work fine here:

First one:

| makeresults 
| eval SourceServer=substr("ZZZZZZZZZZAAA001", 11, 6) 
| eval SourceInstance=case(
    SourceServer=="AAA001", "PROD", 
    SourceServer=="AAA002", "PROD", 
    SourceServer=="BBB001", "UAT", 
    SourceServer=="BBB002", "UAT",
    1=1, "UNKNOWN")

alt text

As well as the second one:

alt text

cheers, MuS

0 Karma
Get Updates on the Splunk Community!

Splunk Observability Cloud’s AI Assistant in Action Series: Analyzing and ...

This is the second post in our Splunk Observability Cloud’s AI Assistant in Action series, in which we look at ...

Elevate Your Organization with Splunk’s Next Platform Evolution

 Thursday, July 10, 2025  |  11AM PDT / 2PM EDT Whether you're managing complex deployments or looking to ...

Splunk Answers Content Calendar, June Edition

Get ready for this week’s post dedicated to Splunk Dashboards! We're celebrating the power of community by ...