# Comparative analysis with alternate states - Qlik View

In addition to time - based comparisons, there are other scenario's in which the comparison of two different sets of data can help enhance the analytical capabilities of a QlikView document. In this section, we will present a feature available in QlikView since version 11 which makes it easy to create highly dynamic comparative scenarios that enables business discovery in an entirely new way.

A comparative analysis example

Let's discuss one of these scenarios by using our Airline Operations document. Suppose we want to compare how the number of international flights arriving at the city of Chicago, IL, and performed by US carriers, compares to the number of domestic flights departing from Chicago and bound to the State of California, performed by US Carriers as well. If we were to see this comparison in a bar chart and over time, we would have the following:

In the preceding example, the total flights in Group A (represented by the blue bars) correspond to those coming from outside the US, arriving at Chicago, and performed by US carriers, as depicted in the corresponding current selections box on the left. Group B (represented by the brown bars) covers flights departing from Chicago, bound to California, and performed by US carriers as well, as depicted in the current selections box on the right.

We can easily see how the amount of flights performed by Group A is greater to that of Group B during the first five months of 2011, it is almost equal during June and July, and it is lower during the rest of the year.

Alternate states step-by-step

We have been able to define two different record sets and compare the corresponding results side-by-side by enabling the alternate states functionality.
To see how this works, let's build the preceding chart, step-by-step, by following this procedure:

1. Open the Airline Operations.qvw document we have been working with.
2. Click on the Add Sheet button from the Design toolbar to create a new sheet.
3. Right-click on an empty space of the worksheet area of the new sheet and select Properties..
4. From the Sheet Properties window, activate the General tab and enter Comparative Analysis into the Title field.
5. Click on OK in the Sheet Properties window.
6. Now, navigate to Settings | Document Properties. and activate the General tab from the Document Properties window.
7. Then, click on the Alternate States. button and a new pop-up window will appear , as seen here:

8. From the Alternate States window, click on the Add. button and enter Group A into the New State Name window. Then, click on OK to create the new alternate state and get back to the Alternate States window.
9. Click on the Add. button once more in the Alternate States window to create a new state, and name it Group B.
10. Click on OK to close the Alternate States window and on OK again to exit the Document Properties window.
At this point, we have defined two different states: Group A and Group B. Each of these states will be used to save the two different record sets we want to analyze, which will be given by user selections in a set of listboxes we will now create.
11. Add the following listboxes by right-clicking on an empty space from the worksheet area and clicking on Select Fields. from the context menu:
Origin Country, Origin State Code, Origin City, Origin Airport, Destination Country, Destination State Code, Destination City,Destination Airport, and Flight Type.
12. Now, create a container object with all of the above-listed listboxes in it.
13. Enter Origin and Destination - Group A as the container object's title and click on OK to close the Container Properties dialog window.
14. From the General tab of the container object properties window, select Group A in the Alternate State drop-down list, as seen here:

This drop-down list is available in the sheet object once the Alternate States feature has been enabled by defining at least one alternate state from the Document Properties window. If no alternate state has been previously created, the drop-down list will not be visible.
15. Now, clone the container object just created by copying it to the clipboard (right-click on Copy to Clipboard and go to Object) and then pasting it again (right-click on the sheet workspace area and select Paste Sheet Object).
16. Access the properties window corresponding to the new copy of the container object and change the alternate state from Group A to Group B. Change the title as well to Origin and Destination - Group B.
17. After arranging the presentation of the two container objects just created, we should have the following:

What we've done is define two sets of listboxes to control selections and indicate which data each alternate should state take into account. As the state of all new objects, by default, set to inherited, all of our listboxes are using the alternate state defined in the next higher-level object, which is the container into which they were placed.

Any selection made on one state does not affect any other state.
By using container objects to accommodate the various listboxes related to origin and destination dimensions, we are able to save some valuable screen space and keep an ordered layout in our document.We will now create a bar chart to visualize the comparison of the two record sets:

1. Before continuing, copy the listboxes corresponding to Year, Quarter, and Month from one of the other sheets and place them into the new Comparative Analysis sheet as linked objects.
2. Now, create a new chart by clicking on the Create Chart button from the Design toolbar and setting the following properties:
• Chart Type: Bar Chart
• Window Title: Group Avs Group B
• Show Title in Chart: Disabled
• Dimension: Period
• Expression 1:
Label: Group A
Definition:Sum({[Group A] * $} [# Departures Performed]) • Expression 2: Label: Group B Definition:Sum({[Group B] *$} [#Departures Performed])
• Primary Dimension Labels: With diagonal orientation
• Number Format Settings: Integer for both expressions

At this point, our chart will respond to user selections on either state and show the corresponding comparisons. Let's select the following values in each of the alternate states:

• Group A:
• Flight Type: International, US Carriers Only
• Destination City: Chicago, IL
• Group B:
• Flight Type: Domestic, US Carriers Only
• Origin City: Chicago, IL
• Destination State Code: CA
• Year: 2011
The selection in Year is made in the default document state, as the corresponding listbox has not been associated to either alternate state. This selection will apply to both chart calculations.
Our comparative analysis chart will now show the trend we discussed previously. Let's now look at how we constructed the preceding expressions.

State-based expressions

The syntax we used is very similar to the one we described when we introduced Set Analysis. In this case, the [Group A] and [Group B] parameters are the equivalent of a set identifier. To quickly recap, the two different set identifiers we described previously are:

• $(Dollar symbol): This set identifier is used to base the calculation on the default current selections. • 1 (number one): This set identifier is used when the calculation should be based on the entire document data, disregarding any selections made by the user. Similar to these two, we can use any defined alternate state as set identifiers in our expressions so that the calculation is based only on selections made on listboxes (or objects) linked to that specific state. The syntax, apart from the set identifier (or state identifier), is basically the same as with Set Analysis. This means we can integrate additional set modifiers into our state-based expressions. We can also use the same set operators described in the Set operators section in this chapter. Combining alternate states and the default state In the two expressions we just created, we are using the Intersection set operator to further restrict our calculation and use a modified record set. Take, for example, the expression corresponding to group A: Sum({[Group A] *$}[#Departures Performed])

With this expression, the calculation will be based on both the selections made on the alternate state named Group A and the selections made on the default state. In other words, that is, only the data found in both record sets is considered in the calculation. This is especially useful when we only need a few fields to differentiate each alternate state (like the origin and destination dimensions), while selections in all other fields should be equally considered in all alternate states. In our case, we are using the intersection operation to be able to take into account selections made on the Year, Quarter, and Month fields.

We must be careful, though, when using set intersections in our state-based expressions. For this to work appropriately, no conflicting selections should be made in the default state over the fields we have also defined in our alternate states.

However, you want to play it safe, there is another way in which we could construct our expressions:

• Group A:
• Group B:

The preceding expressions will only consider the selections made in the Year, Quarter, and Month fields from the default document state and add them to all selections made on the corresponding alternate state. By doing so, we ensure that all of the selections made in any other field outside the alternate state are not taken into account.

Applying alternate states to layout objects

By default, all layout objects (that is, charts, listboxes, sheets, and so on) use an Inheritedstate unless specifically overridden via its properties window. Sheet objects can inherit states from a higher-level object, such as a sheet or a container object.

There is another state that can be specifically applied to any sheet or sheet object: the default state. The QlikView document is always in the default state.

Additionally, once the alternate states feature has been enabled, we can create new alternate states from any layout object by selecting the <new state>option from the Alternate States drop-down list.

Just as with Set Analysis, the use of alternate states can become a bit confusing for the end users if we are not careful. It is very important to develop our QlikView documents in such a way that every state-based object is properly labeled. This will help the user to easily identify how each calculation is being performed and the record set it is based on.

One way we can do this is by adding current selections boxes to represent each alternate state at any given time. Look at the image with which we started this section and you'll see how important they are.

Clearing selections in an alternate state

By default, all of the buttons in the navigation toolbar affect all states. That is, when we click on the Clear button, all selections from all states are cleared. Similarly, the Back and Forward commands apply to all selections in all of the states.

However, it is possible to clear the selections in one specific state without affecting the others. This is accomplished via a menu item found under the Clear button drop-down menu, as shown below: