Qlikview Technical Brief - Conditional Functions

  • Upload
    skb

  • View
    224

  • Download
    0

Embed Size (px)

Citation preview

  • 7/24/2019 Qlikview Technical Brief - Conditional Functions

    1/15

    CONDITIONAL FUNCTIONS

    QlikView Technical Brief

    April 2014

    QlikView 11.2

  • 7/24/2019 Qlikview Technical Brief - Conditional Functions

    2/15

    Conditional Functions | 2

    Contents

    INTRODUCTION ................................................................................................................. 3

    SET THE SCENE ................................................................................................................ 4FUNCTIONS,FEATURESANDTRICKSUSED .................................................... 4

    CLASS() FUNCTION .......................................................................................................... 5

    ALT() FUNCTION ................................................................................................................ 8

    PICK() FUNCTION ............................................................................................................ 10

    IF() FUNCTION ................................................................................................................. 11

    MATCH(), MIXMATCH(), WILDMATCH() FUNCTIONS .................................................. 13

    SUMMARY ........................................................................................................................ 15

    REFERENCES .................................................................................................................. 15

  • 7/24/2019 Qlikview Technical Brief - Conditional Functions

    3/15

    Conditional Functions | 3

    IntroductionIn this document, the use of the following conditional functions will be

    discussed:

    Class()

    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).

    SUPPORTING MATERIALS QVW.A QVW titled

    Conditional Functions.qvw

    was created to illustrate how

    these conditional functions

    can be used.

  • 7/24/2019 Qlikview Technical Brief - Conditional Functions

    4/15

    Conditional Functions | 4

    Set the Scene

    There are times when an expression needs to be evaluated in order to determine the nextcourse 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()

  • 7/24/2019 Qlikview Technical Brief - Conditional Functions

    5/15

    Conditional Functions | 5

    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

  • 7/24/2019 Qlikview Technical Brief - Conditional Functions

    6/15

    Conditional Functions | 6

    Using the offset parameter changes the starting point from 0 to the parameter. In the examplebelow 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.

  • 7/24/2019 Qlikview Technical Brief - Conditional Functions

    7/15

    Conditional Functions | 7

  • 7/24/2019 Qlikview Technical Brief - Conditional Functions

    8/15

    Conditional Functions | 8

    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 numberrepresentation. If no such match is found, the last parameter will be returned. Anynumber 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') asValidatedDate

    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-272013-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

  • 7/24/2019 Qlikview Technical Brief - Conditional Functions

    9/15

    Conditional Functions | 9

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

    th

    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).

  • 7/24/2019 Qlikview Technical Brief - Conditional Functions

    10/15

    Conditional Functions | 10

    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') asRandomDim

    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.

  • 7/24/2019 Qlikview Technical Brief - Conditional Functions

    11/15

    Conditional Functions | 11

    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 ofany type. They should preferably be of the same type. If condition is true, thefunction returns the value of the expression then. If condition is false, the functionreturns 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 isfalse. 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);

  • 7/24/2019 Qlikview Technical Brief - Conditional Functions

    12/15

    Conditional Functions | 12

    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.

  • 7/24/2019 Qlikview Technical Brief - Conditional Functions

    13/15

    Conditional Functions | 13

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

    The Match(), Mixmatch() and Wildmatch() functions are similar in that they perform acomparison 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.

  • 7/24/2019 Qlikview Technical Brief - Conditional Functions

    14/15

    Conditional Functions | 14

    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 theuse 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.

  • 7/24/2019 Qlikview Technical Brief - Conditional Functions

    15/15

    Conditional Functions | 15

    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 QlikTechproducts 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 aretrademarks or registered trademarks of their respective owners. The information published herein is subject to change without notice. This publication is for informational purposes only, withoutrepresentation or warranty of any kind, and QlikTech shall not be liable f or errors or omissions with r espect to this publication. The only warranties for QlikTech products and services are those thatare set forth i n the express warranty statements accompanying such products and services, if any. Nothing herein should be construed as constituting any additional warranty.