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

Announcements
Discover the Trends Shaping AI in 2026: Register Here!
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Dynamic periods

Hi,

I'm facing some difficulties in creating dynamic period. I like to aggregate data in 30 days periods for a specific customer, and I'd like to create a period number for that customer. As an example look at the data structure below. The first period for one specific customer starts with his first appearance. All appearances for that customer withion 30 days is in period 1. Period 2 for that customer should start with his first appearance without his first 30 days period, so that period might start 31 days from the first as well as 300 days from the first

IdDatePeriod
12008-03-011
12008-03-041
12009-01-142
12009-03-153
22009-01-141
22009-01-151
22009-01-161
22009-01-171
22009-01-181
22009-01-191
22009-01-201
22009-01-211


Does anyone have a solution to this problem, please let me know!

1 Solution

Accepted Solutions
johnw
Champion III
Champion III

This seems to do the trick:

,if(Id<>previous(Id) or Date-peek(PeriodStart)>30,Date,peek(PeriodStart)) as PeriodStart
,if(Id<>previous(Id),1,peek(Period)-(Date-peek(PeriodStart)>30)) as Period

It probably needs a little explanation, though. Previous() looks at the previous record in the SOURCE table. Peek() looks at the previous record in the table you are currently creating. Conditions evaluate to -1 for true, 0 for false, so when I subtract (Date-peek(PeriodStart)>30), this is shorthand for adding if(Date-peek(PeriodStart)>30,1,0).

View solution in original post

2 Replies
johnw
Champion III
Champion III

This seems to do the trick:

,if(Id<>previous(Id) or Date-peek(PeriodStart)>30,Date,peek(PeriodStart)) as PeriodStart
,if(Id<>previous(Id),1,peek(Period)-(Date-peek(PeriodStart)>30)) as Period

It probably needs a little explanation, though. Previous() looks at the previous record in the SOURCE table. Peek() looks at the previous record in the table you are currently creating. Conditions evaluate to -1 for true, 0 for false, so when I subtract (Date-peek(PeriodStart)>30), this is shorthand for adding if(Date-peek(PeriodStart)>30,1,0).

Not applicable
Author

Thanks a lot, It seems to work just perfect.