Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
gmenoutis
Partner - Creator II
Partner - Creator II

Performance: if() vs variable fieldname in expression

I will be using code that might have slight syntactic errors, but I hope it will be sufficient to express my question.


If in some expression I use SUM(fieldname*5+2), the formula inside the parenthesis must obviously be evaluated for each fieldname value.


I guess this is the same in the case of SUM(if(fieldname<5,fieldname*3,fieldname/5)).



However, a variable has a constant value throughout all the data. So, one question is if the following: Will


SUM(if(varname<5,fieldnameA,fieldnameB))


be evaluated for each A-B combination, or is qlikview smart enough to evaluate the if condition once and then replace it by whichever field the condition leads to?



If the answer is "unfortunately, no", how about the following:


SUM(&(varname))


where in the script I have:


set varname=if(varname<5,'fieldnameA','fieldnameB')



Will this be evaluated once, as the formula text is not resolved from the start?


I hope, and believe, this must be "yes", since else how would qv know for what data combinations it would have to evaluate the final expression?



If I have been vague or there is any way to improve this post, please let me know and have a chance to make it right!

1 Solution

Accepted Solutions
tresesco
MVP
MVP

The variable evaluation might not happen row-wise (based on it's definition), however, the check of TRUE/FALSE in IF clause would get evaluated row-wise. Instead, for such case the below structure would be a winner may be:

If( variable<5, Sum(FieldA), Sum(FieldB))

Or, may be like second way you have shown.

View solution in original post

2 Replies
sunny_talwar

Not full sure I understand where you are using this, but AFAIK anytime you use an if statement (through a variable or directly) it will be evaluated on row by row basis and it will evaluate both the true and false condition before it displays the output for either of the condition.

tresesco
MVP
MVP

The variable evaluation might not happen row-wise (based on it's definition), however, the check of TRUE/FALSE in IF clause would get evaluated row-wise. Instead, for such case the below structure would be a winner may be:

If( variable<5, Sum(FieldA), Sum(FieldB))

Or, may be like second way you have shown.