QlikView Expressor: The Decode Function

    Originally published on 07-21-2011 09:02 AM

     

    The decode function is included in the basic group of expressor datascript  functions.  This function can be used to create a single statement that  implements the same sort of processing as an if..then..else or an  if..elseif..else statement.  When you use the decode function, you do  not need to explicitly code a return statement or an assignment  statement.  Rather, the decode function will return a value that you  specify. Consequently, you can use the decode function to implement the equivalent of a complex block of code while adhering to the guidelines required of the Expression Editor.

    The API of the decode function sets up a series of logical comparisons, each associated with a return value.  The function evaluates each comparison in sequence and returns the return value for the first comparison that evaluates to true.

    As an example, let’s assume that each incoming record includes four attributes: the president’s chronological position, last name, first name, and party.  We want to rework the data in the input record, concatenating a president’s first and last names and his party affiliation into a single statement.  Each outgoing record includes a single attribute named summary containing a statement such as “George Washington is neither a Democrat nor a Republican.”  You could write expressor datascript to implement this logic using an if..elseif..else statement.

    decode_2.png

    But you could implement the same logic as a single statement, replacing the if..elseif..else block with the decode function.

    decode_1.png

    Note how the four arguments to the decode function perform the same  logical comparisons as the if..elseif..else block and that by using the  decode function as an argument to the string.concatenate function, it  was possible to generate the entire output value within a single  statement.

    The implementation that employs the decode function could be entered into the Expression Rule as the syntax of the right-hand side of the assignment statement adheres to the guidelines for using an Expression Rule (the keyword return and assignment statements are not included in the coding).