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

Announcements
Join us to spark ideas for how to put the latest capabilities into action. Register here!
cancel
Showing results for 
Search instead for 
Did you mean: 
H-Zey
Contributor II
Contributor II

Set Expression vs If Statement

I have a Bar chart with an expression that contains a working if Statement:

Count(DISTINCT if( (Field_X = Field_A and Field_Y = Field_B and COUNTRY = 'USA' and TYPE = 'Val'),ID) )

If all the conditions are true I get back a distinct count of IDs.

I have two questions:
(1) Can this be re-written using Set Expression?

I tried re-writing it to the following statement but I'm not getting the correct results. Most likely the use of identifiers and operands are incorrect:

Count(DISTINCT { <Field_X = Field_A>+$1<Field_Y = Field_B>+$2<COUNTRY = {'USA'}>+$3<TYPE = {'Val'}>>} ID)

Any help is greatly appreciated

(2)  Is there a difference - performance-wise between the If condition and the set expression or are both the same?

 

Thanks

Labels (1)
1 Solution

Accepted Solutions
sidhiq91
Specialist II
Specialist II

@H-Zey Yes you can definitely write the above if expression in the form of Set analysis. Please see below.

Count(DISTINCT if( (Field_X = Field_A and Field_Y = Field_B and COUNTRY = 'USA' and TYPE = 'Val'),ID) )

Can be replaced by

Count(distinct {<Field_X={'Field_A'},Field_Y={'Field_B'},COUNTRY={'USA'},TYPE={'VAL'}>}ID)

Secondly Set expression are the preferred choice for writing Complex expression that contains if conditions.

If this answers your question, kindly like and accept it as your solution.

View solution in original post

3 Replies
sidhiq91
Specialist II
Specialist II

@H-Zey Yes you can definitely write the above if expression in the form of Set analysis. Please see below.

Count(DISTINCT if( (Field_X = Field_A and Field_Y = Field_B and COUNTRY = 'USA' and TYPE = 'Val'),ID) )

Can be replaced by

Count(distinct {<Field_X={'Field_A'},Field_Y={'Field_B'},COUNTRY={'USA'},TYPE={'VAL'}>}ID)

Secondly Set expression are the preferred choice for writing Complex expression that contains if conditions.

If this answers your question, kindly like and accept it as your solution.

H-Zey
Contributor II
Contributor II
Author

Hi @sidhiq91 ,

Yes! it did work - thank you.  I see that you combined all the field all into one set inside the '< > ' tags. 

Is that done to treat it as an And? 

Will accept it as the solution

Thanks again

sidhiq91
Specialist II
Specialist II

@H-Zey  Yes you are right. Between {} it is treated as AND. I would recommend you to study Set Analysis Topic from our Qlik Community which will help you resolve many issues.

Happy Learning!