Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
Kasper_Declercq
Contributor
Contributor

Dynamic Variables (Reuse code as much as possible)

Hello, 

I 'm changing an existing app were we have a lot of almost the same content within our variables. 


There is a computations which calculates the total cost increase/decrease for a certain region (4 regions in total) against a given reference period. 
Each computation is displayed therefore there are 4 variables.

General formula is something like: 
((ReportingYear) - (Reference period)) / (Reference period)

Each part in the above formula is given by the next part (Just changing Country code or Zone or the Year:
sum(
{$<
DimLocation.Country = {'....'}
, DimLocation.Zone = {''}
, DimCalendar.Year = {"$(=UV_ReportingYear)"}
, .... // Extra filtering that is constantly the same.
>}
Cost )

Depending on the region we use Country or Zone. (In country there is also a list that is used = "BE","NL", "FR", "DE")

I know I can split up the main parts of the formula in different variables to reuse them but it makes the amount of variables grow very big and makes is messy to do a little change. 

Instead I would like to use some kind of dynamic function to give the changing parts as parameter. 

((

$(ComputeCost("DimLocation.Country = {'BE','NL','DE,'FR'}", '$(uv_ReportingYear)'))
- $(ComputeCost("DimLocation.Country = {'BE','NL','DE,'FR'}", '$(uv_ReportingYear -1)'))

)/  $(ComputeCost("DimLocation.Country = {'BE','NL','DE,'FR'}", '$(uv_ReportingYear -1)'))
 
Is something like this possible in Qlik? 
How is this possible? 

Labels (1)
1 Solution

Accepted Solutions
marcus_sommer

Variables with parameter are possible and even the nesting of variables - whereby the last could become quite complicated because each comma is always treated as parameter-delimiter which couldn't be masked else only bypassed with doubled replacing-approaches or syntax which avoids the commas. So be careful not to make simple things more complex as sensible.

A parametrized variable may look like:

var:
$1({$2 < $3 = {$4}>} $5)

and called:

$(var(sum, $, Country, ("BE"|"NL"), Cost))

View solution in original post

1 Reply
marcus_sommer

Variables with parameter are possible and even the nesting of variables - whereby the last could become quite complicated because each comma is always treated as parameter-delimiter which couldn't be masked else only bypassed with doubled replacing-approaches or syntax which avoids the commas. So be careful not to make simple things more complex as sensible.

A parametrized variable may look like:

var:
$1({$2 < $3 = {$4}>} $5)

and called:

$(var(sum, $, Country, ("BE"|"NL"), Cost))