Vous êtes sur la page 1sur 15

CONDITIONAL FUNCTIONS

QlikView Technical Brief

April 2014
QlikView 11.2

Contents
INTRODUCTION ................................................................................................................. 3
SET THE SCENE ................................................................................................................ 4
FUNCTIONS, FEATURES AND TRICKS USED .................................................... 4
CLASS() FUNCTION .......................................................................................................... 5
ALT() FUNCTION ................................................................................................................ 8
PICK() FUNCTION ............................................................................................................ 10
IF() FUNCTION ................................................................................................................. 11
MATCH(), MIXMATCH(), WILDMATCH() FUNCTIONS .................................................. 13
SUMMARY ........................................................................................................................ 15
REFERENCES .................................................................................................................. 15

Conditional Functions | 2

Introduction
In this document, the use of the following conditional functions will be
discussed:

Class()

SUPPORTING MATERIALS
QVW. A QVW titled
Conditional Functions.qvw
was created to illustrate how
these conditional functions
can be used.

Alt()
Pick()
If()
Match()
Mixmatch()
Wildmatch()

Conditional functions provide a way for us check a condition so that we


can determine what action to perform. All of these functions can be
used in the script as well as in expressions in the user interface (UI).

Conditional Functions | 3

Set the Scene


There are times when an expression needs to be evaluated in order to determine the next
course of action. QlikView provides a host of functions that return a value based on a condition.
FUNCTIONS, FEATURES AND TRICKS USED
The following string functions will be defined in this document:
Class()
Alt()
Pick()
If()
Match()
Mixmatch()
Wildmatch()

Conditional Functions | 4

Class() Function
The Class() function is defined in QlikView Help this way:

class(expression, interval [ , label [ , offset ]])


Creates a classification of expressions. The bin width is determined by the
number set as interval. The result is shown as a<=x<b, where a and b are the
upper and lower limits of the bin. The x can be replaced by an arbitrary string
stated in label. 0 is normally the default starting point of the classification. This
can be changed by adding an offset.subfield(s, 'delimiter' [ , index ] )

The Class() function is very useful in the script or in an UI expression when there is the need to
create a bucket/grouping of data. For example, assume you have a data model with order data
and there is a field with the number of days an order is late. Using class(), the number of days
late can be grouped or put in appropriate bins based on the interval parameter. The expression
below will create buckets with 30 day intervals.
Class([Number Of Days Late],30) as [Days Late]

The buckets will look like this:

Since a label was not used in the function, x is used but this can be anything you like such as
days.
Class([Number Of Days Late],30, 'days') as [Days Late]

Conditional Functions | 5

Using the offset parameter changes the starting point from 0 to the parameter. In the example
below the offset was set to 3.
Class([Number Of Days Late],30,'days',3) as [Days Late]

Using class allows you to create a chart like the one below that shows the number of late orders
based on various buckets.

Conditional Functions | 6

Conditional Functions | 7

Alt() Function
The Alt() function is defined in QlikView Help this way:

alt(case1[ , case2 , case3 , ...] , else)


The alt function returns the first of the parameters that has a valid number
representation. If no such match is found, the last parameter will be returned. Any
number of parameters can be used.

The Alt() function can be used in the script or in an UI expression and will return the first valid
number representation (including dates). If no match is found then the last parameter will be
returned. An example of how this function can be used is to check the format of a date field.
Assume there is a date field that has dates in various formats. The Alt() function can be used to
check these formats and to indicate which dates do not match a format listed. In the example
below only dates with the format M/D/YYYY or DD/MM/YYYY is ok otherwise Invalid Date
should be displayed.
ValidatedDates:
LOAD
TempDate,
Alt(Date#(TempDate, 'M/D/YYYY'), Date#(TempDate, 'DD/MM/YYYY'),
Date#(TempDate, 'DD/MM/YYYY'), Date#(TempDate, 'YYYY-MM-DD'), Date#(TempDate,
'DD.MM.YYYY'), 'Invalid Date') as ValidatedDate
INLINE [
TempDate
1/1/2014
1/6/2014
01/08/2014
25/01/14
25/1/2014
40729
30.02.2014
16.08.2014
31/12/2013
20/05/2014
2014-02-27
2013-11-20
];

In the script above there is a Dates table with dates in various formats. In the ValidatedDates
table, the Alt() function is used to check the dates. If the format matches one of the parameters
then it is returned. If the format of the date does not match any of the parameters, then Invalid
Date is returned. Many parameters can be used in the Alt() function if necessary. The order of
the parameters indicates the priority order so that can be used to determine how the dates
Conditional Functions | 8

th

th

should be interpreted. For example, should 7/4/2014 be interpreted as 4 of July in the US or 7


of April in the UK.

The result of the script can be seen in the table below. The TempDate is the original date and
the ValidatedDate is the value returned by the Alt() function.

Notice that 40729 did not match the format thus Invalid Date was returned.

The Alt() function can also be used to display a value other than null when an expression results
in null. For example, assume you have an expression that multiplies two values and one of the
values is null, the Alt() function can be used to display 0 (or something else) instead of null like
this: Alt(A*B, 0).

Conditional Functions | 9

Pick() Function
The Pick() function is defined in QlikView Help this way:

pick(n, expr1[ , expr2,...exprN])


Returns the n:th expression in the list. n is an integer between 1 and N.

The Pick() function will return the expression/value corresponding to the expression that
matches the first parameter. For example, Pick(2, A, B, C) will return B because B is the
second expression and Pick(Number, Sum(1+1), Sum(2+2), Sum(3+3), Sum(4+4), Sum(5+5))
will return 6 if Number = 3.

This function is excellent when you want to generate a random field value, e.g.
Pick(Ceil(3*Rand()),'A','B','C') as RandomDim

This random field was added to the Salesrep table and randomly stored A, B or C in the
RandomDim field. Pick() can be used in both the script and in an UI expression.

Conditional Functions | 10

If() Function
The If statement is defined in QlikView Help this way:

if(condition , then , else)


The three parameters condition, then and else are all expressions. The first one,
condition, is interpreted logically. The two other ones, then and else, can be of
any type. They should preferably be of the same type. If condition is true, the
function returns the value of the expression then. If condition is false, the function
returns the value of the expression else.

An If() statement can be used in the script or in an UI expression. The first parameter checks a
condition and returns the then parameter if it is true or the else parameter if the condition is
false. There are many reasons to use an if statement. In a script, the if statement can be used
to create a flag as seen in the script below.
SalesDetails:
LOAD
[Address Number],
BudKey,
AggKey,
DateKey,
[Order Number],
[Item-Branch Key],
[Sales Rep],
[Item Class],
CustKey,
[Line Desc 1],
[Invoice Number],
[Sales Quantity],
[Actual Delivery Date],
[Last Status],
[Number Of Days Late],
Class([Number Of Days Late],30) as [Days Late],
if([Number Of Days Late]>=30,1,0) as [30 or More Days Late],
[Order Status],
[Line Number],
[Item Number],
[Line Type],
[List Price],
[Sales Cost Amount],
[Order Margin],
[Open Order Cost Amount],
[Discount Amount],
[Invoice Date],
[Unit Price],
[Promised Delivery Date],
[Open Order Amount],
[Sales Amount],
[BackOrder Amount],
[Sales Price]
FROM $(vPath)SalesDetails.qvd (qvd);
Conditional Functions | 11

In this script a flag ([30 or More Days Late]) is created to indicate if an order is 30 or more days
late. If it is 30 or more dates late, then the flag is set to 1 (true) otherwise it is set to 0 (false).
This flag can then be used in the UI as a list box or in a set analysis expression.

Conditional Functions | 12

Match(), Mixmatch(), Wildmatch() Functions


The Match(), Mixmatch() and Wildmatch() functions are similar in that they perform a
comparison between the first parameter and the expression parameters.

The Match() function is defined in QlikView Help this way:

match( str, expr1 [ , expr2,...exprN ] )


The match function performs a case sensitive comparison.

The Match() function does a case sensitive comparison between the first parameter and the
expressions. It can be used in the script or in a UI expression. In the expression Match(X, Jan,
Feb, Mar, Apr), 1 is returned if X=Jan, 2 if X=Feb and so on. If X=Dec, then 0 is returned.

The Matchmix() function is defined in QlikView Help this way:

mixmatch( str, expr1 [ , expr2,...exprN ] )


The mixmatch function performs a case insensitive comparison.

The Mixmatch() function does a case insensitive comparison between the first parameter and
the expressions. It can also be used in the script or in a UI expression. In the expression
Mixmatch(X, Jan, Feb, Mar, Apr), 1 is returned if X=Jan, 2 if X=feb and so on. If X=Dec,
then 0 is returned. With Mixmatch() the case does not matter.

Conditional Functions | 13

The Wildmatch() function is defined in QlikView Help this way:

wildmatch( str, expr1 [ , expr2,...exprN ] )


The wildmatch function performs a case insensitive comparison and permits the
use of wildcard characters ( * and ?) in the comparison strings.

The Wildmatch() function does a case insensitive comparison between the first parameter and
the expressions and wildcard characters can be used. It can be used in the script or in an UI
expression. In the expression Wildmatch(X, Ja*, F?b, mar, Apr), 1 is returned if X=jan, 2 if
X=Feb, 3 is returned if X=Mar and so on. If X=Dec, then 0 is returned.

Conditional Functions | 14

Summary
In this technical brief we looked at 7 conditional functions: Class(), Alt(), Pick(), If(), Match(),
Mixmatch() and Wildmatch() that can be used in expressions and the script to do a comparison
or check a condition before deciding the next course of action.

References
Below are supporting materials that may be helpful:

QVW Conditonal Functions.qvw - example expressions and scripts of all conditional functions
discussed in this document.

www.qlikviiew.com

2012 QlikTech International AB. All rights reserved. QlikTech, QlikView, Qlik, Q, Simplifying Analysis for Everyone, Power of Simplicity, New Rules, The Uncontrollable Smile and other QlikTech
products and services as well as their respective logos are trademarks or registered trademarks of QlikTech International AB. All other company names, products and services used herein are
trademarks or registered trademarks of their respective owners. The information published herein is subject to change without notice. This publication is for informational purposes only, without
representation or warranty of any kind, and QlikTech shall not be liable for errors or omissions with respect to this publication. The only warranties for QlikTech products and services are those that
are set forth in the express warranty statements accompanying such products and services, if any. Nothing herein should be construed as constituting any additional warranty.

Conditional Functions | 15

Vous aimerez peut-être aussi