Skip to main content
Announcements
Have questions about Qlik Connect? Join us live on April 10th, at 11 AM ET: SIGN UP NOW
cancel
Showing results for 
Search instead for 
Did you mean: 
joeallen
Contributor III
Contributor III

Cumulative sum expression that works with cyclic group?

Hello

Say I have sum(Sales)

I want it as a cumulative sum over a time period, to start with Date

So say sum sales for 20191001 is 200, for 20191002 is 300, then it should show 200+300 as the value on 20191002 etc...

Then you can for example use the new for QV12 sortable AGGR function

Aggr(RangeSum(Above(sum(Sales), 0, RowNo())), YearMonth, (Date, (NUMERIC)))

This works for me.

HOWEVER

My time period is really not only Date, but it's a cyclic group with Date, YearWeek, YearMonth and Year

The above expression only works for Date, but not for the others

How do I make an expression with cumulative sum that works for this cyclic time group?

Labels (1)
1 Solution

Accepted Solutions
sunny_talwar

Correct me if I am wrong @tresesco , but won't we need Dollar sign expansion here?

Aggr(
  RangeSum(Above(Sum(Sales), 0, RowNo()))
, YearMonth, ($(='[' & GetCurrentField (group_name) & ']'), (NUMERIC)))

View solution in original post

15 Replies
tresesco
MVP
MVP

Try replacing your direct field in aggr sort argument by expression: GetCurrentField (group_name), like:

 

Aggr(RangeSum(Above(sum(Sales), 0, RowNo())), YearMonth, (GetCurrentField (group_name), (NUMERIC)))

 

 

sunny_talwar

Correct me if I am wrong @tresesco , but won't we need Dollar sign expansion here?

Aggr(
  RangeSum(Above(Sum(Sales), 0, RowNo()))
, YearMonth, ($(='[' & GetCurrentField (group_name) & ']'), (NUMERIC)))
tresesco
MVP
MVP

Sunny,

That is something I wanted to test and post. In fact I typed two possibilities as answer, one with $ and another without, then I thought that, since the getcurrentfield() would actually be evaluated with the chart (since it is dimension-selection sensitive) - the main expression and getcurrentfield() evaluation might work simultaneously. Again, not tested here.    

sunny_talwar

From my understanding of using this in the past, GetCurrentField() will just give the name of the field as a text... to actually make it read as a field, you will need $()... but let me know if you find otherwise while testing.

tresesco
MVP
MVP

If the field name doesn't contain space, that should probably not matter. So it's not about evaluating before the actual expression gets evaluated, rather adding '[' ']' to give a field name convention for those even that has spaces in field name.  

 

Edit: I roughly tested and it works (without space in field name obviously)

sunny_talwar

Spaces is taken care by '[' and ']', but to convert it to field is done by $(=)... I still feel it is needed.

tresesco
MVP
MVP

Sunny, so yes, yours is a better approach since that would work in all scenarios. 🙂

sunny_talwar

For some reason I don't see it working without $().. please check the attached file

 

joeallen
Contributor III
Contributor III
Author

Thanks for the idea Tresesco and for the correction Sunny.

It works fine for Date and YearWeek (I want the cumulative sum to reset per YearMonth for Date and YearWeek so I want this expression as is for those two)

However once the Cyclic group changes to YearMonth it doesn't work any more (it just sums like normal and not cumulative per YearMonth, I assume because of the YearMonth grouping in the expression that works so well for YearWeek and Date. And Year doesn't work at all.
I guess have to use some kind of If function here to get YearMonth and Year to work?