Splunk Search

If-statement does not work

HeinzWaescher
Motivator

Hi,

my search looks like this:

...
| eval month=strftime(_time, "%Y_%m")
| chart dc(user_id) as count by user_id, month
| stats dc(eval(if(2013_11="1" AND 2013_12="1", user_id, null()))) AS output

The stats command does not work and I don't know why

"Error in 'stats' command: The eval expression for dynamic field 'eval(if(2013_11="1", account_id, null()))' is invalid. Error='The expression is malformed. Expected ).' "

I've used such a command many times before and Splunk never complained about it.

BR

Heinz

Tags (2)
0 Karma
1 Solution

martin_mueller
SplunkTrust
SplunkTrust

Non-standard field names have to be enclosed in single quotes for where and eval:

... | stats dc(eval(if('2013_11'="1" AND '2013_12'="1", user_id, null()))) AS output

View solution in original post

martin_mueller
SplunkTrust
SplunkTrust

Non-standard field names have to be enclosed in single quotes for where and eval:

... | stats dc(eval(if('2013_11'="1" AND '2013_12'="1", user_id, null()))) AS output

HeinzWaescher
Motivator

thank you very much!

0 Karma

HeinzWaescher
Motivator

I just tried out another apporach and it seems to work fine:

...
| eval month=strftime(_time, "%Y_%m")
| stats values(month) AS months by user_id
| stats dc(eval(if(months="2013_11" AND months="2013_12", user_id, null()))) AS users
0 Karma

HeinzWaescher
Motivator

In the end, I want to have a chart that shows the number of active users in 2013_12, who have been active in 2013_11 as well.

I'm wondering, why it is not possible to use it in the if command? Using it as " | search 2013_01="1" is working, so Splunk seems to know that it is the fieldname here

0 Karma

HeinzWaescher
Motivator

These are the column names after the chart command. The Stats command should count number the users if the value in both columns is "1"

0 Karma

strive
Influencer

Why are you using 2013_11 and 2013_12 in if condition. Could you please tell us what are you trying there?
You need to use month in if, not its value. If you give us more details we can help you to write the search

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Data Drivers: How We're Streaming Real-Time F1 Telemetry Directly into Splunk ...

Data Drivers: Every Lap Tells a Story The Spectacle Two F1 racing sims go head-to-head on the .conf26 show ...

Data Management Digest – July 2026

  Welcome to the July 2026 edition of Data Management Digest! As your trusted partner in data innovation, the ...

Announcing Modern Navigation: A New Era of Splunk User Experience

We are excited to introduce the Modern Navigation feature in the Splunk Platform, available to both cloud and ...