Splunk Search

How to add a conditional where based on the field existence?

jieli
New Member

mvexpand metrics | spath input=metrics | rename "cityCode" as pcc | where if($selected_pcc|s$="all",like(pcc,"%"),like(pcc,$selected_pcc|s$)) | stats count as Total

I use the command above to filter the result by looking into the json field cityCode, and verify if the value equals to my dropdown value selection, by default all cityCode would be included (%).
The issue is when the message does not have the cityCode field, the default select All cityCode will not work since the like (pcc,"%") would fail.

Currently, the conditional selection is inside the where clause, Is there a way to do conditional selection outside the where clause, meaning if I did not select cityCode, the where clause should be ignored completely.

0 Karma
1 Solution

manjunathmeti
SplunkTrust
SplunkTrust

hi @jieli,

Try this. Here pcc will be set to "" values when cityCode not exists in the events and like matches "" values.

mvexpand metrics | spath input=metrics | eval pcc = if(isnotnull(cityCode), cityCode, "") | where if($selected_pcc|s$="all",like(pcc,"%"),like(pcc,$selected_pcc|s$)) | stats count as Total

View solution in original post

manjunathmeti
SplunkTrust
SplunkTrust

hi @jieli,

Try this. Here pcc will be set to "" values when cityCode not exists in the events and like matches "" values.

mvexpand metrics | spath input=metrics | eval pcc = if(isnotnull(cityCode), cityCode, "") | where if($selected_pcc|s$="all",like(pcc,"%"),like(pcc,$selected_pcc|s$)) | stats count as Total
Get Updates on the Splunk Community!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...