Business Rule - use of And to remove IF statement
Hello,
We may have come across
the forecasting requirement wherein we need to perform calculation from Current
Month to Dec for Current Year and from Jan to Dec for Next Years.
Using the below pseudo
code, we can achieve this requirement.
Fix ({Year}, Jan:Dec)
If @ismbr(¤tyear)
If @ismbr(¤tmonth :Dec )
Business logic
Endif
End if
Else
Business logic
Endif
If @ismbr(¤tyear)
If @ismbr(¤tmonth :Dec )
Business logic
Endif
End if
Else
Business logic
Endif
If we wish to use
DATACOPY, we need to eliminate IF statement because DATACOPY is not supported
inside IF statement. That means we need to manage this in FIX statement and
distinguish between current year and next year.
So, let's start
converting if statement to fix
Fix
(¤tyear,¤tmonth:Dec)
Endfix
Fix (&nextyear , Jan:Dec)
Endfix
Endfix
Fix (&nextyear , Jan:Dec)
Endfix
The problem here is the
code will run for all (both) the years instead of the user selected year. Now to
restrict the code to run only for the user selected year we can make use of AND
in our Fix statement.
Fix({Year} and
¤t year)
Fix (¤t month : dec)
Endfix
Endfix
Fix (¤t month : dec)
Endfix
Endfix
Fix({Year} and @list (@member(@nextsibling(&
currentyear)) : @member(@shiftsibling( & current year, 3))
Fix(Jan:dec)
Endfix
Endfix
Fix(Jan:dec)
Endfix
Endfix
Let us analyze the script
now. Imagine user is running the code for FY17 and the ¤t year is
also set to FY17. The code will run for all the years present in the year
dimension because the second fix evaluate empty set and run for entire year
dimension. Now we will add SET EMPTYMEMBRSETS ON; to avoid the calculation at
entire year dimension.
SET EMPTYMEMBRSETS ON;
Fix({Year} and
¤tyear)
Fix (¤tmonth: dec)
Endfix
Endfix
Fix (¤tmonth: dec)
Endfix
Endfix
Fix({Year} and
@list(@member(@nextsibling(& currentyear)) : @member(@shiftsibling( &
currentyear, 3))
Fix(Jan:dec)
Endfix
Endfix
Fix(Jan:dec)
Endfix
Endfix
If statements are completely
removed. Now we can use data copy in the script.
Note: - There can be many
cases where it's impossible to avoid if statement. This is just one of the
example where if statement can be avoided.
Comments
Post a Comment