15 Replies Latest reply: Oct 23, 2013 1:59 AM by Pavan Kumar RSS

For Next Loop

Shumail Hussain

The below expression is not working, please help?

=(

if(

Month(LoadDate_Active)>1

,

Let c=0

for a = Month(LoadDate_Active) to 1 step -1

Let c = $(c) + sum({$<AIF = {P}, Bucket_MOM = {0,1,2,3,4,5,6}>} bal)

next

$(c)

,

sum({$<AIF = {P}, Bucket_MOM = {0,1,2,3,4,5,6}>} bal)

)

)

/(100*1000000)

 

Shumail

  • For Next Loop
    NagaianK

    Where are you using this expression?

     

    Set expressions do not work in load script and for ...next loop is not available in chart expressions.

    • For Next Loop
      Shumail Hussain

      Is there any alternate of "For..Next" loop in chart expression?

       

       

      Also

       

      Is there ne matrix for all these details I mean something like

       

                                    Load ScriptExpressionMacro (vbscript)
      For Next LoopYesNoYes
      Do While LoopYesNoYes
      Set NoYesYes

       

      Shumail

      • For Next Loop
        Kaushik Solanki

        Hi,

         

           As krishnamoorthy said, the looping can not be done in expression.

         

           There is no matrix available like you shown.

         

           Why you want to use for loop in expression.

         

           Provide us your problem definition, may be there are some other solution available.

         

        Regards,

        Kaushik Solanki

        • For Next Loop
          Shumail Hussain

          Actually I am looking for the sum of customer balances by year like if i select May-2011 then expression can sum all the values in 2011 before and in may-2011 excluding june-2011 onward. similarly with every month.

           

          I think I have to apply many nested If conditions like this

           

          =(

          if(Month(LoadDate_Active)=1

          ,sum({$<AIF = {P}, {$<MonthName_ID = {'$(=MonthName(LoadDate))'}, Bucket_MOM = {0,1,2,3,4,5,6}>} bal)

          ,if(Month(LoadDate_Active)=2,

          sum({$<AIF = {P}, {$<MonthName_ID = {'$(=MonthName(LoadDate))'}, Bucket_MOM = {0,1,2,3,4,5,6}>} bal)

          +

          sum({$<AIF = {P}, {$<MonthName_ID = {'$(=MonthName(LoadDate,-1))'}, Bucket_MOM = {0,1,2,3,4,5,6}>} bal)

          ,if(Month(LoadDate_Active)=3,

          sum({$<AIF = {P}, {$<MonthName_ID = {'$(=MonthName(LoadDate))'}, Bucket_MOM = {0,1,2,3,4,5,6}>} bal)

          +

          sum({$<AIF = {P}, {$<MonthName_ID = {'$(=MonthName(LoadDate,-1))'}, Bucket_MOM = {0,1,2,3,4,5,6}>} bal)

          +

          sum({$<AIF = {P}, {$<MonthName_ID = {'$(=MonthName(LoadDate,-2))'}, Bucket_MOM = {0,1,2,3,4,5,6}>} bal)

           

           

          ..

          ...

          .... till the twelve months

           

           

          ,if(Month(LoadDate_Active)=12,

          sum({$<AIF = {P}, {$<MonthName_ID = {'$(=MonthName(LoadDate))'}, Bucket_MOM = {0,1,2,3,4,5,6}>} bal)

          +

          sum({$<AIF = {P}, {$<MonthName_ID = {'$(=MonthName(LoadDate,-1))'}, Bucket_MOM = {0,1,2,3,4,5,6}>} bal)

          +

          ..

          ...

          ....

          sum({$<AIF = {P}, {$<MonthName_ID = {'$(=MonthName(LoadDate,-11))'}, Bucket_MOM = {0,1,2,3,4,5,6}>} bal)

          )

          )

          )

          )

          /(100*1000000) 

           

          Shumail

          • For Next Loop
            Kaushik Solanki

            Hi,

             

               Let me say what i understood from this is that,

             

               when you select May-2011, you want data from Jan-2011 till May-2011.

             

               Am i right.

             

            Regards,

            Kaushik Solanki

            • Re: For Next Loop
              Shumail Hussain

              Yes exactly you are right kaushik!

               

              Thanks for the reply Toni, I am attaching one sample of my project. I am having problem on disabled field COF Final... Please see it on expression tab and if you have any solution then let me know... Thanks in Advance.

               

              Shumail

              • Re: For Next Loop
                shaun somai

                I have a solution but i cant seem to upload to the forum.

                 

                send me  your email and i wil send the solution.

                 

                Try somehting like this in your script

                I use it  together with a calendar

                 

                I used these with set analysis to calculate some expressions in my chart(i.e. YTD,MTD,WTD...etc)

                 

                 

                IF(M>3,Y+1,Y)as FinYear,

                NUM(IF(M>3,Month(AddMonths(D,-3)),Month(AddMonths(D,9))),'00')as FinMonth,

                YearName(D,0,4) as FinPeriod,

                'Q' & Ceil(Month(addmonths(D,-3))/3) as Quarter,

                NUM(IF(M>3,Y+1,Y)& Ceil(Month(addmonths(D,-3))/3),'00') as FinYearQuarter,

                IF(M>3,Y+1,Y) & NUM(IF(M>3,Month(AddMonths(D,-3)),Month(AddMonths(D,9))),'00') as FinYearMonth,

                • Re: For Next Loop
                  Steve Dark

                  I may be missing something but is this not a simple pivot with Months as the dimension along the top and then Set Analysis in the expression to limit to the max year and months prior to the max selected?

                   

                  Something like:

                   

                  sum({<Year={$(vMaxYear)},Date={'<=$(vMaxDate)'},Month=>}Value)

                   

                  I've also attached an example that may help.

                   

                  By extending the example to also be aware of the Prior year you could put in a YTD prior year comparison with variances.

                   

                  - Steve

                • Re: For Next Loop
                  Shumail Hussain

                  Steve,

                   

                  Thanks for your helpful response and the attached example. it resolves one part of my problem

                   

                  but the thing is i wanted to do the average of these values by month

                   

                  like if the selected month is july 2011 in your example

                  then average should be the

                   

                  sum of values of each month divided by 7 i.e. 13,782.86.

                   

                  ValueJanFebMarAprMayJunJulTotal Average
                  Total 18,238.00 15,172.00 17,612.00 14,284.00 12,064.00 13,854.00    5,256.00 96,480.00   13,782.86

                   

                  not

                   

                  the sum of each row / number of rows

                   

                   

                  Shumail

                   

                  Shaun my email is shumail.hussain@fgb.ae

                  • Re: For Next Loop
                    Shumail Hussain

                    I think the possible way is

                     

                     

                    sum({<Year={$(vMaxYear)},Date={'<=$(vMaxDate)'},Month=>}Value)/vMaxMonth 
                    


                    divide the expression with number of months

                    • Re: For Next Loop
                      Steve Dark

                      That looks like it should work, but will divide every month by the number of months - so your average will be correct but not the monthly columns.  The other thing that would not be as your example above is having both a Total and an Average column.  This is not easy (possible?) in a pivot.

                       

                      What you could do is change it to a straight table and have a separate expression for each month, and then separate expressions for the Total and Average fields.  You could then change the background and text settings for those expressions to make it clear they are totals.  Finally you would need to conditionally show or hide the months (unless you wanted to show the zero future months).

                       

                      I've added another tab to my example showing the straight table approach.

                       

                      - Steve

          • For Next Loop
            Toni Kautto

            Generally it is really not recommende to use to many nested IF statements, since this can have serious impact on the application performance. You are much better of if you can find a solution using set analysis and/or Aggr().

             

            An other option can be to do some additonal calculations in the load script, so that parts are statically precalculated and only needs to be sumed in the expression. This would be of some performance gain in case you run larger data volumes or a slower machine.

             

            It would be easier to advise on a good approach if you attach a sample of you solution, so that the data model can be evaluated.