Alternate States has been a feature in QlikView for many years. It has now appeared in Qlik Sense Cloud, which means it is likely to appear in other versions very soon. This post, and the associated example app, looks at what Alternate States are and how to use them in Sense.
What Are Alternate States?
Everything in Qlik products revolve around associations and selections. When a selection is made all tables and charts flex to only show data from within that selection. However, something that is incredibly useful with analytics is to be able to compare two sets of data based on totally different selections. This is exactly what Alternate States provides.
When this feature was first brought into QlikView the best article I saw on it was by Wesley Smit in this blog post. This post is four years old, but still worth a read. The implementation in Sense follows the same idea. You can create a number of different ‘states’ and then attach these to different sheets and objects. Each state has it’s own set of selections so charts can behave independently.
Creating Alternate States in Sense
Alternate States appear under the Master Items menu in Sense. There is a simple Create New button to click.
The dialog for adding a state is much simpler than when you add Measures, Dimensions or Visualisations. You are only asked for a name.
Enter the state name and then click Create.
Often if I am using Alternate States I will not use the default state at all, and instead ensure that every object is linked to a new state. This is what I have done in the example associated with this blog post. I have created two new states State 1 and State 2.
Applying States to Sheets and Objects
To apply a state you can simply drag it from the Master Item list onto the object or sheet you wish to change the state of.
When applying a state to a sheet the method is exactly the same; just drag the state onto the sheet. Any charts on the sheet which do not have a state explicitly set will ‘inherit’ the state from the sheet. This way everything on one sheet can be set to be independent of selections made on other sheets.
As well as dragging states onto Sheets and Objects from the Master Item palette they can also be applied in the properties of Sheets and other Objects. You will find the drop-down State selector on it’s own menu section for Sheets and under Appearance for other Objects.
Once you have your states set up you can then start making selections to see them in action.
In order to create a very simple demo I loaded just four lines of data, from an inline load.
I then created a simple bar chart to show this data and replicated that twice. The first chart was left in the default state and the clones were set to State 1 and State 2. I then changed the colour on the two alternate state charts.
Obviously with no selections all charts are the same.
Selecting values from the default state chart has no impact on the two charts which are in different states, despite them all being based on the one dimension.
Similarly, if selections are made on the charts in the other two states these are also applied independently. This way values can be compared across two different sets of selections.
Note that the values shown in the report title do not presently respond to the state of the chart automatically. This seems like a glitch in the Alternate States implementation which is on Qlik Cloud as at now. Hopefully it will be fixed soon. I have used Set Analysis to get the right totals, the syntax is given below.
The Selection Bar and Alternate States
The selections bar when a selection is made in the default state only is identical to when a selection is made without Alternate States. When a selection is made on a field in any other state the state name is shown in the selections field along with the dimension. When selections are made on the same field in more than one state then all selections fro that field are grouped on the selections bar. These three selection styles are shown here:
Note the green dot on the middle selection shows it is an Alternate State selection, but with only one state selected in. The number in the right-hand selection shows the same field has selections in multiple states.
The drop down then shows what selections have been made and in which state.
Selections can be amended in each state, or cleared in all states.
Alternate States and Set Analysis
With the Sense point and click interface you can apply Alternate States to different objects and compare the differences there. Sometimes however you need to compare more closely, perhaps showing a variance between values in one Alternate State to another. This means having more than one state in play in a single chart object.
Fortunately this is possible, using a set of syntax called Set Analysis. Typically this syntax is used for tying a particular chart or column to a selection in a field. It can also be used to tie a measure to a state in the expression.
To find the total value in State1 the syntax would be:
If you use set analysis for limiting to dimension values you will recognise the squiggly brackets. The Alternate State syntax then replaces the $ or 1 with the state name. It’s worth noting that to apply extra Set Analysis syntax a set modifier can still be applied when specifying an Alternate State as the scope.
This chart and table can then be produced that combine values from both states.
Note that to get the colour of the stacked bar chart correct needed a bit of crafty code, as it is not possible to just pick a different colour for each measure. Download the app if you need to see how that works.
Effective Alternate State Usage
The thing to watch with Alternate States is that your application will not work how your users will expect it to. Whenever users are trained in how applications work they are told that selections made anywhere affect everything in the app (this is something I pick up on in my ‘Star Equals’ blog post). When you add Alternate States this golden rule is then broken.
In order not to confuse users, and cause them to make incorrect assumptions, ensure you always add sufficient signposts as to what is going on. The first way of doing this is probably to add a Text and Image object with detailed explanation of what Alternate States you have set up and what they are there to achieve.
Another good idea is to use strong colour coding for different states. You will see that the two colours that I use for the states in the individual charts then carry through into the stacked chart that combines the two states. This kind of implicit legend, if properly explained, can make the use of Alternate States much less confusing.
Personally, I would advise not using Alternate States too frequently and certainly don’t abuse them. For instance putting each sheet in a different state so that selections don’t carry from sheet to sheet would be a very bad idea. Used well they can be very powerful, used incorrectly they can be a disaster.
This app can be found here: https://community.qlik.com/t5/Member-Articles/Qlik-Sense-App-Alternate-States-in-Qlik-Sense/ta-p/1487961
If you have found this article and the app associated with it helpful please do give it a like on the Community, as this makes it more visible to other users who may also benefit.
If you have any comments about Alternate States please do leave them below.