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

getting previous year value with if statement

Hi all,

I'm migrating some reports from BusinessObjects to QlikView. I find difficult to calculate the previous year price for materials. I tried in two ways:
- using set analysis: =Sum({<YEAR = {$(vPY)}>} PRICE)
- using if statement: =sum(if(YEAR = vPY, PRICE)
where vPY = max(YEAR)-1

The problem is that if I use set analysis I don't get the values I expect (even there are little differences) because BusinessObjects uses If Statement in formulas and I imagine the set analysis logic is different from if statement logic.
I'd like to choose if statement way instead of set analysis but the problem is that my formula doesn't work. It displays no values in the column. Can you help me getting the previous year price value using if statement, please?
Another solution I was thinking was to calculate the value into the load script, but the PRICE is calculated based on fields from different tables and for a newbie like me it seems pretty difficult.

Any ideas?
thanks

2 Replies
Not applicable
Author

Hi !

You need to use double quotes, like this:

=Sum({<YEAR = {"$(vPY)"}>} PRICE)


or for static year


=Sum({<YEAR = {"$(=year(today())-1)"}>} PRICE)



Bye !


Oleg_Troyansky
Partner Ambassador/MVP
Partner Ambassador/MVP

Double quotes are only needed for search, so I don't believe that this is the problem in this case.

I think what's missing in the Set Analysis formula is the equal sign within the $-sign expansion. The variable contains a formula, hence the $-sign expansion needs to begin with an equal sign. Or, the other way - the variable can include the equal sign and then the set analysis won't need it:

using set analysis: =Sum({<YEAR = {$(=vPY)}>} PRICE)

where vPY = 'max(YEAR)-1'


or


using set analysis: =Sum({<YEAR = {$(vPY)}>} PRICE)

where vPY = '=max(YEAR)-1'


The formula with an IF has similar problems, but using IF is not recommended anyway, so I won't waste your time explaining it...


You can learn more about $-sign expansions from my recent blog:


http://www.naturalsynergies.com/q-tip-6-those-tricky-sign-expansions/


cheers,


Oleg Troyansky