Qlik Community

New to Qlik Sense

Discussion board where members can get started with Qlik Sense.

Highlighted
morenoju
Contributor III

Field not updating after automatic load

Hi guys,

I have the following piece of code in my load script:

[hotspots_currentperiod]:
LOAD
if (hour(now())>='6' AND hour(now())<'10',1,if (hour(now())>='16' AND hour(now())<'19',2,0)) As CurrentPeriod
INLINE [
CurrentPeriod
];

Basically, I want to divide the day in three periods:

- Between 6 and 10 AM it is period 1

- Between 4 PM and 7 PM period 2

- Rest: period 0

This was working OK until I published the app. It is right now 7:25 PM, and I'm reloading data every 5 minutes. CurrentPeriod=2 no matter what. 

Do you see anything wrong in my code?

Thanks

Labels (3)
1 Solution

Accepted Solutions

Re: Field not updating after automatic load

I guess what I am trying to understand is the value of Hour(Now()) or Time(Frac(Now())) when the if statement is getting evaluated...

for example... I ran this script

[hotspots_currentperiod]:
LOAD Now() as CurrentDateTime,
	 Time(Frac(Now())) as CurrentTime,
	 If(Frac(Now()) >= MakeTime(6) and Frac(Now()) < MakeTime(10), 1,
	 If(Frac(Now()) >= MakeTime(16) and Frac(Now()) < MakeTime(19), 2, 0)) as CurrentPeriod_Time,
	 Hour(Now()) as CurrentHour,
	 If(Hour(Now()) >= 6 and Hour(Now()) < 10, 1,
	 If(Hour(Now()) >= 16 and Hour(Now()) < 19, 2, 0)) as CurrentPeriod_Hour
AutoGenerate 1;

and I am seeing this

image.png

I am seeing that CurrentTime is 9:31:40 AM and Current Hour is 9... so it make sense that I am getting 1... what do you get when you run the above script? Share a screenshot similar to what I posted using the above script.

17 Replies

Re: Field not updating after automatic load

Now() function won't work.. can you tell me what is the purpose to use now() here?
Before develop something, think If placed (The Right information | To the right people | At the Right time | In the Right place | With the Right context)
morenoju
Contributor III

Re: Field not updating after automatic load

I used Now() because I wanted to check the current time.
E.g If it’s 19:45 now, the CurrentPeriod must be 0 because it’s later than 19:00.
Do you have any alternative function I can use?

Re: Field not updating after automatic load

Try this

[hotspots_currentperiod]:
LOAD 
If(Now() >= MakeTime(6) and Now() < MakeTime(10), 1,
If(Now() >= MakeTime(16) and Now() < MakeTime(19), 2, 0)) as CurrentPeriod
AutoGenerate 1;
MVP
MVP

Re: Field not updating after automatic load

Is your server in the same timezone as you are?
morenoju
Contributor III

Re: Field not updating after automatic load

Hi @sunny_talwar,

I tried that and the result was strange. CurrentPeriod=2 even though it's 8:36 AM right now.

Any other alternative?

Thanks

morenoju
Contributor III

Re: Field not updating after automatic load

Hi @tresesco.

Same timezone, yes.

Re: Field not updating after automatic load

Try running this

[hotspots_currentperiod]:
LOAD Now() as CurrentDateTime,
If(Now() >= MakeTime(6) and Now() < MakeTime(10), 1,
If(Now() >= MakeTime(16) and Now() < MakeTime(19), 2, 0)) as CurrentPeriod
AutoGenerate 1;

and see what value do you see for CurrentDateTime 

morenoju
Contributor III

Re: Field not updating after automatic load

12/5/2018 8:50:20 AM. The value I had expected.
I'm thinking of going back to use a variable. Something like
SET CurrentPeriod = If(Now() >= MakeTime(6) and Now() < MakeTime(10), 1, If(Now() >= MakeTime(16) and Now() < MakeTime(19), 2, 0))

Re: Field not updating after automatic load

Now can you try this

[hotspots_currentperiod]:
LOAD Now() as CurrentDateTime,
 Time(Frac(Now())) as CurrentTime,
 If(Frac(Now()) >= MakeTime(6) and Frac(Now()) < MakeTime(10), 1,
 If(Frac(Now()) >= MakeTime(16) and Frac(Now()) < MakeTime(19), 2, 0)) as CurrentPeriod
AutoGenerate 1;

Changed the if statement to use Frac(Now()) instead of Now()