# Access query syntax - division by zero in aggregate function

on
Dear experts -
hopefully an easy one.
I am calculate an average using the formula below - which works well, however i want to avoid division by zero, which is giving me a #Num! error for certain results.

Any suggestions?
Thanks!

``````TR_Reg: Sum(IIf([e].[billunit]=1 And [e].[volumeunit]=1 And [accounts].[regulatory]=True And [e].[action_ID]=38,[e].[billrate],0))/Sum(IIf([e].[billunit]=1 And [e].[volumeunit]=1 And [accounts].[regulatory]=True And [e].[action_ID]=38,1,0))
``````
Comment
Watch Question

Do more with

EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016
Commented:
Hi,

Maybe

``````TR_Reg: Sum(IIf([e].[billunit]=1 And [e].[volumeunit]=1 And [accounts].[regulatory]=True And [e].[action_ID]=38,[e].[billrate],0))/IIF(Sum(IIf([e].[billunit]=1 And [e].[volumeunit]=1 And [accounts].[regulatory]=True And [e].[action_ID]=38,1,0))=0,null,Sum(IIf([e].[billunit]=1 And [e].[volumeunit]=1 And [accounts].[regulatory]=True And [e].[action_ID]=38,1,0)))
``````
Regards
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
This is one method, to first check if the sum is zero:

TR_Reg:
IIf(Sum(IIf([e].[billunit]=1 And [e].[volumeunit]=1 And [accounts].[regulatory]=True And [e].[action_ID]=38,1,0))=0, 0,
Sum(IIf([e].[billunit]=1 And [e].[volumeunit]=1 And [accounts].[regulatory]=True And [e].[action_ID]=38,[e].[billrate],0))/Sum(IIf([e].[billunit]=1 And [e].[volumeunit]=1 And [accounts].[regulatory]=True And [e].[action_ID]=38,1,0)))

/gustav

Commented:
This worked - thank you.

Do more with