Skip to main content
Announcements
See why Qlik is a Leader in the 2024 Gartner® Magic Quadrant™ for Analytics & BI Platforms. Download Now
elif-tutuk
Former Employee
Former Employee

We all use QlikCommunity when we need an answer for a QlikView question. We post a question and get an answer, usually in minutes, leveraging the expertise of 91,000+ QlikView users! QlikCommunity is the most-visited and active user community in the BI industry.

I also use QlikCommunity. And sometimes not to get an answer for a question but to think about new ways of doing things by reading the answers on the existing posts. So I have been curious about the answers on calculating the accumulative sums in QlikView and did a search on QlikCommunity. There are many answers provided as expected. Here is one way of achieving it by using rangesum() and aggr() functions.

It is a very common requirement to display the accumulative sum of a chart metric. The easiest way of doing this is to use the accumulate setting under the expressions tab of chart property.

Chart accumulation (2).jpg

Figure 1. Accumulate option on chart property

With this setting, the values of the selected metric will be accumulated. But sometimes, the requirement is to have the first data point accumulated as well. For example, to show the accumulated sales for 12 months where the first month displayed should be the sum of the previous 12 months and the following months should accumulate on top of this value by adding one month at a time. One way of achieving this is to use the rangesum() and the aggr() functions together.

Let us first look at Rangesum(). It is a range function that returns the sum of a range of 1 to N arguments Together with the above() function, you can calculate the sum of the last 12 months;

rangesum( above( sum(Sales),0,12) )

This expression will return the sum of sales evaluated for each month as they appear on the 12 rows above the current row.

The next step is to combine RangeSum() with  the aggr() function to aggregate the sum of sales for the 12 months at the month level.

sum( aggr( rangesum( above( sum(Sales),0,12) ),Month))

Using the Aggr() function, it is possible to calculate the accumulated sum for any other dimensions in addition to  the time dimension. For example, the 12 months accumulated sum of sales by store can be calculated as;

sum( aggr( rangesum( above( sum(Sales),0,12) ),Month, Store))

Optionally you could also clear any selections on the month field as the calculation should reflect the accumulated sales for the previous 12 months, regardless of selections. This is achieved by using the set analysis “{$<Month>}”.

sum( aggr( rangesum( above( sum( {$<Month>}  Sales),0,12) ),Month))

One important thing to notice with this solution is the sort order inside the aggr() function. If the sort order is not correct, the aggregated sum of the 12 months would not be correct. QlikView always sorts the groupings (in this case the groups defined by the second parameter of the aggr() function) according to the load order. If the data is sorted by date during the load, QlikView will use this order and the aggr() function will show the correct totals for the desired time frame.

In summary, the accumulation option on the expression tab is the simplest way of achieving accumulations, but the use of rangesum() and aggr() functions are alternative ways of accumulating the data in more complex situations.

33 Comments
mhappiee
Partner - Contributor III
Partner - Contributor III

Hi

How to get YTD in Straight table as an additional measure  along with Monthly sales measure

To Detail:

I have Data like

Yearmonth, Volume, Client name, product type

attached has the sample data and I want to calculate YTD and the pct difference along with the last 13 months volume

was able to use set analysis to get the sum for YTD but when I add that in pivot that is getting split by Year month  like below

Cname ,                                          year month                                            YTD

                             Jan 2017|Feb 2017|.....|Jan 2018|Feb 2018    Jan 2017|Feb 2017|.....|Jan 2018|Feb 2018

Arun                         10 |20.....|30|40                                                       10 |20.....|30|40

Asha                         30 |40.....|20|10                                                       30 |40.....|20|10


Can some body help me out to get the YTD and pct difference as Given in the excel


Thanks for all your help ahead


Regards,

mhappiee

0 Likes
627 Views
mhappiee
Partner - Contributor III
Partner - Contributor III

please find the post link which has the excel attachment

YTD calculation

0 Likes
627 Views
craigbrett
Partner - Contributor II
Partner - Contributor II

@anderseriksson  This solution is brilliant! Nothing else has worked for me but the below formula

Count({1<Month={">=$(=Min(Month))<=$(=Max(Month))"}>} DISTINCT 1) *
RangeSum(Above(Sum({1}0) + Sum({$<Month>} Sales), 0, 3))

No TOTAL required so it follows the dimensions of whatever you put it in and you can easily use the set analysis to discard selections.

 

 

0 Likes
341 Views