7 Replies Latest reply on Oct 28, 2015 6:25 AM by jbrown

    Nested If statements?

    GJBDJC

      Hello all,

       

      I'm trying to get a nested if statement to work:

      If (Order Start Date > Get(CurrentDate); "0"; If (End Date - (Get(CurrentDate)) ≥ 0; "1"; "0")) Is the product running today?

      If (Order Start Date > Get(CurrentDate); "0"; If (End Date - (Get ( CurrentDate) +1) ≥ 0; "1"; "0")) is the product running tomorrow?

       

      Basically, return a "1" if the product is running today, based on a start date and an end date.

      The (Order Start Date > Get(CurrentDate); "0" returns a zero for products with future start dates and

      the If (End Date - (Get(CurrentDate)) ≥ 0; "1"; "0")) returns a 1 if the end date minus  today returns a non-negative number.

       

      Seems simple, won't work?????

        • 1. Re: Nested If statements?
          bigtom

          I would consider a Case instead of nested If. A little easier to troubleshoot and I hear they are faster as well.

          • 2. Re: Nested If statements?
            DanielShanahan

            I agree with bigtom - a Case statement is easier to read than a nested If.

             

            However, in your case, I think you can still use an If statement to determine if the project is taking place today:

             

            If (

              Get ( CurrentDate ) >= TABLE::dateStart and

              Get ( CurrentDate ) <= TABLE::dateEnd ;

              1 ;

              0

            )

            • 3. Re: Nested If statements?
              jbrown

              Morning.

              That's weird. I was able to get your exact statement to work. Can you give us an example of when it fails? Every time?

               

              Here's my statement to solve the question "Is this product running today?":

               

              Let ([

              _Date = Get(CurrentDate);

              _Start = Order Start Date;

              _End = End Date

              ];

               

              If (

                   _Date ≥ _Start  and _Date ≤ _End ;

                        //If today's date is greater that the start date and less than the end date

                        1 ;

                        // True: return a 1

                        0

                        // False: return a 0

                   )

              )

              • 4. Re: Nested If statements?
                GJBDJC

                Thank you all for the responses.  I will work with the case statement, if for nothing else, the new experience.

                 

                However, I found my original problem.

                When using StartDate > CurrentDate, I did not change the equation for tomorrow or the next day,

                so the StartDate was always greater than today (at least for today) in each of my equations.

                So I needed to change tomorrow to StartDate > CurrentDate+1 and so on..... +2, +3....

                • 5. Re: Nested If statements?
                  jbrown

                  Case statements are meant for testing a whole bunch of possibilities.

                   

                  Let (

                  _Today = DayName (Get (CurrentDate));

                   

                  Case (

                  _Today = "Monday"; "ugh, another week is starting!";

                  _Today = "Tuesday" ; "It's only Tuesday?";

                  _Today = "Wednesday" ; "Hump day!"

                  _Today = "Thursday" ; "Almost Friday, almost Friday";

                  _Today = "Friday"; "YES! The weekend!"; ""

                  )

                  )

                   

                  You have one possibility: Is today's date greater than the start date AND is it less than the end date. You case statement would look exactly like the if statement.

                   

                  Please try case statements. What's cool about them is that they stop at the first true statement, so you want to put the most likely outcome (if applicable) at the beginning so they stop calculating faster. But I am sure the difference is just a fraction of a millisecond.

                  • 6. Re: Nested If statements?
                    keywords

                    I would strongly suggest you set this up as a Let() because:

                     

                    1.     It is much easier to set out clearly the logic of your argument

                    2.     It is more efficient because you can remove multiple DB calls—your original If() calc calls StartDate and EndDate twice each, and Get ( Current Date ) four times, so set them up as variables, thus calling each once only, then reference the variables in a Case() in the result portion of the Let() calc.

                    • 7. Re: Nested If statements?
                      jbrown

                      Thanks keywords, I forgot to explain why I set it up in Let statements.