Skip to main content
Woohoo! Qlik Community has won “Best in Class Community” in the 2024 Khoros Kudos awards!
Announcements
Nov. 20th, Qlik Insider - Lakehouses: Driving the Future of Data & AI - PICK A SESSION
cancel
Showing results for 
Search instead for 
Did you mean: 
nzcncfan
Contributor II
Contributor II

How to create User-Defined function and call it?

Hi, I would like to create a function to scramble a few fields. A few questions:

1. Should I use Sub for Function key word? See below example, which don't have detailed logic.

Sub Scramble (field)
       Scramble = UCase(field)
End Sub;

2. Can this function be placed in a block within the script editor?

3. How do I call this function? I called this function using below script, but it failed. Any suggestions?

Scramble(ContactName) AS Name

1 Solution

Accepted Solutions
nzcncfan
Contributor II
Contributor II
Author

That's a good idea. Thanks.
It's fine using variable for simple algorithm, but what about more complex logic is required.

View solution in original post

5 Replies
nzcncfan
Contributor II
Contributor II
Author

I have to scramble the text string in the SQL function, but would really like to know how to create and call function in Qlik.

A simple working example is most helpful. I am struggling to find such a sample, as there is very limited resources on web.

Thanks for your time!

mfarsln
Creator II
Creator II

Hi,
You can set a dynamic variable. For example;

Set vFirstXLetters = mid($1,1,len($2)

After you declare this variable in script editor, you can use this variable like any function. 

=$(vFirstXLetters('qliksense',1,'qlik') this will return 'qlik' as a result.

And you can use this variable in script editor.

set vOmitLetters = mid($1,len($2)+1,len($1));

load 
*,
$(vOmitLetters(country,prefix)) as CountriesWithoutPrefix
inline
[
country, prefix
germany,ger
england,eng
sweden,swe
]

 

I know the example is pretty much simple but it should be your starting point.

 

nzcncfan
Contributor II
Contributor II
Author

That's a good idea. Thanks.
It's fine using variable for simple algorithm, but what about more complex logic is required.
LucaFe
Partner - Contributor
Partner - Contributor

Thank you for the answer. However, how can I set up a function in the editor (like yours vFirstXLetters) and then repeatedly use this function in tables and charts? 

mfarsln
Creator II
Creator II

Hi,

Sorry for late answer. Adding .qvf file to check how to use custom functions on your tables and charts.