User-defined functions

Apart from strings, numbers, and system functions, variables can hold user-defined functions. 

A user-defined function is a group of calculations with parameters that are stored in variables. These variables can then be used throughout the script or in any application. As the variables hold functions or calculations, the user can pass parameters that will then be used to calculate the output.

User-defined functions are stored in variables and thus must be created with the prefix SET or LET when using script code or from the Variable overview in the application edit mode.

Let's use the following user-defined variable as an example:

SET vFormatNumber = if($1 >=0 AND $1 <=999, round($1,0.1), if($1 >=1000 and $1 <=999999, round($1/1000,0.1) & 'K', if($1 >=1000000, round($1/1000000, 0.1)&'M', $1)));

In the preceding example, we created a user-defined function vFormatNumber that formats numbers in thousands (K) or millions (M) and rounds them to the nearest decimal. The parameter $1 will hold the numbers that are sent to the function when it is called.

The following block of code shows a clear example of how to use a user-defined variable within a script:

SET vFormatNumber = if($1 >=0 AND $1 <=999, round($1,0.1), if($1 >=1000 and $1 <=999999, round($1/1000,0.1) & 'K', if($1 >=1000000, round($1/1000000, 0.1)&'M', $1)));


DataTable:
LOAD
$(vFormatNumber(Sales)) AS SalesFormatted,
Sales
INLINE
[
Sales
-1500
1875
180000
1570000
];

Note how we call the function we first defined within the LOAD statement. It's as easy as that. The great thing about user-defined variables is that you can store them in a file so you can reuse them in any script.

In the example, $1 represents the first parameter; however, user-defined functions can hold more than one parameter. The second parameter is replaced with $2, the third with $3, and so on.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.245.212