Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deviding by a value that could be 0 to get the percent 1

Status
Not open for further replies.

mohebk

MIS
Aug 23, 2005
139
0
0
US
Hi,
Using MS Access 2003.

I have a formula in a field that looks like this: =([Curr_Vis_Out]-[Prev_Vis_Out])/[Prev_Vis_Out] to get the percent of a change between two periods. I am getting a 0 if either fields is a zero value. How can I program this to accommodate when the zero is in the denominator or the numerator. I think it should work is way: if the [Curr_Vis_Out] = 0 or null the result should be (-100%) but if the [Prev_Vis_Out] = 0 or null the result should be 100%.

Thanks

Mo
 
I'm not sure what you want if they are both Null and/or zero.
Code:
=IIF(Nz([Prev_Vis_Out],0) = 0, 1, (Nz([Curr_Vis_Out],0)-[Prev_Vis_Out])/[Prev_Vis_Out])

Duane
Hook'D on Access
MS Access MVP
 
This is exactly what I was looking for. The only thing left is actually an answer to your question, if both are null or zero I would like the result be 0%. Where can insert that.

Thanks

Mo
 
I hate creating nested IIf()s since you end up store business rules in expressions. I would create a small user-defined function that would accept the two values and return a result:
Code:
Public Function GetPctChange(varPrev As Variant, varCur As Variant) As Double
If Nz(varPrev, 0) = 0 Then
   If Nz(varCur, 0) = 0 Then
      GetPctChange = 0
    Else
      GetPctChange = 1
   End If
 Else
   GetPctChange = (Nz(varCur, 0) - varPrev) / varPrev
End If

End Function
You could then use this in a query or contol source:
Code:
=GetPctChange([Prev_Vis_Out], [Curr_Vis_Out])

Duane
Hook'D on Access
MS Access MVP
 
The second post was the perfect approach. Thanks a lot for your help.

Mo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top