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

Announcements
Want to see what's currently in public preview? We've pulled it all together in one place. Check it out here
cancel
Showing results for 
Search instead for 
Did you mean: 
IAMDV
Master II
Master II

How to encapsulate complex expressions in to variable?

I have few complex expressions in my document. I wanted to assign this expression calculation to variable or like a UDF?

Thanks in advance.

Cheers - DV

Labels (1)
1 Solution

Accepted Solutions
gandalfgray
Specialist II
Specialist II

To encapsulate expressions in a variable simply do like this:


Set eValue = 'if(vFlag = 1, Value1, Value2)';


You can also use previousy defined variables inside variables, like this for instance:


Let CurrentMonth = Left(PurgeChar(vToDay,'-'),6);
Let SelectedCurrentMonth = CurrentMonth;
Let PrevMonth = Left(PurgeChar(AddMonths(vToDay,-1),'-'),6);
Set ExclDateSelectsAll = 'Year=, Date=, YearQtr=, WeekYear=, Month=, Quarter=, Week=, WeekDay=, Day=';
Set eValue = 'if(vFlag = 1, Value1, Value2)';
Set eInvoiced = 'if(SelectedCurrentMonth=CurrentMonth,
Sum({$<YearMonth={"$(CurrentMonth)"}, $(ExclDateSelectsAll)>} $(eValue)),
Sum({$<YearMonth={"$(PrevMonth)"}, $(ExclDateSelectsAll)>} $(eValue)))';


When you want to access the values of these expressions you need to use dollar-sign expansion, like this: $(eInvoiced)

Also note that when using a variable inside the definition of another variable you should also use dollar-sign expansion, like in my examples.

View solution in original post

3 Replies
gandalfgray
Specialist II
Specialist II

To encapsulate expressions in a variable simply do like this:


Set eValue = 'if(vFlag = 1, Value1, Value2)';


You can also use previousy defined variables inside variables, like this for instance:


Let CurrentMonth = Left(PurgeChar(vToDay,'-'),6);
Let SelectedCurrentMonth = CurrentMonth;
Let PrevMonth = Left(PurgeChar(AddMonths(vToDay,-1),'-'),6);
Set ExclDateSelectsAll = 'Year=, Date=, YearQtr=, WeekYear=, Month=, Quarter=, Week=, WeekDay=, Day=';
Set eValue = 'if(vFlag = 1, Value1, Value2)';
Set eInvoiced = 'if(SelectedCurrentMonth=CurrentMonth,
Sum({$<YearMonth={"$(CurrentMonth)"}, $(ExclDateSelectsAll)>} $(eValue)),
Sum({$<YearMonth={"$(PrevMonth)"}, $(ExclDateSelectsAll)>} $(eValue)))';


When you want to access the values of these expressions you need to use dollar-sign expansion, like this: $(eInvoiced)

Also note that when using a variable inside the definition of another variable you should also use dollar-sign expansion, like in my examples.

IAMDV
Master II
Master II
Author

Quick question - Where am I declaring these variables? I mean is it under - Edit Module or Variable Overiew window?

Thanks again.

Cheers - DV

gandalfgray
Specialist II
Specialist II

Hi,

I would do it in Edit Script (Ctrl-E) (that is: in the load script)

(maybe in variable overview, but not in Edit Module anyway)

Smile