Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
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.
In the expression editor, Qlik correctly interprets this as:
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
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.
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!
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)
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://qlikviewcookbook.com/2021/08/if-tips/
https://qlikviewcookbook.com/2014/12/how-to-choose-an-expression/