At the Masters Summit for QlikView in London we ended the second day with an informal ‘stump the geek’ session. In this session participants could submit technically challenging questions to a panel. Besides the four presenters, this panel also consisted of Steve Dark, Stephen Redmond and Henric Cronström. You can imagine that I found myself surrounded by an awesome amount of QlikView knowledge.
While challenging, most of the questions could either be answered on the spot or a conceptual solution could be described. There was however one question that didn’t really yield an, in my opinion, satisfactory answer. The consensus almost seemed to drift towards ‘it cannot be done’ or ‘that is not how QlikView works’. The question was:
Can we make “or” selections between list boxes, instead of the standard “and” selections?
And & or selection modes
To explain that question a little more, consider the following example of sales amounts by customer, product and salesman:
By default, selections within a list box are done in or-mode. By selecting Customer 1 and Customer 2, we select all records that are associated with either Customer 1 or Customer 2. By contrast, selections between list boxes are always done in and-mode. Selecting Product 2 and Salesman 1 returns all records that are associated with Product 2 and Salesman 1.
The question was if we could override the selection mode between list boxes. In other words, if we could return a result that showed all records associated with either Product 2 or Salesman 1.
At that moment, no one could think of an easy moment to achieve this. While I had some general ideas about where to look I wasn’t able to clearly articulate those at the time. We were stumped, or at least, I was.
A possible solution
Of course, to someone who likes solving problems there is nothing more challenging than a problem that appears unsolvable. Impossible? We’ll see about that! (I like how I am not the only one who accepted this challenge by the way, Steve Dark has also posted some of his thoughts and solutions to the QlikCommunity.)
After taking some time to think it through, I came up with a preliminary solution. Now that the Masters Summits have finished and I’ve finally caught up on my day job again, I found the time to turn this into a more presentable solution:
The image above shows the exact same selection as the earlier image. However, when you look at the straight table you will notice that the selection has now been made in or-mode. Data is shown when the Customer is Customer 1 or Customer 2 or when the Product is Product 2 or when the Salesman is Salesman 1. This is achieved using a Set Analysis statement that is built using the following (simplified) logic:
- For each field in the data model that is not in the excluded fields list;
- Check if selections have been made in the field using GetSelectedCount();
- If selections have been made, generate a Set Analysis statement clearing selections in all other fields, except fields in the excluded fields list;
- When all fields are processed, union all the Set Analysis statements together (note: I will expand the post later to explain this concept in some more detail);
- Use the unioned Set Analysis statement in your expressions.
A variable is used to store a list of fields to exclude from the or-mode. An example of fields to exclude are calendar fields. Showing sales that were made in the year 2012 or by Salesman 1 doesn’t really make any sense. The expression that I use to generate the Set Analysis statement is shown below. You can imagine that it was quite a beast to write:
What I like about this solution is that it’ll work in any data model, there is no need to predefine things in your data model or create alternate states. There’s also a few things that could be improved though:
- Create an additional statement that doesn’t work on all fields in the data model, but only on those fields specified in an included fields list. This should be a relatively easy change.
- The current expression clears all selections except for the active field. Rewrite the expression so that it only clears selections in fields that actually have selections. Conceptually a very easy change, but might require some more tweaking to get it into the expression without errors.
Performance is another important aspect. In the test I did with 10,000 row dimension tables and a 10,000,000 row fact table this solution actually performed quite well. Of course I haven’t applied this to any real world solutions, but who knows if it might prove useful in the future?
Download the example file
You can download the example file below. In it you will find the expressions that I used, which you can copy to your own applications. Of course, if you improve the expressions then I’d like to hear from you.
Download the example application
Your thoughts?
So what are your ideas? Is this something you should even be doing in QlikView? Is there a better way to achieve this? I’d be very interested in hearing your thoughts and feedback.
34 Comments
Hi Barry – many thanks for the dual mentions in this post and for sharing your ideas on the topic.
Many thanks to Steve and Barry for taking time to crack this. It shows again what a versatile tool Qlikview is, when backed by such masterful knowledge.
Well done Barry!
Excellent solution Barry. Thanks for taking the time to work it out and post it.
Thats amazing news and great work around. OR mode.. Great…
wow, great solution! thanks for sharing it with us Barry. I’ve even seen some ideas in your example application that I could reuse in our developments
Hi Marcos,
Glad you liked it. Out of curiosity, which ideas did you find useful?
Cheers,
Barry
Hi Barry.
To begin, the script to generate random data, I liked it. The use of the field “$Field” toghether with CONCAT in vSelection.Or variable and the whole construction of the variable; use of different variables and their combination to generate the final expression of Sales, and finally the solution of the example to be able to use the OR-Mode
All these parts of the example have shown me an interesting use of QlikView options that I know, but I liked how you put it into practice
Hi Barry,
for some reason I can’t download a example file. It gives me Error (404). Can you help, please?
Hei Berry!
Nice job. I tried to download example, but it didn’t work. Can you check dropbox link ?
Andres
Vlad, Andres,
I accidentally deleted the file, it’s been restored now.
Kind regards,
Barry
There is a built in ‘and’ mode. It requires you to modify the data model http://blogagilos.wordpress.com/2011/07/15/tip-1-the-qlikview-and-mode/
Maybe thats something abit different though?
Hi Barry,
This is great. Thank you for the solution. It works perfectly when I apply it to 10 fields. However, I tried to do it with a survey result where there are brand perference questions. For example, do you prefer to chocolate brand A, B or C? There are lots of questions like this that results in thousands of fields. The solution doesn’t work in this case.
Do you know the maximum variables that it can take? Or is there other tweaks that I need to do to make it work?
Thanks,
Carol
Hi Carol,
I guess there’s some limit in the length of an expression, and with this solution you’re basically generating an expression that’s longer than the combined length of the (in your case thousands of) fields you’re trying to perform an ‘OR’ on.
Last year I’ve tried to build a variant on this solution based on the {1} set instead of {$} (so I didn’t have to clear lots of fields using ‘Fieldname=’ set modifiers), but didn’t quite pull it off. 🙁
If anyone manages to optimize Barry’s solution using this strategy I’d also be interested.
Regards,
Martijn
Hey Barry,
I like your solution. I need to get it to work on mine. But when I put it in it doesn’t work. I put it in this way. It won’t make the vSelection. Any ideas?
SET vSelection.Or=$(=
Concat(DISTINCT {}
‘If(GetSelectedCount([‘ & [$Field] & ‘]) > 0, ‘ & chr(39) &
‘<[' & chr(39) & ' & Concat(DISTINCT {} [$Field], ‘ & chr(39) & ‘]= ,[‘ &
chr(39) & ‘) & ‘ & chr(39) & ‘]= > + ‘ & chr(39) & ‘)’ , ‘ & ‘)
);
Thank you.
J
Hello ,
I am not able to download the file. It says “Web Page Not Available”.
Please suggest how can I get the example file?
Thanks,
Dixit
+1
Barry,
This is a very clever solution, but I have a potential problem. The scenario is that we add a Customer 4 which is associated with a Product 4, and no other customers are associated with Product 4. When I start by filtering on Customer 1, Product 4 will be grayed out/unassociated. When I then select the grayed out Product 4, QlikView will force Customer 1 to unselect. It seems that this method only works when everything is associated with everything?
hi.
sorry, i don’t know english..
I’m qliview studying.
i want this example file.
please.
Thanks for your topic! is great. But how can use in een datmodel without autogenarate.
Is there a way to overrride the grey behavior of qlikview. Looks like the ‘or’ selections work but still show up grey in the example attached in this blog.
Thanks for the solution. We have reached till this point in our application, however challenge is that how can we get this logic working in Section Access.
Considering the above example how can i restrict one particular user the application access for Product2 or Salesman1
In our case we are trying to restrict user to access RegionA = ‘Asia'(Region B can be asia or anything else) or RegionB = ‘Asia'(Region A can be asia or anything else)
So User has access to Region A = asia or Region B = asia
Hi, I’m not able to download solution… How can I do?
Best regards
Francesco
Hi Barry,
I can see that most of the files for your examples are not available to download from Dropbox link you have provided.
Can you check this and come back to us?
I am not able to download the file from dropbox. Can somebody please send me the file.
What a genius idea! Unfortunately I am not able to download the file. Can you please send me the file?
Hi,
I am very interested in your solution to this issue, but am unable to download the example.
Could you send it to me?
Thanks!
Please send me your example plis.. Your page is not working im this time…
thank you so much…
Does anyone have this example file they can share please?
Can you please upload solution again. The Dropbox file is unavailable.
I am unable to download this qvw file from Dropbox. Could you please provide me an updated link?
For those looking for the download, you can find it here: https://community.qlik.com/t5/QlikView-App-Development/Or-mode-selections-between-list-boxes/m-p/1625643/highlight/true#M446099