# New to Qlik Sense

Discussion board where members can get started with Qlik Sense.

Not applicable

## Variables for current year and previous year

I have two variables for current year and previous year,

Let vmaxyear='Max(Year)';
Let vpreyear = 'Max(Year)-1';

so how can I write an expression for

Current year - Previous year / Previous Year

9 Replies

## Re: Variables for current year and previous year

I wouldn't use variables for this. Variables are good for things which are frequently used or which are long and complex and not for very easy things - it's cluttered only the variable-overview. I would query this directly within a set analysis:

sum({< Year = {"\$(=max(Year))"}>} value) - sum({< Year = {"\$(=max(Year)-1)"}>} value)

- Marcus

Not applicable

## Re: Variables for current year and previous year

I need to use multiple variables in the single set analysis so only I asked that question by giving small example

any other idea to create variables and to work out them properly

## Re: Variables for current year and previous year

If you want to use variables I would create these both variables within the gui:

vmaxyear:

= Max(Year)

vpreyear:

= Max(Year)-1

and then:

sum({< Year = {"\$(vmaxyear)"}>} value) - sum({< Year = {"\$(vpreyear)"}>} value)

Here is an excellent explanation how to create variables: The Magic of Variables.

- Marcus

Not applicable

## Re: Variables for current year and previous year

yes Marcus Thank you,

however same thing I done it ,

is not working properly for me,

what I done is

i took one text object gave first expression =sum({< Year = {"\$(vmaxyear)"}>} value)

in another text object gave second one =  sum({< Year = {"\$(vpreyear)"}>} value)

if you take both the expressions ,

they are not working properly it is giving same value
without variables the value is:
for ex:
first one:3000
second one:5000

With Variables

for ex:

first one: 3000
Second one :3000

here not changing the value I hope you understand my problem

## Re: Variables for current year and previous year

This looked that the variables aren't correct. Please check them again maybe by putting them within a textbox and they should be different. If this don't worked create a small example with a small inline-table and those variables and textboxes + charts.

- Marcus

Not applicable

## Re: Variables for current year and previous year

Hi Marcus,

in the edit script I created two variables like this:

vmaxyear='max(Year)';
vprreyear='max(Year)-1';

and I took two bar charts one is for current year salaries, and one is for last year salaries
current I gave expression like this:sum({< Year_P = {"\$(vmaxyear)"}>} Salaries)

last year I gave expression like this: sum({< Year_P = {"\$(vpreyear)"}>} Salaries)

can you correct me where I have done mistake?
if you select 2008 or 2009 two bar charts showing same value

## Re: Variables for current year and previous year

If you creates those variables within the script the variables contain no year-number else the expression as string. To use such variables you need \$-sign expansion to get the string converted to an expression:

sum({< Year = {"\$(=\$(vmaxyear))"}>} value) - sum({< Year = {"\$(=\$(vpreyear))"}>} value)

Beside the above mentioned post The Magic of Variables see also The Magic of Dollar Expansions.

- Marcus

Not applicable

## Re: Variables for current year and previous year

thank you marcus for your response,

I gave like this is it correct?

let vmaxyear=' sum({< Year_P = {"\$(=\$(vmaxyear))"}>}Salaries)';

let vpreyear= 'sum({< Year_P = {"\$(=\$(vmaxyear)-1)"}>}Salaries)';

in the bar chart I had mention this   \$(vmaxyear) as a measure right?

if above one is right I am not getting correctly?

## Re: Variables for current year and previous year

No, it's not correct. The \$-sign expansion has to happens within the gui-expression and not by creating the variables within the script. I suggest you takes some minutes to read the both above mentioned postings which explain it very well which kinds of variable-creations are possible, how they differs to each other and how you could access these variables.

- Marcus