Qlik Community

Qlik Design Blog

All about product and Qlik solutions: scripting, data modeling, visual design, extensions, best practices, etc.

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
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?

3,978 Views

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

3,978 Views

Honestly I never used the "/" in Set analysis

Nice post !

0 Likes
3,978 Views
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
3,978 Views

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

3,978 Views
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...

3,978 Views