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: 
QuentinDeci
Partner - Contributor
Partner - Contributor

Cumulative flag=1

Hello everybody,

I have a question for you:

I search to calculate the cumulative number of days where the flag = 1 (if the flag = 0 the cumulative stop).

For exemple:

magcugdateflag
1101009/06/20211
1101009/05/20211
1101009/04/20211
1101009/03/20210
1101009/02/20211
1121209/06/20211
1121209/05/20211
1121209/04/20210
1121209/03/20210
1121209/02/20211

 

So the table with the cumulative calcul (based on today() so change at every load) will be :

magcugNb Cumulative
110103
112122

 

Thank you 

Have a good day

1 Solution

Accepted Solutions
chrismarlow
Specialist II
Specialist II

Hi,

I would start with joining the last date with zero in flag to the data (so code similar to below) & then you have choices depending what you want to do (you could create a new flag that is only 1 where date>maxdate and sum, you could aggregate that into a separate table ...).

Cheers,

Chris.

data:
load * Inline [
mag, cug, date,flag
1, 1010, 06/09/2021,1
1, 1010, 05/09/2021,1
1, 1010, 04/09/2021,1
1, 1010, 03/09/2021,0
1, 1010, 02/09/2021,1
1, 1212, 06/09/2021,1
1, 1212, 05/09/2021,1
1, 1212, 04/09/2021,0
1, 1212, 03/09/2021,0
1, 1212, 02/09/2021,1
];

maxzero:
Load
	mag,
	cug,
	max(date) as maxdate
resident data
where flag=0
group by mag,
	cug;

left join (data)
Load *
resident maxzero;

drop table maxzero;

 

View solution in original post

3 Replies
chrismarlow
Specialist II
Specialist II

Hi,

I would start with joining the last date with zero in flag to the data (so code similar to below) & then you have choices depending what you want to do (you could create a new flag that is only 1 where date>maxdate and sum, you could aggregate that into a separate table ...).

Cheers,

Chris.

data:
load * Inline [
mag, cug, date,flag
1, 1010, 06/09/2021,1
1, 1010, 05/09/2021,1
1, 1010, 04/09/2021,1
1, 1010, 03/09/2021,0
1, 1010, 02/09/2021,1
1, 1212, 06/09/2021,1
1, 1212, 05/09/2021,1
1, 1212, 04/09/2021,0
1, 1212, 03/09/2021,0
1, 1212, 02/09/2021,1
];

maxzero:
Load
	mag,
	cug,
	max(date) as maxdate
resident data
where flag=0
group by mag,
	cug;

left join (data)
Load *
resident maxzero;

drop table maxzero;

 

QuentinDeci
Partner - Contributor
Partner - Contributor
Author

Hey Chris,

Thank you very much for your quicky answer !

Sometimes i search difficult solution (whith previous() or while...) but the simplicity is the most time the best!

Have a good day

chrismarlow
Specialist II
Specialist II

Thanks, glad it helped.