Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
hic
Former Employee
Former Employee

In Set expressions, an equals sign can be used to assign a selection to a field. But this is not the only assignment symbol that can be used. There are a couple of others. This post is about the alternative assignments symbols that can be used, and their respective use cases.

Strictly speaking, the equals sign in set expressions is not an equals sign. Rather, it is an assignment symbol. This is the reason why you cannot use relational operators in its place. The equals sign assigns a selection state to a field e.g.

     { $ <Country = {Sweden, Germany, 'United States'}> }

In this case, the element set ‘Sweden’, ‘Germany’, ‘United States’ is assigned as selection to the field “Country”.

But what if the set identifier already has a selection in the field “Country”?

In such a case, the old selection will be replaced by the new one. It will be like first clearing the old selection of the field, then applying a new selection.

However, this is not the only way to assign a set as selection in a field. You can also use assignments with implicit set operators. These will use the existing selection in the field to define a new selection:

Implicit Union:

     { $ <Country += {'United States'}> }

Note the plus sign.

This expression will use the union between the existing selection and ‘United States’ as new selection in the field, i.e. add the listed values to the previously select ones. The use case is not a common one, but it happens sometimes that you always want to show a specific country (or product or customer) as a comparison to the existing selection. Then the implicit union can be used.

Implicit Intersection:

     { $ <Country *= {"=Continent='Europe' "}> }

Note the asterisk.

This will use the intersection between the existing selection of countries and the countries in Europe as new selection in the field. (The search is an expression search that picks out European countries.) The set expression will not remove any part of the existing condition – instead it will just be an additional limitation.

This construction can in many cases be used instead of a normal assignment. In fact, it is often a much better solution than the standard assignment, since it respects the user selection and thus is easier to understand.

The implicit intersection should be used more.

Implicit Exclusion:

     { $ <Country -= {'United States'}> }

Note the minus sign.

This expression will use the existing selection, but exclude ‘United States’. The use case for an implicit exclusion is exactly this – you want to exclude a value. Hence, this construction is very useful.

Implicit Symmetric Difference:

     { $ <Country /= {'United States'}> }

Note the slash.

The above expression will select values that belong either to existing selection, or to the values in the set expression, but not to both. It’s like an XOR. I have never used this, and I would like to challenge the readers to come up with a relevant use case.

Bottom line: Assignments with implicit set operators are sometimes very useful. You should definitely consider using the implicit intersection and the implicit exclusion.

HIC

Further reading related to this topic:

A Primer on Set Analysis

Why is it called Set Analysis?

13 Comments
swuehl
MVP
MVP

Henric,

I just tried to come up with a challenge response.

I am a little unclear about

"The above expression will select values that belong neither to existing selection, nor to the values in the set expression."

Wouldn't this rather be

"union of the two sets excluding intersection", so the resulting set values should belong to either one, but not both?

10,502 Views
hic
Former Employee
Former Employee

You are of course right. Brain freeze.... I'll change it.

Old, incorrect: "that belong neither to existing selection, nor to the values in the set expression"

New, correct: "that belong either to existing selection, or to the values in the set expression, but not to both"

HIC

10,502 Views
nicolett_yuri

Honestly I never used the "/" in Set analysis

Nice post !

0 Likes
10,502 Views
tresesco
MVP
MVP

Henric/Stefan,

But if I select only the value that is in the set expression, it returns zero. I couldn't comprehend the same with the statement above. Am I missing something?

0 Likes
10,502 Views
hic
Former Employee
Former Employee

I assume you talk about the Implicit Symmetric Difference?

If so, when you select the same value as in the set expression, this value belongs to both element sets (the selection and the set expression) and should therefore not be included in the XOR. All other values belong to neither of the two element sets, and should therefore also not be included in the XOR. Hence zero.

HIC

10,502 Views
tresesco
MVP
MVP

Perfect !

Thanks for directing my comprehension to the right way. It was not too complex, but somehow I made it so when was thinking.

Brain freeze...

10,502 Views
Or
MVP
MVP

Challenge accepted.

This is sometimes useful for basket analysis, when you want to research the people who aren't following the traditional basket, e.g. people who are buying diapers or baby food, but for some reason not both. You can then figure out how to get these people to increase their basket diversity/size.

Of course, you could also get this done by using:

If(isnull(Sales of A) OR isnull(Sales of B), sum(Sales of A+Sales of B))

and hide nulls. But workarounds are easy for any of the advanced set operators, pretty much.

8,878 Views
Saravanan_Desingh

Great post HIC. Thank you.

0 Likes
8,878 Views
satishkurra
Specialist II
Specialist II

Hi HIC

Thanks for the article.

I liked the approach of using Slash

Thanks

Satish

0 Likes
8,878 Views
swuehl
MVP
MVP

I've just found another use case (still remembering the challenge):

If you want to find out if the user selected an exact set of values, no more and no less then listed, we can use

=NOT COUNT(DISTINCT {<FIELD /= {Value1, Value2, Value3} >} FIELD)

You can challenge back that we can use GetFieldSelections(FIELD) to get the same answer.

True, unless the user / developer might use some fancy search strings in FIELD list box...

8,878 Views