Skip to main content
Announcements
Introducing Qlik Answers: A plug-and-play, Generative AI powered RAG solution. READ ALL ABOUT IT!
cancel
Showing results for 
Search instead for 
Did you mean: 
RobinB1
Contributor
Contributor

Usage of AND and OR in same IF statement

Hi,

 

So, in a LOAD script you can do IF-AND and IF-OR like:

IF(A=1 AND B=1, 1, 0) as _flagTest

IF(B=1 OR C=1, 1, 0) as _flagTest

 

For cases where we need to use both logical operators, as far as I know you would have to get nesty, so for example like:

IF(A=1 and B=1, 1, 

    IF(C=1, 1, 0)) as _flagTest

 

Question: is there an unnested way? Like:

IF(A=1 and B=1 or C=1, 1, 0) as _flagTest

 

Can QlikView evaluate this? If so, which operator takes precedence? I mean, like:

A

B

C

_flagTest

1

1

<>1

1

1

<>1

1

1

<>1

1

1

0

1

<>1

<>1

0

<>1

1

<>1

0

<>1

 <>1

 1

0

 

…or like:

 

A

B

C

_flagTest

1

1

<>1

1

1

<>1

1

1

<>1

1

1

1

1

<>1

<>1

0

<>1

1

<>1

0

<>1

 <>1

 1

1

 

?

 

Thanks,

Robin

1 Solution

Accepted Solutions
Or
MVP
MVP

There shouldn't be a problem with this. Ideally, you should parenthesize to tell Qlik what the parsing order, for example:

if((1 = 2 AND 3 = 4) OR 5 =5,'Yes','No')  = Yes

if(1 = 2 AND (3 = 4 OR 5 =5),'Yes','No')  = No

Note that without parenthesizing:

if(1 = 2 AND 3 = 4 OR 5 =5,'Yes','No') = Yes

 

View solution in original post

3 Replies
Or
MVP
MVP

There shouldn't be a problem with this. Ideally, you should parenthesize to tell Qlik what the parsing order, for example:

if((1 = 2 AND 3 = 4) OR 5 =5,'Yes','No')  = Yes

if(1 = 2 AND (3 = 4 OR 5 =5),'Yes','No')  = No

Note that without parenthesizing:

if(1 = 2 AND 3 = 4 OR 5 =5,'Yes','No') = Yes

 

RobinB1
Contributor
Contributor
Author

Wizard of Or, thanks for the input,



Splendid piece of info that parenthesizing work in that way. Out of curiosity, your parenthesis-less last example indicates that parsing order is just based on execution left to right? Nothing like difference in force between AND/OR?

 

Thanks,

Robin

Or
MVP
MVP

I've never given it too much thought, to be honest. Even if I knew and remembered what happened in this scenario, I find it difficult to read and would much rather have the logic explicitly stated via parenthesizing. That said, you should be able to test this fairly easily if you want to.