Variables and Buttons, I pretty love these two Qlik features!
Let’s see together an application in which I will use together variables and buttons and how one object can change the other’s behaviour.
First of all, we must create a new application; in this example, i will use following script:

Application’s script

In this way, our application will have a Sales Table with 4 fields: Year, Customer, Sales and Profit.
Imagine that you want to create a bar chart showning the sales data across the years; however, it could also happen that you might swap the dimension Year to Customer.
This can be easily achieved with an alternate dimension, in detail:

Sales Data Bar Chart With Alternate Dimensions

Furthermore, it might happen that you want to change the measure from sales to profit, and it can be always achieved with an alternative measure.
You will notice that we have now four combinations of analysis in this chart:

  • <customer , sum(Sales)>
  • <year , sum(Sales)>
  • <customer , sum(Profit)>
  • <year , sum(Profit)>

It could happen that you won’t always notice that you can swap the dimensions and the measure by the little arrow… so an idea to optimize this kind of analysis could be using buttons and variables!
In this example, I will need two different buttons for the dimensions and two different buttons for the measures, in detail:

Buttons in the sheets

Firstly, the desired output is that, while pressing the ‘Year’ Button, the desired dimension in the bar chart will be Year and, while pressing the Profit button, the desired measure in the bar chart will be Profit.
This means that:

  • Year and Customer buttons will change a value for the dimension chart
  • Sales and Profit buttons will change a value for the measure chart

This can be achieved using two variables, in detail v_dimensions and v_measure, let’s now create them!

Creations of v_dimension and v_measure variables

If you notice, I had to insert a definition for both variables because it will the default variables value while opening the application!
Furthermore, if you want to show by default the Sales data across the Customer dimensions, you will need to define:

  • v_dimension: Customer
  • v_measure: Sales

Now, we need to let the button change a variables’s value! Let’s see how to achieve it (in this example I will use the Year button):

Button actions with variable
  1. Open the sheet in edit mode
  2. Select the desired button
  3. In the right panel, click ‘Add action’
  4. Set as action ‘Set Variable value’
  5. Select as variable v_dimension in the dropdown menu
  6. Set as value Year

As a result, if you will press the Year button, the v_dimension variables will be set to Year.

In addition, let’s do the same steps for the remaining buttons:

  • Customer button, the v_dimension variable will be set to Customer
  • Sales button, the v_measure variable will be set to Sales
  • Profit button, the v_measure variable will be set to Profit

Now that we set all the buttons with the desired variables, we have to remove the alternate dimension and the alternate measure in the bar chart… and now, let the magic begins!

Firstly, I will start by the dimension, which will change according to the v_dimension variable

Variable as a dimension

Secondly, I will change the measure according to the v_measure variable

Variable as measure

If you followed correctly all the steps, if you press a dimension or measure variable, it will change the bar chart! However, there is a “little problem” that will achieved very easily!
In the current situation, you’ll know which dimension or measure you just selected by only watching the bar chart… so, my idea is to give a custom colour based on the current value of the variable!

As before, in this example I will use the Year button:

  • Open the sheet in edit mode
  • Select the desired button
  • In the right panel, go on Appearance, Background
  • Click the dropdown menu and select ‘By Expression’
  • The expression will be the following if(‘$(v_dimension)’=’Year’,green(),LightGray())
Change the button background based on the variable value

Following the same steps for the remaining buttons, the output will be the following

Bar chart with variables as dimension/measure

Hope you enjoyed the reading 🙂