A little while ago, I was asked to change an existing QlikView Application. While scanning the application, I found that it used Alternate States. I had already heard about Alternate States, and that they could store different user selections, but didn’t have any hands-on experience yet. I decided to dig a bit deeper into the subject before continuing. I wrote down what I learned in this post
What are the default selection state and alternate states?
By default, user selections in QlikView are stored in the default selection state, which is referred to as $. Alternate States, introduced in QlikView 11, is a functionality that makes it possible to create additional selection states besides the default selection state. This means that you’re able to show two (or more) different sets of selections in a single view.
This feature is very handy when you want to perform comparative analysis. Without Alternate States, you would have to switch back and forth to compare two sets of data. With the use of Alternate States, these two selections can be shown in a single view – which makes it easier for us to compare the two.
How can you create and apply alternate states?
Without the user creating Alternate States, Qlikview only uses the Default state ($). Alternate States are created by going to the General tab in the Document Properties (opened by pressing Ctrl + Alt + D). Here, you find four buttons, the third of which says “Alternate States…”.
If you click it, a dialog box appears. You can add additional states by clicking the Add button (you don’t have to worry about their values – you just create the empty states here).
Now, in the General tab of the properties of your objects, an extra drop-down field is created (next to the Title field). Here, you can assign the state that object responds to. By default, <Inherited> is selected.
Inherited means that the object inherits the state of its parent object or document. Unless an alternate state is selected, an object inherits its state from a worksheet, which inherits its state from a document. If a worksheet is placed in an alternate state, by default all of the objects in that worksheet (which are set to the Inherited state) will be placed into the same alternate state.
Another example is when objects are placed in a container, unless explicitly overridden at the object level these objects will inherit the state of the container. To override the inherited state, you can select an alternate state from the Alternate State drop-down menu.
So, now that we know about creating states and applying them to objects, let’s see how we can use them in our analysis.
A word of warning
Before we start digging deeper, I would like to cite the warning that the QlikView Help file gives us:
“Alternate States functionality is enabled by a QlikView developer and should be used with caution as it can cause great confusion with end-users as there is no automatic on-screen indication that objects or expressions are in, or reference, Alternate States. It is up to the QlikView developer to provide this information to the end-user using the StateName() function.”
Alternate States create great opportunities, but it is of great importance to let the user know what he or she is looking at. Otherwise, values may be wrongly interpreted, which may lead to wrong decisions etc. We don’t want that. So keep in mind: handle with care!
How to use different states?
So, since Alternate States are user-enabled, we need to add a state by the route I’ve outlined earlier before we can start using them. I will show how Alternate States can be used by an example, which you may want to download below, so you can read along and see what happens.
Download the alternate states example application
Example 1.
On the first tab of the example file, you find a very plain use of Alternate States.
As you can see, there are four listboxes for two fields: Year and Client. In the Title of these listboxes, I used the Statename() function to explicitly name which state the listbox uses. If you change the state of the listbox, it automatically changes so that the state remains clear for the user.
Below, you find a bar chart. In the chart, I created two expressions: one that shows the sales for the selection in the default state ( {$} ) and one for the StateA I’ve created.
By making different selections, we’re able to compare different years or different clients – in the same chart without changing views. If no further Set Analysis is used, placing the state name ( ‘$’ or ‘StateA’) between curly brackets is the only thing you need to do in your expressions to create the visualization. ‘$’ may be omitted since that is the default state (and the object is also in the <inherited> state, which by default is the default state), but for the sake of clarity, I prefer to use it anyway.
Now, to show you some more advanced use of Alternate States within set analysis, go to the second tab of the example file:
Example 2.
The difference with the first example is found in the expression for StateA. You might want to make sure that when you make selections in certain fields (e.g. Year here), you always compare the same years for both selections. In that case, you can adjust the expression for the ‘StateA’ to:
The ‘StateA’-part is the State Identifier that refers to StateA. As you can see, within the Set Analysis, this is partially overwritten in the ‘ $::Year ’ part. This is the referral to the selection in the Year field in the $-state, and makes sure that the year is always similar to the selection in the $-state. This way, the comparison will always be for the same years. (In the example file, a listbox for Year (StateA) is shown to make clear that selections have no impact on the visualization. Thus, it may be left out of the dashboard altogether).
$:: versus P()
Besides the double colon (::) notation to transfer selections between states, I also learned about the P() function to transfer selections between states. “What is the difference?” you might wonder. Consider the following two expressions:
As you can see, I added two textboxes in which the results of the above methods are shown.
I figured that both methods look quite similar to each other and wanted to find out if, and if so, what is the difference. It comes down to this:
In the first case, the $::Year is literally referring to the selection you’ve made in the Year field in the $ state. Although there is no data for Client B in 2012, if you do not make an explicit selection in the Year field, all three years will still be shown.
In the second case, the P( {$} ) Year – part is referring to all possible (p) values of the Year field in the $ state. Thus, if you select Client B in the $ state., 2012 will not be shown in the chart as that year is not associated with that Client and therefor is not a possible value.
You’ll find both methods in the expressions of the chart. Feel free to enable them both to see what actually happens when you change your selections. Depending on what you want to achieve you may choose one or the other.
To summarize
As I’ve tried to show, using Alternate States for comparative analysis can be a pretty nice addition to a dashboard, but should be handled with caution. Alternate States can be used by applying it directly to a worksheet or object, or by using them in expressions with Set Analysis to create comparing charts. In the last case, remember that your set-expressions will overwrite the state of the object.
I am aware that this is a very plain example, but I think it might get you thinking about the possibilities, and help you on your way to create them without confusing your users!