Splunk Search

eval case like only populates first row of evaluated field

ohlafl
Communicator

I have the following query:

city=* store=*  | stats values(store) by city | eval Role=case(store LIKE "%frt%", "FT", store LIKE "%byt%", "BT", store LIKE "%bea%", "BA", store LIKE "%gwt%", "GT")

This results in:

city                store                role

london                "HT10gwt1"                  GT
                      "HT55gwt2"                              
                      "Ogwt22"                            
                      "ASbet44" 

paris                 "PPgwt11"                  BT
                      "OBbea2"                        
                      ...                         
                      ... etc

Why isn't role getting evaluated properly and getting populated for each store? What am I doing wrong? Every store should have a role

Tags (4)
1 Solution

woodcock
Esteemed Legend

The answer is because field store is now a mutlivalued field and case exits once the first condition is true so you need something that evaluates on every test. This will work:

 city=* store=*  | stats values(store) by city | eval Role=mvappend(if(like(store, "%frt%"), "FT", null()), if(like(store, "%byt%"), "BT", null()), if(like(store, "%bea%"), "BA", null()), if(like(store, "%gwt%"), "GT", null()))

But I suspect you will be displeased with that and are probably looking for this:

 city=* store=*  | stats values(store) by city | mvexpand store | eval Role=case(store LIKE "%frt%", "FT", store LIKE "%byt%", "BT", store LIKE "%bea%", "BA", store LIKE "%gwt%", "GT")

View solution in original post

woodcock
Esteemed Legend

The answer is because field store is now a mutlivalued field and case exits once the first condition is true so you need something that evaluates on every test. This will work:

 city=* store=*  | stats values(store) by city | eval Role=mvappend(if(like(store, "%frt%"), "FT", null()), if(like(store, "%byt%"), "BT", null()), if(like(store, "%bea%"), "BA", null()), if(like(store, "%gwt%"), "GT", null()))

But I suspect you will be displeased with that and are probably looking for this:

 city=* store=*  | stats values(store) by city | mvexpand store | eval Role=case(store LIKE "%frt%", "FT", store LIKE "%byt%", "BT", store LIKE "%bea%", "BA", store LIKE "%gwt%", "GT")

ohlafl
Communicator

Thank you, the later example worked as expected but it would be really neat if the result would group by city (as of now each store is within its on cell which I guess is expected since you did an mvexpand). The first example did more lines of role but not for all stores and not always the right role.

0 Karma

woodcock
Esteemed Legend

This will do it:

city=* store=*  | stats values(store) by city | mvexpand store | eval Role=case(store LIKE "%frt%", "FT", store LIKE "%byt%", "BT", store LIKE "%bea%", "BA", store LIKE "%gwt%", "GT") | stats list(store) AS store list(Role) AS role BY city
0 Karma

ohlafl
Communicator

I actually have a new question in relation to this, I want to expand the chart by giving each store a Status, the value of "Status" is defined by a field within the event (1 = open, 0 = closed). I would of thought that something like...

city=* store=*  | stats values(store) by city | mvexpand store | eval Role=case(store LIKE "%frt%", "FT", store LIKE "%byt%", "BT", store LIKE "%bea%", "BA", store LIKE "%gwt%", "GT") | eval Status=case(openStatus=1, "Open", openStatus=0, "Closed") | stats list(store) AS store list(Role) AS role list(Status) as Status BY city

... would work but it doesn't, it seems as if the value of openStatus is not properly passed, what am I missing?

0 Karma

woodcock
Esteemed Legend

Try this (upvote this comment if it works):

city=* store=*  | stats values(store) by city | mvexpand store | eval Role=case(store LIKE "%frt%", "FT", store LIKE "%byt%", "BT", store LIKE "%bea%", "BA", store LIKE "%gwt%", "GT") | eval Status=case(openStatus=1, "Open", openStatus=0, "Closed") | stats list(store) AS store list(Role) AS role list(Status) as Status BY city
0 Karma

ohlafl
Communicator

Sorry I was really tired yesterday, the "eval Status=" was supposed to be in the original comment as it makes no difference.

0 Karma

woodcock
Esteemed Legend

Then you do not have a field called openStatus; check you fields and names (casing).

0 Karma

ohlafl
Communicator

The weird thing is that I absolutely do, I have checked and double checked an even though there are existing events with that field in the exact writing the above code will not evaluate and produce the wanted result.

Edit: It appears as if I can only evaluate fields mentioned within the query such as city, store and role, but when I reference fields that I do know exist in the events and that are parsed as fields in Splunk nothing happens. Even running an [ eval Status=if(openStatus>-1, "yes", "no") ] always produces "no"...

0 Karma

ohlafl
Communicator

Awesome. Worked like a charm. Thank you.

0 Karma
Get Updates on the Splunk Community!

Introducing Splunk Enterprise 9.2

WATCH HERE! Watch this Tech Talk to learn about the latest features and enhancements shipped in the new Splunk ...

Adoption of RUM and APM at Splunk

    Unleash the power of Splunk Observability   Watch Now In this can't miss Tech Talk! The Splunk Growth ...

Routing logs with Splunk OTel Collector for Kubernetes

The Splunk Distribution of the OpenTelemetry (OTel) Collector is a product that provides a way to ingest ...