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

Announcements
Discover how organizations are unlocking new revenue streams: Watch here
cancel
Showing results for 
Search instead for 
Did you mean: 
Programmer77
Contributor
Contributor

How to find the number of consecutive records which has zero or unknown values

I have a data set like this 

Columns:

Id | date | value 

Value contains 0 and unknown. I would like to find the records having consecutive zero or unknown 

I have tried the below script:

If(value=0 and previous(value)=0 , missing 

Ordered by date

But it does not work as i need to flag more than 7 consecutive 0 or unknown based on order of date

Can anyone suggest any solution for this

 

 

Labels (2)
1 Solution

Accepted Solutions
vinieme12
Champion III
Champion III

@Programmer77 please close the thread by accepting a response as solution

Vineeth Pujari
If a post helps to resolve your issue, please accept it as a Solution.

View solution in original post

5 Replies
MayilVahanan

HI Try like below

Load *, if(id = Peek(id) and ((value = 0 or value = 'unknown') and (peek(value)=0 or Peek(value)='unknown')), peek('missing')+1, 1) as missing Inline
[
id, date, value
1, 12/01/2022, 10
1, 13/01/2022, 0
1, 14/01/2022, 0
1, 15/01/2022, unknown
1, 16/01/2022,0
1, 17/01/2022, 0
1, 18/01/2022, 0
1, 19/01/2022, 0
1, 20/01/2022, 14
2, 12/01/2022, 11
2, 13/01/2022, 0
2, 14/01/2022, 0
];

MayilVahanan_0-1650705492645.png

 

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

A slightly simpler logic 🙂

if(id = Peek(id) ,if(WildMatch(value,0,'Unknown'),peek('missing')+1),0) as missing

 

Also you need to order by both ID,Date

Vineeth Pujari
If a post helps to resolve your issue, please accept it as a Solution.
Programmer77
Contributor
Contributor
Author

Hi .Thank you very much for the help.  I wanted to check if I can just flag for the ones which has the 0 or unknown values . My final output should have the dates range and the number of missing values.

Output example:

Id | Daterange|number of missing values

1 | 25april-29apri| 4

 

 

 

 

 

vinieme12
Champion III
Champion III

Full script;

 

modified the data to test multiple daterange of missing values in same ID

Also assuming your raw table is already sorted by ID and Date Ascending

raw:
Load *
,if(WildMatch(value,0,'Unknown'),if(id=peek(id),alt(peek('missing'),0)+1,1),0) as missing
,if(WildMatch(value,0,'Unknown'),if(id=peek(id),if(peek('startdate')=0,date,peek('startdate'))),0) as startdate
Inline
[
id, date, value
1, 12/01/2022, 10
1, 13/01/2022, 0
1, 14/01/2022, 0
1, 15/01/2022, unknown
1, 16/01/2022,10
1, 17/01/2022, 0
1, 18/01/2022, 0
1, 19/01/2022, 0
1, 20/01/2022, 14
2, 12/01/2022, 11
2, 13/01/2022, 0
2, 14/01/2022, 0
];

NoConcatenate
temp:
load
id
,startdate&'-'&date as Daterange
,missing as missingcount
resident raw
Where Previous(startdate)=0
order by id,date desc
;
drop table raw;

 

exit script;

outputoutput

 

Vineeth Pujari
If a post helps to resolve your issue, please accept it as a Solution.
vinieme12
Champion III
Champion III

@Programmer77 please close the thread by accepting a response as solution

Vineeth Pujari
If a post helps to resolve your issue, please accept it as a Solution.