Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
grajmca_sgp123
Creator
Creator

Cumulative sum for last 12 months

Hi I am trying to do the cumulative sum for last 12 months ,I have used below expression for cumulative sum but my yearmonth did nor sorted properly.

How can we sort it

YearMonth sum( aggr( rangesum( above( sum([va]),0,12)),YearMonth))
2014Oct169591262.31912
2014Nov195147123.40447
2014Dec183528473.22945
2015Jan193411687.76049
2015Feb10023235.79498
2015Mar-5481521.8884398
2015Apr12031879.59047
2015May22385354.41466
2015Jun7010994.9133703
2015Aug33994469.05486
2015Jul29853128.87857
2014Sep190288699.13375
2014Jun183123809.9909
2014Jul199262258.59527
2014Aug181742724.37857

9 Replies
jonathandienst
Partner - Champion III
Partner - Champion III

Change YearMonth into a proper date field during load, and then sort it numerically...

     LOAD

          YearMonth As YearMonthText,

          Date(MonthStart(Date#(YearMonth, 'YYYYMMM')), 'YYYYMM') As YearMonth,

Now use YearMonth as the dimension and sort numerically.

Logic will get you from a to b. Imagination will take you everywhere. - A Einstein
grajmca_sgp123
Creator
Creator
Author

how about if we use dim as Months only..Oct,Nov,Dec,Jan,Feb.Mar

and above months are defining from  Date field (format of date ::MM/DD/YYYY)

so how can we use Months as dim for cumulative sum for last 12 months

ankitbisht01
Creator
Creator

For months you can use this  expression:

month(date(date#(YearMonth,'YYYYMMM'),'YYYYMMM')) as months

sasiparupudi1
Master III
Master III

You need to have year value in order to get the cumulative sum for  the last 12 months. I would go with Jonathan's suggestion.

hth

Sasi

jonathandienst
Partner - Champion III
Partner - Champion III

You can add year and month dimensions easily enough:

     LOAD

          YearMonth As YearMonthText,

          Date(MonthStart(Date#(YearMonth, 'YYYYMMM')), 'YYYYMM') As YearMonth,

          Month(Date#(YearMonth, 'YYYYMMM')) As Month,

          Year(Date#(YearMonth, 'YYYYMMM')) As Year,

          ...


Just keep in mind that Month and Year are good for YTD calculations and selections but are not appropriate for a rolling 12 month analysis.


    

Logic will get you from a to b. Imagination will take you everywhere. - A Einstein
jonathandienst
Partner - Champion III
Partner - Champion III

You could also add a 12 month flag -

     LOAD

          YearMonth As YearMonthText,

          Date(MonthStart(Date#(YearMonth, 'YYYYMMM')), 'YYYYMM') As YearMonth,

          Month(Date#(YearMonth, 'YYYYMMM')) As Month,

          Year(Date#(YearMonth, 'YYYYMMM')) As Year,

          If(Date#(YearMonth, 'YYYYMMM') > AddYears(Today(), -1), 1, 0) As Flag12Months,

          ...


Then use the flag in set expressions like:


     Sum({<Flag12Months = {1}>} Amount)


with a dimension of YearMonth






Logic will get you from a to b. Imagination will take you everywhere. - A Einstein
jonathandienst
Partner - Champion III
Partner - Champion III

See also

Calculating rolling n-period totals, averages or other aggregations

Logic will get you from a to b. Imagination will take you everywhere. - A Einstein
kavita25
Partner - Specialist
Partner - Specialist

Click on Sort Tab...

And check Expression and use YearMonth field in ascending order.

Hope it helps you!!

Regards,

Kavita

Anonymous
Not applicable

Hi Rajesh,

Try below expression in Sort Expression and select ascending in dropdown

=Left(YearMonth,4) & '' &match(Right(YearMonth,3),'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')

Regards

Neetha