Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

how to create a user defined function?

I'm using Qlikview version 11.20 SR5 64-bit version.

NONE of the examples I find on the website work, even when I copy paste them in. If I use SET to define the function when I use I simply get the function back as a string.

eg:

SET addTheseForMe = $1 + $2;

trace $(addTheseForMe(1,2));

this will return:

$1 + $2

if I use:

LET addTheseForMe = $1 + $2;

trace $(addTheseForMe(1,2));

I get an empty value. I've reduced it to a simple trace command for now since it wasn't working inside my regular logic. I hear there have been major changes between versions but I seem to be spending more time getting minor functions to (not) work than I am actually investing in solid development time.

FWIW my original UDF is:

set CURRENT_PERIOD_NAME = if(num($1) < 10, 'p0' & $1, 'p' & $1);

(because SET will simply return the function declaration, and if I wrap the response in $() I receive an empty string. If I use 'let' I simply receive 'p' back with no recognition of the $1 value... even when the value passed in is a '1', which should eval to p0 as the returned result.

help!

2 Replies
pgrenier
Partner - Creator III
Partner - Creator III

Hello Dave,

You can accomplish this by creating a macro (Tools -> Edit Module), such as this one:

Function addTheseForMe(var1, var2)

  addTheseForMe= var1+var2

End Function

Then, allow macros to run during the load with by check this box in the Document's properties:

MacroSecurity.jpg

Finally, if you may access the function in your load script as such:

LET varA = 7;

LET varB = 8;

LET varAB = addTheseForMe(varA, varB);

Please find the example application in attachement, regards,

Philippe

maxgro
MVP
MVP

me too I have 11.20 SR5

script and log are

SET addTheseForMe = $1 + $2;

TRACE $(addTheseForMe(1,2));               // 1 + 2

let result=$(addTheseForMe(1,2));

trace $(result);                                        // 3

set cpn = if(num($1) < 10, 'p0' & $1, 'p' & $1);

TRACE $(cpn(1);

let result=$(cpn(1));

trace $(result);                              // p01

let result=$(cpn(10));

trace $(result);                              // p10

1.png