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: 
Not applicable

Is it possible to use variables in the script, within an IF statement?

Hi guys,

I wonder if it is possible to get things working here. What I need to know is, if it is possible to get your If-Statement answers replaced by variables already defined.



E.g:
LOAD
Price,
Article,
If(Year = ('A'), x, y)
FROM
C:\,,,

Variables:
for x = CurrentYear =Year(today())
for y = PreviousYear =Year(today()-365)

This works propably, but there is a problem replacing the x's and y's with a variable.
My Goal is to see the value for "CurrentYear" where "A" is in Year in my table.
I tried almost everything... defining it with $(CurrentYear) but I get an error everytime.
How does the proper Command would look like?

Thanks,



1 Solution

Accepted Solutions
IAMDV
Master II
Master II

You should define the variables with 'Let' keyword. Something like this...

Let vCurrentYear =Year(today())

And you can call the variables by using the below syntax...

=$(vCurrentYear)

I hope this helps!

View solution in original post

6 Replies
IAMDV
Master II
Master II

Please read the below post. I had used variables as user defined functions by passing the input value and returning a scalar value as output. I hope this should help you...

http://community.qlik.com/forums/t/43601.aspx

You can also post sample QV document with fictitious data and one of us will help you...

IAMDV
Master II
Master II

You should define the variables with 'Let' keyword. Something like this...

Let vCurrentYear =Year(today())

And you can call the variables by using the below syntax...

=$(vCurrentYear)

I hope this helps!

Not applicable
Author

Writing an '=' before the $ makes the Synthax light up red!

Thats how I typed it in:
If(Year = ('A'), $(CurrentYear), $(PreviousYear))

Any clue?
Thx
That's my error message;
Error in expression: ')' expected
E.g.:
LOAD Price,
Article,
If(Year = ('A'), =Year(today()), =Year(today()-365))
FROM
C:\....xlsx
(ooxml, embedded labels, table is Tabelle1)

As far as I can tell, Qlikview just won't use the value of the variables I defined.




Not applicable
Author

You don't have to use the "=" sign before the variable.

In you case you will have to do the next:

if ( Year = 'A' , $(CurrentYear) , $(PreviousYear) )

Not applicable
Author

With the Let statement it works just fine!

Thank you all

IAMDV
Master II
Master II

I am assuming that you are declaring the variable before the Load Script (Because QV reads from Left to Right and Top to Bottom). Please don't use the '=' in the load script and try using ($(vCurrentYear)) instead of $(vCurrentYear)