cancel
Showing results for 
Search instead for 
Did you mean: 

Javascript to calculate previous six months

Former Member
0 Kudos

I need help with the Javascript to calculate the previous six months based on the current month.  I tried using the While statement but that does not seem to be a valid statement in Design Studio.  The data I am reading does not always have data for every month so I cannot use an array of the 0CALMONTH values.  I want to do something like this (syntax is not correct just pseudo).

var month = APPLICATION.getInfo().dateNowInternalFormat.substring(4,2);

var year = APPLICATION.getInfo().dateNowInternalFormat.substring(0,4);

While (i < 6) {

   month = month - 1;

   if month > 1;

     month = 12;

     year = year - 1;

   endif.

i++;

}

Thanks.

Sandy

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

This is what I eventually got to work. There is probably a better way to do this but I am a newbie to Javascript.

var current_month = APPLICATION.getInfo().dateNowInternalFormat;

var sCurrYear = APPLICATION.getInfo().dateNowInternalFormat.substring(0,4);

var iCurrYear = Convert.stringToInt(sCurrYear);

var sCurrMon = APPLICATION.getInfo().dateNowInternalFormat.substring(4,6);

var iCurrMon = Convert.stringToInt(sCurrMon);

var iRadioMonths = Convert.stringToInt(RADIOBUTTONGROUP_1.getSelectedValue());

if(iRadioMonths == 6 || iRadioMonths == 12){

    var iMonths = iRadioMonths - 1;

       var iMon = iCurrMon + iMonths * -1;

       iCurrMon = iMon;

       if(iMon<=0){

              iCurrYear = iCurrYear - 1;

              iCurrMon = iMon + 12;     

             
       }else{

              if (iMon>12){

                     iCurrYear = iCurrYear +1;

                     iCurrMon = iMon - 12;

              }

       }

}

var iFrom = iCurrYear * 100;

iFrom = iFrom + iCurrMon;

var sFrom = Convert.floatToString(iFrom,"######");

DS_1.setFilter("0CALMONTH",{"low":sFrom,"high":current_month});

Former Member
0 Kudos

Hi Sandy Assum

You can refer to this blog to find trailing 6 months from the current month

TammyPowlas
Active Contributor
0 Kudos

Sandy - what is your data source?  Wouldn't it be easier and faster to do this type of calculation in the BEx query (and by faster, I mean faster for your dashboard consumer of the Design Studio application)

Former Member
0 Kudos

I want to have a radio button for them to select 6 or 12 months or quarters (JFM, AMJ, JAS, OND).  I also want to be able to use when using a Hana view.

TammyPowlas
Active Contributor
0 Kudos

So you are not using a BEx query?  Otherwise those could be variables to pass using BEx