I have 2 fields where either can be zero. I need to determine the percentage differance between the two. How can I avoid a zero divide error when one of these fields is zero.
The problem is that either field might be zero. I am showing the % change is sales from one year to another. The result might be a plus/minus. If last years sales were 5 and this year was 0 then I would show -100%
So close. The following function works except when the YTD is zero and the PYR has a valus. When they both have a value the formula works regardless of which field is greater.
if {AR1_CustomerMaster.SalesYTD} = 0 then
0
else if {AR1_CustomerMaster.SalesPYR} > 0 and {AR1_CustomerMaster.SalesYTD} = 0 then
-100
else
({AR1_CustomerMaster.SalesYTD}-{AR1_CustomerMaster.SalesPYR}) % {AR1_CustomerMaster.SalesYTD}
if {table.lastyearamt} = 0 then 0 else
({table.thisyearamt}-{table.lastyearamt}) % {table.lastyearamt}
...then if {table.thisyearamt} = 0 the result would be 100%, so I'm not sure what your concern is--unless you are saying that {table.thisyearamt} might be null?
if (
isnull({table.lastyearamt}) or
{table.lastyearamt} = 0
) then 0 else
if isnull({table.thisyearamt}) then -100 else
({table.thisyearamt}-{table.lastyearamt}) % {table.lastyearamt}
if (
isnull({table.lastyearamt}) or
{table.lastyearamt} = 0
) then 0 else
if isnull({table.thisyearamt}) then -1 else
({table.thisyearamt}-{table.lastyearamt}) % {table.lastyearamt}
I thought you wanted the percentage based on last year in the denominator (which I think is the more usual comparison). Please post the formula you are using.
Basically I am looking to post the % change between the Current year and Prior year keeping in mind that either can be zero. Here is the formula:
if (
isnull({AR1_CustomerMaster.SalesPYR}) or
{AR1_CustomerMaster.SalesPYR} = 0
) then 0 else
if isnull({AR1_CustomerMaster.SalesYTD}) then -1 else
({AR1_CustomerMaster.SalesYTD}-{AR1_CustomerMaster.SalesPYR}) % {AR1_CustomerMaster.SalesPYR}
Thanks for all your effort. I appreciate the help.
YTD PYR +-%
0 0 0 Good
10 0 0 Should be %100
0 10 -100 Good
47 177 -73 Good
if {AR1_CustomerMaster.SalesYTD} = 0 and {AR1_CustomerMaster.SalesPYR} = 0 then
0
else if {AR1_CustomerMaster.SalesYTD} = 0 and {AR1_CustomerMaster.SalesPYR} > 0 then
-100
else if {AR1_CustomerMaster.SalesYTD} > 0 and {AR1_CustomerMaster.SalesPYR} = 0 then
100
else
({AR1_CustomerMaster.SalesYTD}-{AR1_CustomerMaster.SalesPYR}) % {AR1_CustomerMaster.SalesYTD}
...0% is the correct figure. Anything divided by 0 is 0. However, you could force the display of 100% by using:
if (
(
isnull({AR1_CustomerMaster.SalesPYR}) or
{AR1_CustomerMaster.SalesPYR} = 0
) and
(
isnull({AR1_CustomerMaster.SalesYTD}) or
{AR1_CustomerMaster.SalesYTD} = 0
)
) then 0 else
if (
isnull({AR1_CustomerMaster.SalesPYR}) or
{AR1_CustomerMaster.SalesPYR} = 0
) then 1 else
if isnull({AR1_CustomerMaster.SalesYTD}) then -1 else
({AR1_CustomerMaster.SalesYTD}-{AR1_CustomerMaster.SalesPYR}) % {AR1_CustomerMaster.SalesPYR}
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.