Skip to main content
Announcements
Introducing a new Enhanced File Management feature in Qlik Cloud! GET THE DETAILS!
cancel
Showing results for 
Search instead for 
Did you mean: 
nbuchholz1
Contributor II
Contributor II

Calculate the Number of Consecutive Weeks of WoW Declines in Sales

I'm trying to count the number of consecutive weeks that the Week on Week change in Sales is < 0 (basically the number of weeks in a row that sales have declined). I think this should be done in the load script, but I'm not entirely sure how to approach!

My data table would look something like this: 

WeekYearSum of sales
4120201000
402020129
392020477823
38202078324
3720207378
36202027834
35202038783
342020287812
332020237878
322020237878
312020238
3020204637
292020834728
2820206738
27202023478
26202024878
2520202767
24202059477
23202034783
2220207348
2120205789


Thanks in advance for any input

Labels (5)
3 Solutions

Accepted Solutions
nbuchholz1
Contributor II
Contributor II
Author

@Taoufiq_Zarra  The output would be something like this: 

WeekYearSum of ReadsChange from Week PriorIs Negative DeclineConsecutive Weeks Of Sales Decline
4120201000675%00
402020129-100%11
392020477823510%00
38202078324962%00
3720207378-73%13
36202027834-28%12
35202038783-87%11
34202028781221%00
3320202378780%11
32202023787899849%00
312020238-95%12
3020204637-99%11
29202083472812288%00
2820206738-71%12
27202023478-6%11
26202024878799%00
2520202767-95%11
2420205947771%00
23202034783373%00
222020734827%00
2120205789-0 


I'm pretty clear how I would address this if I just needed to build a table of data going forward.  However, the challenge is I need to run this analysis on my historical data. I believe I somehow need to iterate through the whole time series to come up with these calculations, but I'm not sure how to do that in Qlik. 

View solution in original post

Taoufiq_Zarra

@nbuchholz1  Maye be this:

Data:
LOAD * INLINE [
    Week, Year, Sum of sales
    41, 2020, 1000
    40, 2020, 129
    39, 2020, 477823
    38, 2020, 78324
    37, 2020, 7378
    36, 2020, 27834
    35, 2020, 38783
    34, 2020, 287812
    33, 2020, 237878
    32, 2020, 237878
    31, 2020, 238
    30, 2020, 4637
    29, 2020, 834728
    28, 2020, 6738
    27, 2020, 23478
    26, 2020, 24878
    25, 2020, 2767
    24, 2020, 59477
    23, 2020, 34783
    22, 2020, 7348
    21, 2020, 5789
];

Tmp:
noconcatenate

load * resident Data order by Year,Week;

drop table Data;


Final:
noconcatenate

load *,if([Is Negative Decline]='1' and peek([Is Negative Decline])=[Is Negative Decline],peek([Consecutive Weeks Of Sales Decline])+1,[Is Negative Decline]) as [Consecutive Weeks Of Sales Decline];

load *,Num(if(Year=peek(Year),([Sum of sales]-peek([Sum of sales]))/peek([Sum of sales])),'# ##0%') as [Change from Week Prior]
,if(Num(if(Year=peek(Year),([Sum of sales]-peek([Sum of sales]))/peek([Sum of sales])),'# ##0%')<0,'1','0') as [Is Negative Decline]

  resident Tmp;

drop table Tmp;

 

output:

Capture.PNG

Regards,
Taoufiq ZARRA

"Please LIKE posts and "Accept as Solution" if the provided solution is helpful "

(you can mark up to 3 "solutions") 😉

View solution in original post

Gabriel
Partner - Specialist III
Partner - Specialist III

@nbuchholz1 

Please see script below and hopefully it helps you.

tmp:
LOAD
"Week",
"Year",
"Sum of sales"
FROM [lib://CommunityQuestion]
(html, utf8, embedded labels, table is @1);

NOCONCATENATE
tmp1:
LOAD * RESIDENT tmp ORDER BY "Year","Week" ASC;


DROP TABLE tmp;

NOCONCATENATE

tmp2:
LOAD *,
IF("Week" > PEEK("Week") AND [WoW Changes] < PEEK([WoW Changes]),1,0)
AS [Consecutive_Decline]
;
LOAD *,
(("Sum of sales" / PEEK("Sum of sales"))-1) AS [WoW Changes],
IF((("Sum of sales" / PEEK("Sum of sales"))-1) <0, 1 , 0) AS [Flag_Negative_Decline]
RESIDENT tmp1;

DROP TABLE tmp1;

View solution in original post

4 Replies
Taoufiq_Zarra

@nbuchholz1  can you also share the output expected from this sample ?

Regards,
Taoufiq ZARRA

"Please LIKE posts and "Accept as Solution" if the provided solution is helpful "

(you can mark up to 3 "solutions") 😉
nbuchholz1
Contributor II
Contributor II
Author

@Taoufiq_Zarra  The output would be something like this: 

WeekYearSum of ReadsChange from Week PriorIs Negative DeclineConsecutive Weeks Of Sales Decline
4120201000675%00
402020129-100%11
392020477823510%00
38202078324962%00
3720207378-73%13
36202027834-28%12
35202038783-87%11
34202028781221%00
3320202378780%11
32202023787899849%00
312020238-95%12
3020204637-99%11
29202083472812288%00
2820206738-71%12
27202023478-6%11
26202024878799%00
2520202767-95%11
2420205947771%00
23202034783373%00
222020734827%00
2120205789-0 


I'm pretty clear how I would address this if I just needed to build a table of data going forward.  However, the challenge is I need to run this analysis on my historical data. I believe I somehow need to iterate through the whole time series to come up with these calculations, but I'm not sure how to do that in Qlik. 

Taoufiq_Zarra

@nbuchholz1  Maye be this:

Data:
LOAD * INLINE [
    Week, Year, Sum of sales
    41, 2020, 1000
    40, 2020, 129
    39, 2020, 477823
    38, 2020, 78324
    37, 2020, 7378
    36, 2020, 27834
    35, 2020, 38783
    34, 2020, 287812
    33, 2020, 237878
    32, 2020, 237878
    31, 2020, 238
    30, 2020, 4637
    29, 2020, 834728
    28, 2020, 6738
    27, 2020, 23478
    26, 2020, 24878
    25, 2020, 2767
    24, 2020, 59477
    23, 2020, 34783
    22, 2020, 7348
    21, 2020, 5789
];

Tmp:
noconcatenate

load * resident Data order by Year,Week;

drop table Data;


Final:
noconcatenate

load *,if([Is Negative Decline]='1' and peek([Is Negative Decline])=[Is Negative Decline],peek([Consecutive Weeks Of Sales Decline])+1,[Is Negative Decline]) as [Consecutive Weeks Of Sales Decline];

load *,Num(if(Year=peek(Year),([Sum of sales]-peek([Sum of sales]))/peek([Sum of sales])),'# ##0%') as [Change from Week Prior]
,if(Num(if(Year=peek(Year),([Sum of sales]-peek([Sum of sales]))/peek([Sum of sales])),'# ##0%')<0,'1','0') as [Is Negative Decline]

  resident Tmp;

drop table Tmp;

 

output:

Capture.PNG

Regards,
Taoufiq ZARRA

"Please LIKE posts and "Accept as Solution" if the provided solution is helpful "

(you can mark up to 3 "solutions") 😉
Gabriel
Partner - Specialist III
Partner - Specialist III

@nbuchholz1 

Please see script below and hopefully it helps you.

tmp:
LOAD
"Week",
"Year",
"Sum of sales"
FROM [lib://CommunityQuestion]
(html, utf8, embedded labels, table is @1);

NOCONCATENATE
tmp1:
LOAD * RESIDENT tmp ORDER BY "Year","Week" ASC;


DROP TABLE tmp;

NOCONCATENATE

tmp2:
LOAD *,
IF("Week" > PEEK("Week") AND [WoW Changes] < PEEK([WoW Changes]),1,0)
AS [Consecutive_Decline]
;
LOAD *,
(("Sum of sales" / PEEK("Sum of sales"))-1) AS [WoW Changes],
IF((("Sum of sales" / PEEK("Sum of sales"))-1) <0, 1 , 0) AS [Flag_Negative_Decline]
RESIDENT tmp1;

DROP TABLE tmp1;