Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Switch..case..default..end switch

Hi All, I'm very new at Qlikview, and i want to see an example of how to se the switch case statement, can anyone help with that?

I need to put a value in a field wich is loaded, depending on the values of other fields. For example, i need to load the field Category, with the value "YES" if the field Managment have the value = 1 and the field Ticket have the value = 2, and with a "NO" if it doesnt have those values. I can use an if in this example, but in the real thing, i'm with a little more expressions to evaluate.

Can anyone give me an example on how to use the case clause for loading a field depending on the values of others?

Thank you very much!

8 Replies
prieper
Master II
Master II

Hi,

The Switch ... functionality refers only to individual steps in the script. Think that you will be better off with a normal "IF"-statement

LOAD *, IF(Field1 = 'Management', 'YES', 'NO') AS Evaluation
FROM ....

HTH

Peter

Anonymous
Not applicable
Author

Juan,

You can find a good example of "switch" in QV help.
I think though, if I got the requirements right, that you need a conditional field definition rather than conditional script flow. It could be:
LOAD
...
if(Managment=1 and Ticket=2, 'Yes', 'No') as Category
RESIDENT...;

Anonymous
Not applicable
Author

Peter, you beat me again Big Smile

Not applicable
Author

Thank you very much guys, I have another question to ask regarding the "exists" function.

I searched in the Help documentation, but i couldn't find if this function works with more than one parameter per field, i mean: exists(field, value1, value2, value3,...), is that ok?

The normal procedure is to test this on my own, but i'm not able to do this yet on my work, and i'm trying to advance my work locally.

Thank you very much again.

Anonymous
Not applicable
Author

Juan,
Parametrs of function exists are only fields names, but not the values. It always has two parametersm, both are the fields names. When you see one parameter, it only means that the field name is the same for field1 and field2.
Function checks if ANY value of field1 exists in field2.

Not applicable
Author

I see, thanks for the response, i was minsunderstanding the functionality. Thank you!

johnw
Champion III
Champion III

It's more than you need for your specific problem, but let's say your situation was more complicated than a simple yes/no flag. Something like this:

Management Category
1 A
2 B
27 C
132 D
others E

There are a lot of ways to handle this in QlikView. I'll give you the three I most commonly use. First, you can load the data as a mapping table and then apply it as a map with a default:

[Categories]:
MAPPING LOAD * INLINE [
Management,Category
1,A
2,B
27,C
132,D
];

applymap('Categories',Management,'E') as Category,

You can also just have a series of IF statements:

if(Management= '1','A',
if(Management= '2','B',
if(Management= '27','C',
if(Management='132','D',
'E')))) as Category,

Or you can do a pick(match()):

pick(1+match(Management,'1','2','27','132'),
'E','A','B','C' ,'D') as Category,

I'm sure there are other solutions as well, but those are the ones I tend to use.

johnw
Champion III
Champion III


Juan Nuvreni wrote: I searched in the Help documentation, but i couldn't find if this function works with more than one parameter per field, i mean: exists(field, value1, value2, value3,...), is that ok?


I think you're looking for the match() function. An example is detailed in my previous response. The first parameter is the field you want to check, and the other parameters are the ones you want to check it against. If any of them match, it returns the position of the first matching value. So match('B','A','B','C') would return 2. You can use it as a number, or just use it as a true/false value, because in QlikView, 0 = false and all other values are true.