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

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
tschullo
Creator III
Creator III

RangeSum by multiple dimensions

I am using QV12 and need to solve what seems like a simple problem:

MakeModelMonYrSales3MonSalesCnt
FordPintoDec 201903
FordPintoNov 201923
FordPintoOct 201911
FordEscortDec 201912
FordEscortNov 201911
FordEscortOct 201900

 

I need a way to calculate 3MonSaleCnt (running count of sales for past 3 months)
I can't rely on the number of months always being >= 3 and the method must respect a change in Make and/or Model.
I am open to a script OR expression solution (personally, I prefer a script solution for dashboard efficiency's sake)

 

Labels (1)
1 Solution

Accepted Solutions
Frank_Hartmann
Master II
Master II

Maybe try like this:

tmp:
Load *, (RowNo()) as Row;
Load * Inline [
Make,	Model,	MonYr_,	Sales	
Ford,	Pinto,	Dec 2019,	0
Ford,	Pinto,	Nov 2019,	2
Ford,	Pinto,	Oct 2019,	1
Ford,	Escort,	Dec 2019,	1
Ford,	Escort,	Nov 2019,	1	
Ford,	Escort,	Oct 2019,	0
];	
NoConcatenate

Load *,RangeSum(Sales,	If(Model = Peek(Model, -1), Peek(Sales,-1)),If(Model = Peek(Model, -2), peek(Sales,-2))) as [3Month] 
Resident tmp Order by Row desc; DROP Table tmp;

View solution in original post

2 Replies
Frank_Hartmann
Master II
Master II

Maybe try like this:

tmp:
Load *, (RowNo()) as Row;
Load * Inline [
Make,	Model,	MonYr_,	Sales	
Ford,	Pinto,	Dec 2019,	0
Ford,	Pinto,	Nov 2019,	2
Ford,	Pinto,	Oct 2019,	1
Ford,	Escort,	Dec 2019,	1
Ford,	Escort,	Nov 2019,	1	
Ford,	Escort,	Oct 2019,	0
];	
NoConcatenate

Load *,RangeSum(Sales,	If(Model = Peek(Model, -1), Peek(Sales,-1)),If(Model = Peek(Model, -2), peek(Sales,-2))) as [3Month] 
Resident tmp Order by Row desc; DROP Table tmp;
tschullo
Creator III
Creator III
Author

Thank you Frank!

Simple yet elegant.

In my data there is a chance that the second dimension exists for multiple "Make" values, so I adjusted:

Load *,
RangeSum(Sales, If(Model = Peek(Model, -1) And Make = Peek(Make, -1), Peek(Sales,-1)),
                                     If(Model = Peek(Model, -2) And Make = Peek(Make, -2), peek(Sales,-2))
) as [3Month]
Resident tmp Order by Row desc;