Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
YoussefBelloum
Champion
Champion

Cumulative sum fixed in time

Hi everyone,

I'm struggling on keeping my cumulating sums (column stock) fixed on the time axis.

here is a sample of my data set:

AnnéeTrimestreCode grande brancheNombre contratsStock
2012T1B22
2012T1C1214
2012T1D519
2012T1E2039
2012T1F143182
2012T1H1183
2012T1I3186
2012T1 0186
2012T2B1187
2012T2C5192
2012T2D3195
2012T2E19214
2012T2F1215
2012T2H2217
2012T2I1218
2012T2 0218
2012T3C8226
2012T3D2228
2012T3E17245
2012T3F77322
2012T3H2324
2012T3 0324
2012T4C1325
2012T4D4329
2012T4E18347
2012T4F30377
2012T4H1378
2012T4K2380
2012T4 0380
2013T1A121501
2013T1B2503
2013T1C6509
2013T1D8517
2013T1E19536
2013T1F205741
2013T1H2743
2013T1K1744
2013T1 0744
2013T2B1745
2013T2C4749
2013T2D6755
2013T2E22777
2013T2F11788
2013T2H5793
2013T2 0793
2013T3C2795
2013T3D3798
2013T3E17815
2013T3H1816
2013T3 0816
2013T4C4820
2013T4D4824
2013T4E33857
2013T4H2859
2013T4K2861
2013T4 0861
2014T1B1862
2014T1C25887
2014T1D11898
2014T1E31929
2014T1F2931
2014T1H16947
2014T1I1948
2014T1K5953
2014T1 0953
2014T2B1954
2014T2C5959
2014T2D4963
2014T2E32995
2014T2H3998
2014T2K21000
2014T2 01000
2014T3A11001
2014T3B21003
2014T3C41007
2014T3D21009
2014T3E201029
2014T3H31032
2014T3I21034
2014T3K11035
2014T3 01035
2014T4C31038
2014T4E251063
2014T4H21065
2014T4 01065
2015T1A11066
2015T1C91075
2015T1D201095
2015T1E131108
2015T1F31111
2015T1H41115
2015T1K11116
2015T1 01116
2015T2C71123
2015T2D31126
2015T2E371163
2015T2G11164
2015T2H11165
2015T2K11166
2015T2 01166
2015T3B11167
2015T3C81175
2015T3D71182
2015T3E181200
2015T3H21202
2015T3K11203
2015T3 01203
2015T4C41207
2015T4D61213
2015T4E221235
2015T4H21237
2015T4 01237
2016T1A11238
2016T1B51243
2016T1C71250
2016T1D121262
2016T1E181280
2016T1F11281
2016T1H61287
2016T1I31290
2016T1K31293
2016T1 01293
2016T2C51298
2016T2D21300
2016T2E311331
2016T2H11332
2016T2 01332
2016T3B11333
2016T3C41337
2016T3D21339
2016T3E251364
2016T3H31367
2016T3K11368
2016T3 01368
2016T4C11369
2016T4D71376
2016T4E321408
2016T4H31411
2016T4K21413
2016T4 01413
2017T1A11414
2017T1B71421
2017T1C281449
2017T1D211470
2017T1E321502
2017T1F11503
2017T1H151518
2017T1I41522
2017T1K41526
2017T1 01526
2017T2C61532
2017T2D71539
2017T2E321571
2017T2H11572
2017T2K11573
2017T2 01573
2017T3C41577
2017T3D21579
2017T3E241603
2017T3G11604
2017T3H11605
2017T3 01605
2017T4C71612
2017T4D61618
2017T4E211639
2017T4H51644
2017T4 01644
2018T1A11645
2018T1B311676
2018T1C151691
2018T1D181709
2018T1E281737
2018T1H81745
2018T1I11746
2018T1K51751
2018T1 01751
2018T2B21753
2018T2C81761
2018T2D71768
2018T2E331801
2018T2H51806
2018T2 01806
2018T3B21808
2018T3C91817
2018T3D81825
2018T3E171842
2018T3F11843
2018T3H51848
2018T3I11849
2018T3 01849
2018T4B11850
2018T4C21852
2018T4D31855
2018T4E291884
2018T4H11885
2018T4 01885
2019T1A11886
2019T1B61892
2019T1C311923
2019T1D311954
2019T1E1022056
2019T1F22058
2019T1H142072
2019T1I12073
2019T1K92082
2019T1 02082
2019T2B12083
2019T2C152098
2019T2D102108
2019T2E1242232
2019T2H42236
2019T2I12237
2019T2K22239
2019T2 02239
2019T3B22241
2019T3C142255
2019T3D52260
2019T3E1022362
2019T3H42366
2019T3 02366
2019T4B12367
2019T4C152382
2019T4D112393
2019T4E912484
2019T4H12485
2019T4 02485
2020T1B32488
2020T1C182506
2020T1D172523
2020T1E1162639
2020T1H72646
2020T1I322678
2020T1K12679
2020T1 02679
2020T2B12680
2020T2C62686
2020T2D72693
2020T2E292722
2020T2H22724
2020T2I22726

 

I've calculated the cumulating sum using RangeSum and peek on the script and using the expression below on the front end:

=RangeSum(Above(total sum(NOMBRE_CONTRAT),0,RowNo(TOTAL)))

It works fine until I make selections on the time axis (Year or quarter for example), using fields Année and Trimestre.

What are the modifications to be made on the expression to make the cumulations fixed, to get everytime the same values on the column Stock.

Example, when I choose year 2020, i need this:

2020T1B32488
2020T1C182506
2020T1D172523
2020T1E1162639
2020T1H72646
2020T1I322678
2020T1K12679
2020T1 02679
2020T2B12680
2020T2C62686
2020T2D72693
2020T2E292722
2020T2H22724
2020T2I22726
2020T2 02726
2020T4 02726

 

Thank you !

Youssef

1 Solution

Accepted Solutions
Kushal_Chawda

@YoussefBelloum  Yes. It is possible via script as well. But you can change your expression below and try because frond end expressions will have more flexibility if you want to select any other dimensions apart from what you have in table

=sum(aggr(RangeSum(Above(total sum({<Année,Trimestre,[Code grande branche]>}NOMBRE_CONTRAT),0,RowNo(TOTAL))), (Année,NUMERIC), (Trimestre,TEXT),[Code grande branche]))*avg(1)

View solution in original post

11 Replies
Kushal_Chawda

@YoussefBelloum  modify your expression little bit as below. Assuming you have two dimensions as highlighted below in table. If you have more dimension then add as well in set & aggr as highlighted

=sum(aggr(RangeSum(Above(total sum({<Année,Trimestre>}NOMBRE_CONTRAT),0,RowNo(TOTAL))),Année,Trimestre))*avg(1)

 

YoussefBelloum
Champion
Champion
Author

Hi @Kushal_Chawda 

the extra aggr seems to fix the values when I select a year or a quarter BUT the values are not correct anymore..

here is what i get with it:

Année Trimestre Nombre contrats Stock
2012 T1 186 2139
2012 T2 32 1897
2012 T3 106 2245
2012 T4 56 1953
2013 T1 364 2703
2013 T2 49 2294
2013 T3 23 2726
2013 T4 45 2339
2014 T1 92 1830
2014 T2 47 1708
2014 T3 35 1865
2014 T4 30 1738
2015 T1 51 1624
2015 T2 50 1539
2015 T3 37 1661
2015 T4 34 1573
2016 T1 56 381
2016 T2 39 280
2016 T3 36 417
2016 T4 45 325
2017 T1 113 1216
2017 T2 47 1064
2017 T3 32 1248
2017 T4 39 1103
2018 T1 107 1446
2018 T2 55 1303
2018 T3 43 1489
2018 T4 36 1339
2019 T1 197 890
2019 T2 157 574
2019 T3 127 1017
2019 T4 119 693
2020 T1 194 241
2020 T2 47 47
2020 T4 0 47

 

Kushal_Chawda

@YoussefBelloum may be because aggr function broke the sorting of your data. try below. I am assuming the format of Année is number and Trimestre is text. Change the parameter as per your data values. Look at the parameters here

https://help.qlik.com/en-US/sense/September2020/Subsystems/Hub/Content/Sense_Hub/ChartFunctions/aggr...

=sum(aggr(RangeSum(Above(total sum({<Année,Trimestre>}NOMBRE_CONTRAT),0,RowNo(TOTAL))),

(Année(NUMERIC,ASCENDING)), (Trimestre(TEXT,ASCENDING))))*avg(1)

sunny_talwar

@Kushal_Chawda I don't think Avg(1) is needed because Sum() or Only() after Aggr() should take care of it

 

Aggr(
  RangeSum(Above(TOTAL Sum({<Année, Trimestre>} NOMBRE_CONTRAT), 0, RowNo(TOTAL)))
, (Année, (NUMERIC)), (Trimestre, (TEXT)))

 

Kushal_Chawda

@sunny_talwar  yes. not needed. In some scenarios it requires so keep it. Doesn't have  any harm to keep it actually for safer side.

YoussefBelloum
Champion
Champion
Author

@Kushal_Chawda thanks for your reply, but the syntax is incorrect..

this one works fine (It was missing the 3rd dimension):

=RangeSum(Above(total sum({<Année,Trimestre,[Code grande branche]>}NOMBRE_CONTRAT),0,RowNo(TOTAL)))*avg(1)

one last question please. I find out that my rangesum on the script acts like a simple sum, those numbers I want to get them from the script to use a simple sum() on the front end. is it possible ?

YoussefBelloum
Champion
Champion
Author

Hi @sunny_talwar  thanks for your reply, but I get nulls because the syntax is incorrect

syntax.png

Kushal_Chawda

@YoussefBelloum  Yes. It is possible via script as well. But you can change your expression below and try because frond end expressions will have more flexibility if you want to select any other dimensions apart from what you have in table

=sum(aggr(RangeSum(Above(total sum({<Année,Trimestre,[Code grande branche]>}NOMBRE_CONTRAT),0,RowNo(TOTAL))), (Année,NUMERIC), (Trimestre,TEXT),[Code grande branche]))*avg(1)

YoussefBelloum
Champion
Champion
Author

@Kushal_Chawda the last one is working 🙂 But on the last part: (Année(NUMERIC)), (Trimestre(TEXT)) the syntax seems to be incorrrect like I said to @sunny_talwar 

I changed it to : (Année,NUMERIC),(Trimestre,TEXT)

Is it a syntax that has changed with the versions of qlikview or what  ?