Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
Mira_io
Contributor II
Contributor II

if() in dimension returns false despite true condition — works fine in measure

Hi everyone,

I’m facing unexpected behavior with an if() statement in Qlik Sense when used in a dimension, and I’d really appreciate your help understanding it.

I have a variable vVariable1 that currently holds the value 1.

Here are two expressions I’m testing:

1. =if($(vVariable1)=1, 1, 0)

→ Returns 1, as expected.

2. =if($(vVariable1)=1, 1, if(len(Field_X)=0, NULL(), 1))

→ Returns NULL(), even though $(vVariable1)=1 should cause the expression to return 1 immediately.

Important detail:
This only happens when I use the expression in a dimension.
If I use the exact same logic in a measure, it works as expected — it returns 1 and does not evaluate the nested condition.

Mira_io_0-1747301781359.png

In the expression editor, Qlik correctly interprets this as:

Mira_io_2-1747301870408.png

So I would expect it to just return 1, regardless of what comes next.

My question:
Is this expected behavior in dimensions?
Does Qlik always evaluate both branches of an if() when used in dimensions — even if the condition is already satisfied?

And what would be the best way to structure such logic in a dimension to avoid this issue?

Thanks in advance for any insights!

Best regards,
Mira

Labels (2)
4 Replies
Or
MVP
MVP

1) Qlik always evaluates both the True and False condition if both are provided.

2) When used in a measure, your expression implicitly becomes:

=Only(if($(vVariable1)=1, 1, if(len(Field_X)=0, NULL(), 1)))

So that's what you want to be testing if you are cross-checking dimensions and measures.

I haven't had a chance to actually run the same scenario, so just general stuff here.

Mira_io
Contributor II
Contributor II
Author

Thanks a lot for the explanation — that really helps clarify things!

Just to double-check my understanding: you're saying that in a measure like
=Only(if($(vVariable1)=1, 1, if(len(Field_X)=0, NULL(), 1)))
the inner if is still evaluated fully (true and false parts), but because it's wrapped in Only(), I might not see any issues unless multiple results or NULLs are involved — is that correct?

In contrast, in a dimension, even if $(vVariable1)=1 is true, the second part still gets evaluated, and if Field_X is NULL or causes an error, it breaks the row, which is why I see unexpected NULLs even though the first condition should apply?

That would explain why it works fine as a measure but not as a dimension in my case. Let me know if I'm getting that right!

ali_hijazi
Partner - Master II
Partner - Master II

the calculated dimension always needs to have an aggregation
so your calculated dimension's expression would look like this:
aggr(only(if($(vVariable1)=1, 1, if(len(Field_X)=0, NULL(), 1)),Filed_X)

I can walk on water when it freezes
Or
MVP
MVP

Both sides get evaluated regardless, but that shouldn't impact which branch is actually picked. That said, evaluation errors can presumably break the formula, yes.

As mentioned by Ali, you will likely want to use some form of aggr() if you're using something like this in a calculated dimension.

Recommended reading:

For aggr():

https://community.qlik.com/t5/Design/The-Aggregation-Scope/ba-p/1467321

For If():

https://community.qlik.com/t5/Official-Support-Articles/Why-Is-It-Not-A-Good-Idea-To-Use-Nested-If-S...

https://qlikviewcookbook.com/2021/08/if-tips/

https://qlikviewcookbook.com/2014/12/how-to-choose-an-expression/