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

Announcements
Want to see what's currently in public preview? We've pulled it all together in one place. Check it out here
cancel
Showing results for 
Search instead for 
Did you mean: 
johngouws
Partner - Specialist
Partner - Specialist

Dynamic update variable to use in Where clause

Good morning.

I have a case where I need to load data for specific customers. The difference from the 'norm' is that for discussion purposes, different codes are loaded on each Monday of the month. I have this working manually when I set a variable before the load. eg: SET vReportWeek = 1;
However, I would like to make this dynamic. I have the code to identify the relevant Monday, but when I try to use the value in the varaiable is where I am having the issue.

My Load script is below with the variable set manually.

SET vReportWeek = 1;
//SET vReportWeek = "=max(Week_no)";

t0:
Load * INLINE [
Code , Monday1 ,Monday2 ,Monday3 ,Monday4
AAA000 , x , , ,x
AAB000 , ,x , ,x
AAC000 , , ,x ,x
AAD000 , x , , ,x
AAE000 , ,x , ,x
];

NoConcatenate

t1:
Load
*,
if(DayName(MonthStart(Today()),Mod(7-WeekDay(MonthStart(Today())),7)) = Today(), 1,
if(DayName(MonthStart(Today()),Mod(7-WeekDay(MonthStart(Today())),7)+7) = Today(), 2,
if(DayName(MonthStart(Today()),Mod(7-WeekDay(MonthStart(Today())),7)+14) = Today(), 3,
if(DayName(MonthStart(Today()),Mod(7-WeekDay(MonthStart(Today())),7)+21) = Today(), 4,
if(DayName(MonthStart(Today()),Mod(7-WeekDay(MonthStart(Today())),7)+28) = Today(), 4,
1 //Normally 0 - 1 for testing
))))) as Week_no
Resident t0;
Drop Table t0;

NoConcatenate

t2:
Load
*
Resident t1
Where Monday$(vReportWeek) = 'x';
Drop Table t1;


Thank you - I appreciate any advice.

 

Labels (1)
1 Solution

Accepted Solutions
MayilVahanan
MVP
MVP

Hi

Try like below, hope it helps

t1:
Load
*,
if(DayName(MonthStart(Today()),Mod(7-WeekDay(MonthStart(Today())),7)) = Today(), 1,
if(DayName(MonthStart(Today()),Mod(7-WeekDay(MonthStart(Today())),7)+7) = Today(), 2,
if(DayName(MonthStart(Today()),Mod(7-WeekDay(MonthStart(Today())),7)+14) = Today(), 3,
if(DayName(MonthStart(Today()),Mod(7-WeekDay(MonthStart(Today())),7)+21) = Today(), 4,
if(DayName(MonthStart(Today()),Mod(7-WeekDay(MonthStart(Today())),7)+28) = Today(), 4,
1 //Normally 0 - 1 for testing
))))) as Week_no
Resident t0;
Drop Table t0;

temp:
Load Max(Week_no) as MaxWk Resident t1;

LET vReportWeek = peek('MaxWk');

TRACE vReportWeek -- $(vReportWeek);

NoConcatenate

t2:
Load
*
Resident t1
Where Monday$(vReportWeek) = 'x';
Drop Table t1, temp;

Thanks & Regards, Mayil Vahanan R
Please close the thread by marking correct answer & give likes if you like the post.

View solution in original post

3 Replies
MayilVahanan
MVP
MVP

Hi

Try like below, hope it helps

t1:
Load
*,
if(DayName(MonthStart(Today()),Mod(7-WeekDay(MonthStart(Today())),7)) = Today(), 1,
if(DayName(MonthStart(Today()),Mod(7-WeekDay(MonthStart(Today())),7)+7) = Today(), 2,
if(DayName(MonthStart(Today()),Mod(7-WeekDay(MonthStart(Today())),7)+14) = Today(), 3,
if(DayName(MonthStart(Today()),Mod(7-WeekDay(MonthStart(Today())),7)+21) = Today(), 4,
if(DayName(MonthStart(Today()),Mod(7-WeekDay(MonthStart(Today())),7)+28) = Today(), 4,
1 //Normally 0 - 1 for testing
))))) as Week_no
Resident t0;
Drop Table t0;

temp:
Load Max(Week_no) as MaxWk Resident t1;

LET vReportWeek = peek('MaxWk');

TRACE vReportWeek -- $(vReportWeek);

NoConcatenate

t2:
Load
*
Resident t1
Where Monday$(vReportWeek) = 'x';
Drop Table t1, temp;

Thanks & Regards, Mayil Vahanan R
Please close the thread by marking correct answer & give likes if you like the post.
justISO
Specialist
Specialist

Hi, what about instead of

SET vReportWeek = 1;

use

LET vReportWeek = Ceil(Day(Today())/7);

johngouws
Partner - Specialist
Partner - Specialist
Author

Thank you @MayilVahanan . 
That is exactly what I needed. ✔ I appreciate it very much. 

@justISO - Your solution also works, but the /7 ties me into the first week. The solution from @MayilVahanan I think gives me more options. 

Thanks very much.