Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
microwin88x
Creator III
Creator III

Get the Maximum Number of Consecutive Periods

Hello!

I have a table with Accounts and Periods (YYYY/MM) and I need to get the maximum number of consecutive Periods for each Account. For example, in the following table:

01.png

The result would be:

02.png

For Account 1000000001, the result is 5 because it obtains the Periods: 201306, 201307, 201308, 201309 and 201310.

(201301, 201302 and 201303 are not considerated because despite they are consecutive, the count would be only 3).

The same happens to the other Accounts.

Notes:

I would need this as an expression in any table object instead of in script because the Accounts and Periods are dynamic (they vary) for a corresponding selection in the application.

Thank you!!!

1 Reply
maxgro
MVP
MVP


1.png


SCRIPT

Temp:

Load * Inline

[

  Account, Period

  1000000001, 201301

  1000000001, 201302

  1000000001, 201303

  1000000001, 201306

  1000000001, 201307

  1000000001, 201308

  1000000001, 201309

  1000000001, 201310

  1000000002, 201301

  1000000002, 201302

  1000000002, 201303

  1000000002, 201304

  1000000002, 201310

  1000000002, 201311

  1000000002, 201312

  1000000003, 201301

  1000000003, 201305

  1000000003, 201307

];

NoConcatenate

load

  *,

  if(Account=Peek(Account)

  and

  (

  (Period=(Peek(Period)+1))

  or

  (

  left(Period,4)=left(peek(Period),4)+1

  and Right(Period,2)='01'

  and Right(peek(Period),2)='12'

  )

  ),

  peek(NumConseq)+1, 1) as NumConseq

resident

Temp

order by Account, Period;



The script  shoul work also wiith different years like (bold)

Temp:

Load * Inline

[

  Account, Period

  1000000001, 201301

  1000000001, 201302

  1000000001, 201303

  1000000001, 201306

  1000000001, 201307

  1000000001, 201308

  1000000001, 201309

  1000000001, 201310

  1000000001, 201311

  1000000001, 201312

  1000000001, 201401

  1000000002, 201301

  1000000002, 201302

  1000000002, 201303

  1000000002, 201304

  1000000002, 201310

  1000000002, 201311

  1000000002, 201312

  1000000002, 201402

  1000000002, 201403

  1000000003, 201301

  1000000003, 201305

  1000000003, 201307

];