Skip to main content
Announcements
Introducing Qlik Answers: A plug-and-play, Generative AI powered RAG solution. READ ALL ABOUT IT!
cancel
Showing results for 
Search instead for 
Did you mean: 
JSamuel_MHV
Contributor
Contributor

How to pass a function to a custom variable

I've created a custom variable that accepts a date parameter and returns a value. It works as expected when a date is passed through to the function, but doesn't work when a function is passed through.

For example, my custom variable VDate_Calc is defined as: monthname($1). Passing today's date to the variable like so: =$(vDate_Calc(Today())) returns Apr 2020 as expected. However, I receive an error when I try passing a function through to the variable like so: =$(vDate_Calc(AddMonths(Today(),1)))

The error message reads "Error: Error in expression: AddMonths takes 2-3 parameters". However AddMonths(Today(),1) returns the correct value 24/05/2020.

 

Any suggestions on how to fix this error?

Thanks!

3 Replies
marcus_sommer

The comma from the function will be interpreted as a parameter-delimiter to the variable and therefore it failed. This means you need to ensure that the function is calculated within the variable or you need a different approach. To the first you could try something like this:

$(vDate_Calc($(=AddMonths(Today(),1))))

- Marcus

JSamuel_MHV
Contributor
Contributor
Author

This approach doesn't raise an error, but it returns an incorrect value = Dec 1899. Looks like the AddMonths function doesn't work properly when nested within a custom variable?

marcus_sommer

My suggestion should work within the UI - within the script the feature of creating an adhoc variable with $(= …) isn't available. In general is the approach to nest variables especially if they should contain functions difficult and the efforts to get the various syntax-requirements working much bigger as keeping the logics more simple.

- Marcus