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

Announcements
Qlik GA: Multivariate Time Series in Qlik Predict: Get Details
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Variable troubles

Alright chaps.

I have a variable...

LET vMaxdate = '=max(MonthStart(prvMonthEnd))';

In a for loop I want to end up with...

vMaxdate_1=AddMonths('$(vMaxdate)',-11)

vMaxdate_2=AddMonths('$(vMaxdate)',-10)

vMaxdate_3=AddMonths('$(vMaxdate)',-9) etc.

FOR a = 1 to 12

  LET b = 12-$(a);

  LET vMaxdate_$(a) = '=AddMonths($(vMaxdate),-$(b))';

NEXT a;

But I am getting the text of vMaxdate in each variable...

vMaxdate_1=AddMonths(=max(MonthStart(prvMonthEnd)),-11)

vMaxdate_2=AddMonths(=max(MonthStart(prvMonthEnd)),-10)

vMaxdate_3=AddMonths(=max(MonthStart(prvMonthEnd)),-9) etc.

So how do I force the variable name rather than its expression into the second set of variables?

Thanks.

Foldy

1 Solution

Accepted Solutions
maxgro
MVP
MVP

SCRIPT

LET vMaxdate = 'max(MonthStart(prvMonthEnd))';

FOR a = 1 to 12

  LET b = 12-$(a);

  LET vMaxdate_$(a) = '=AddMonths(' & chr(39) & '$' & '(vMaxdate)' & chr(39) & ',-$(b))';

NEXT a;

RESULT

var.jpg

View solution in original post

8 Replies
alexandros17
Partner - Champion III
Partner - Champion III

I should try this:

LET vMaxdate = max(MonthStart(prvMonthEnd));

FOR a = 1 to 12

  LET b = 12-$(a);

  LET vMaxdate_$(a) = AddMonths($(vMaxdate),-$(b));

NEXT a;

Not applicable
Author

Thanks but that's not quite what I wanted. I think that will try and evaluate the AddMonths whereas I want the AddMonths as text along with the variable $(vMaxdate)...

MarcoWedel

Hi foldyman,

you might also consider using a single parameterized variable instead of the whole creating multible variables in a loop concept:

SET vMaxdate = '=AddMonths(max(MonthStart(prvMonthEnd)),'&$1&'-12)';

which could be used like

$(vMaxdate(1))  for a value of '=AddMonths(max(MonthStart(prvMonthEnd)),-11)'

$(vMaxdate(2))  for a value of '=AddMonths(max(MonthStart(prvMonthEnd)),-10)'

$(vMaxdate(3))   for a value of '=AddMonths(max(MonthStart(prvMonthEnd)),-9)'

...

which then could be calculated using this syntax:

$(=$(vMaxdate(1)))

hope this helps

regards

Marco

maxgro
MVP
MVP

SCRIPT

LET vMaxdate = 'max(MonthStart(prvMonthEnd))';

FOR a = 1 to 12

  LET b = 12-$(a);

  LET vMaxdate_$(a) = '=AddMonths(' & chr(39) & '$' & '(vMaxdate)' & chr(39) & ',-$(b))';

NEXT a;

RESULT

var.jpg

Not applicable
Author

Thanks Marco. Never seen parameterized variables before. I tried it out on my dash but it seems in this instance that performance really suffers. I'll be keeping the idea in my code snippets bank for the future tho. Thanks.

Not applicable
Author

Thanks Massimo, that seems to have done it though I didn't need the chr(39)'s. But...

why doesn't the first line evaluate the same as the second?

'=AddMonths($(vMaxdate),-$(b))';
'=AddMonths(' &  '$' & '(vMaxdate)'  & ',-$(b))';

It seems to me (a .net developer) that they should be the same as the second is just concatenated pieces of text?

MarcoWedel

interesting, I never noticed any performance influence of this.

Did you intentionally mark your own comment helpful?

😉

regards

Marco

Not applicable
Author

Errr yes. obviously getting click happy in my old age. Unmarked now tho.

Perhaps I was doing something wrong - I'll try it again. Thanks.