Skip to main content
Announcements
Have questions about Qlik Connect? Join us live on April 10th, at 11 AM ET: SIGN UP NOW
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

question about LET variables

Hi,

my task is to define a LET variable that will return the maximal value in the field, and then to load only those records that correspond to the maximal value.

I'm trying the following syntax. But it doesn't work. Is there any mistake?

LET vMaxYear = max([Year]);
 
Source:
LOAD Brand,
Year
FROM
C:\Users\Larisa.Filonova\Desktop\brand_data.xlsx
(
ooxml, embedded labels, table is data)
where Year = $(vMaxYear)


Thank you in advance,


Larisa

1 Solution

Accepted Solutions
rubenmarin

Hi Larisa, max(Year) won't work in script to assign value to a variable, one way to get that is create a table of years in descendig order and get the value of the first record, ie:

Years:

LOAD Distinct Year as YearSorted Resident Data Order by Year desc;

LET vMaxYear = Peek('YearSorted', 0, 'Years');

DROP Table Years;

View solution in original post

2 Replies
rubenmarin

Hi Larisa, max(Year) won't work in script to assign value to a variable, one way to get that is create a table of years in descendig order and get the value of the first record, ie:

Years:

LOAD Distinct Year as YearSorted Resident Data Order by Year desc;

LET vMaxYear = Peek('YearSorted', 0, 'Years');

DROP Table Years;

hic
Former Employee
Former Employee

The first thing that happens during a script execution is that the old data model is deleted. Then the script starts building the new data model. The [Year] is a field in the old data model, and so it is not available during script execution.

You can do the following:

Alternative 1:

If you use

     Let vMaxYear = '=Max({1} Year)';

the variable will get calculated every time you click. But it will also keep its value during the script execution, so you can use it the way you want.

Alternative 2:

Calculate vMaxYear from the new data.

     MaxYear:

     Load Max(Year) as MaxYear From <SourceTable> ;

     Let vMaxYear = Peek('MaxYear',-1,'MaxYear');

Now you can use vMaxYear the way you want.

HIC