Examples of Alternate States in Chart Expressions

These examples are intended to show best practices regarding the use of alternate states by QlikView Developers and/or extreme QlikView Power-Users. These examples can be found in the file: ‘Alternate States Functionality.qvw’

Synchronizing Selections between States

The following expressions can be used in a single chart:

count({$} DISTINCT [Invoice Number])

count({State1} DISTINCT [Invoice Number])

count({State2} DISTINCT [Invoice Number])

There is a problem with this method; the QlikView Developer must duplicate the selections (list boxes and multi boxes) for all three states so that the end-user can make appropriate selections for the various states. In many situations the QlikView Developer will want to have a set of ‘common’ selections available to all states. This will allow the end user to set the context for the various charts and then make use of specific selections to show the differences between states. Set Analysis can be used with states to keep certain selections consistent between states.

count({State1<Year = $::Year, Month = $::Month>} DISTINCT [Invoice Number])

count({State2<Year = $::Year, Month = $::Month>} DISTINCT [Invoice Number])

The QlikView Developer will keep the Year and Month selections in State1 and State2 synchronized with the Year and Month selections in the default state. The QlikView Developer can add elements to the set modifiers as necessary in order to keep even more fields consistent between states.

Set Operators

It is possible to use set operators (+, *, -, /) with states. The following expressions are valid and will count the distinct invoice numbers that are in either the default state or State1.

Examples:  

count({$ + State1} DISTINCT [Invoice Number])
counts the distinct invoice numbers in the union of the <default> state and State1.

count({1 - State1} DISTINCT [Invoice Number])
counts the distinct invoice numbers not in State1.

count({State1 * State2} DISTINCT [Invoice Number])
counts the distinct invoice numbers in that are in both the <default> state and State1.

Note: Caution should be used when using set operators in this manner. In some situations the result will not be what is expected. This is because the selections in a given state generate a set of data that may not be fully compatible with the set(s) it is being combined with. This is especially true as the complexity of the data model increases.

Implicit Field Value Definitions

Another way of using set operators is with the element functions P() and E(). These functions are only available within set expressions.

Examples:  

count({$<[Invoice Number] = p({State1} [Invoice Number])>} DISTINCT [Invoice Number])
This expression counts the distinct invoice numbers in the <default> state based on the possible invoice numbers available in State1.

It is almost, but not quite equivalent to the following expression:

count({$<[Invoice Number] = State1::[Invoice Number]>} DISTINCT [Invoice Number])

The difference between the expressions is that in the first one, the possible values on invoice number from State1 are passed into the default state. In the second expression, the selected values from State1 are passed into the default state. If the user has not made any invoice number selections in State1, no invoice number values will be passed into the default state.

Set operators are best used within set modifiers. The following expression finds the intersection of the possible invoice numbers from State1 and State2 and passes those values into the invoice numbers in the default state.

Examples:  

count({$<[Invoice Number] = p({State1} [Invoice Number]) * p({State2} [Invoice Number])>} DISTINCT [Invoice Number])
The expression to find the intersection of invoice numbers in the <default> state and State1 is:

count({$<[Invoice Number] = p({$} [Invoice Number]) * p({State1} [Invoice Number])>} DISTINCT [Invoice Number])

This expression may seem confusing because it uses a state (<default> in this instance) in both the element function and a set identifier. Keep in mind that the element function p($) is returning the possible values in the default state. The set/state identifier {$} is being modified by the result of the element functions. Any invoice selections currently existing in the default state are being overridden by the values from the intersection of the element functions.

It should be noted that the above expression is still not completely correct as it fails to synchronize the common selections between the default state and State1. Below is an expression that will do so:

Examples:  

count({$<[Invoice Number] = p({$} [Invoice Number]) * p({State1<Year = $::Year, Month = $::Month>} [Invoice Number])>} DISTINCT [Invoice Number])

As before, the QlikView Developer can add fields into the modifier to keep selections consistent between various states.

Did this information help you?

Thanks for letting us know. Is there anything you'd like to tell us about this topic?

Can you tell us why it did not help you and how we can improve it?