Karen Schaefer
asked on
Help with syntax IIF statement
I need to modify the following iff statement to include another Iff that will check to see if the
CompletionDt = Null or DateRequired<=Date(), if either = True than do not include the value within the If statement below as part of the Total count that is divided by.
Karen
CompletionDt = Null or DateRequired<=Date(), if either = True than do not include the value within the If statement below as part of the Total count that is divided by.
Karen
IIf(Sum(IIf([CompletionDt]<=[DateRequired],1,0))=0,0,Sum(IIf([CompletionDt]
<=[DateRequired],1,0))/(Sum(IIf([CompletionDt]
<=[DateRequired],1,0))+Sum(IIf([CompletionDt]>
[DateRequired],1,0))+Sum(IIf([DateRequired]<Date(),1,0))))
Can you give us a plain English description of what you are trying to calculate with the two date fields?
ASKER
Not quite. I am looking for the Sum of Percentage complete OnTime per Manager by calculating the individual employees' Percentage Complete OnTime. So I need the current equation to work as is but not include any employee's Courses that are in the future or have not completed as of current date as part of the summation.
Say Employee 1 has been assigned 5 courses over the next year - of those 5 courses 1 has been CompletedOntime 1 was CompletedLate and 1 CompletedDelinquent as of current date and the remaining 2 are in the future - so the Pct = ?% of OnTime
CompletedOntime: Sum(IIf([CompletionDt]<=[D ateRequire d],1,0)) +
CompletedLate: Sum(IIf([CompletionDt]>[Da teRequired ],1,0)) +
CompletedDelinquent: Sum(IIf([DateRequired]<Dat e(),1,0)
Hope this explains what I am trying to accomplish.
Karen
Say Employee 1 has been assigned 5 courses over the next year - of those 5 courses 1 has been CompletedOntime 1 was CompletedLate and 1 CompletedDelinquent as of current date and the remaining 2 are in the future - so the Pct = ?% of OnTime
CompletedOntime: Sum(IIf([CompletionDt]<=[D
CompletedLate: Sum(IIf([CompletionDt]>[Da
CompletedDelinquent: Sum(IIf([DateRequired]<Dat
Hope this explains what I am trying to accomplish.
Karen
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Just a note here:
When your formulas get this complex, please consider using Functions instead.
Remember:
You can't add comments to Formulas
You can't add comments to Formulas
...but you can do both and more with Functions.
SELECT MgrID,
-Sum(CompletionDt<=DateReq uired) AS OnTime,
-Sum(CompletionDt>DateRequ ired) as CompLate,
-Sum(DateRequired>Date()) AS InFuture,
OnTime/(OnTime+CompLate) as PctOnTime
FROM MgrCourses
GROUP BY MgrID
As you can see, I hate nested IIF()'s ;-)
-Sum(CompletionDt<=DateReq
-Sum(CompletionDt>DateRequ
-Sum(DateRequired>Date()) AS InFuture,
OnTime/(OnTime+CompLate) as PctOnTime
FROM MgrCourses
GROUP BY MgrID
As you can see, I hate nested IIF()'s ;-)
ASKER
"Remember:
You can't add comments to Formulas
You can't add comments to Formulas
...but you can do both and more with Functions."
I know that - it was to illustrate my point.
K
You can't add comments to Formulas
You can't add comments to Formulas
...but you can do both and more with Functions."
I know that - it was to illustrate my point.
K
ASKER
GRayL:
this is actual 1 field in an existing query.
K
this is actual 1 field in an existing query.
K
ASKER
cyberkiwi:
Your example - returns #Error.
K
Your example - returns #Error.
K
You did not state how you wanted to treat the situation where the denominator was zero (divide by zero) error.
If all you want is the MgrID and his percent, then do a query of a query.
IIf(
(Sum(IIf([CompletionDt]<=[ DateRequir ed] AND [DateRequired]<=Date(),1,0 ))
+Sum(IIf([CompletionDt]>[D ateRequire d] AND [DateRequired]<=Date(),1,0 ))
+Sum(IIf(IsNull(Completion Dt) AND [DateRequired]<Date(),1,0) ))=0,
0,
Sum(IIf([CompletionDt]<=[D ateRequire d] AND [DateRequired]<=Date(),1,0 ))
/
IIf((Sum(IIf([CompletionDt ]<=[DateRe quired] AND [DateRequired]<=Date(),1,0 ))
+Sum(IIf([CompletionDt]>[D ateRequire d] AND [DateRequired]<=Date(),1,0 ))
+Sum(IIf(IsNull(Completion Dt) AND [DateRequired]<Date(),1,0) ))=0, 1,
(Sum(IIf([CompletionDt]<=[ DateRequir ed] AND [DateRequired]<=Date(),1,0 ))
+Sum(IIf([CompletionDt]>[D ateRequire d] AND [DateRequired]<=Date(),1,0 ))
+Sum(IIf(IsNull(Completion Dt) AND [DateRequired]<Date(),1,0) ))))
(Sum(IIf([CompletionDt]<=[
+Sum(IIf([CompletionDt]>[D
+Sum(IIf(IsNull(Completion
0,
Sum(IIf([CompletionDt]<=[D
/
IIf((Sum(IIf([CompletionDt
+Sum(IIf([CompletionDt]>[D
+Sum(IIf(IsNull(Completion
(Sum(IIf([CompletionDt]<=[
+Sum(IIf([CompletionDt]>[D
+Sum(IIf(IsNull(Completion
ASKER
No there are several other fields I need Percent On Time as of Current date by Empl(Bems).
K
K
SELECT qryUnitChief.UCBEMS, qryUnitChief.UnitChief, qryEmployeeManager.Org, qryEmployeeManager.MgrName, [tblEmployee].[LastName] & ", " & [tblEmployee].[Firstname] AS EmployeeName, tblEmployee.BEMS, tblCourseList_lkup.StandardRequiredDt AS MandReqDate, tblCourseList_lkup.[Ilp Learning Cd], qryEmpRequiredLearning.CompletionDt, IIf(Sum(IIf([CompletionDt]<=[DateRequired],1,0))=0,0,Sum(IIf([CompletionDt]<=[DateRequired],1,0))/(Sum(IIf([CompletionDt]<=[DateRequired],1,0))+Sum(IIf([CompletionDt]>[DateRequired],1,0))+Sum(IIf([DateRequired]<Date(),1,0)))) AS Pct, IIf(DateDiff("d",[ServiceDt],Date())<=60,"X","") AS NH, IIf(IsNull([xfrout]),IIf(DateDiff("d",[xfrIn],Date())<=60,"X","")) AS Transfers, IIf(IsNull([LOA_StartDate])=False And IsNull([LOA_EndDate]),"X","") AS LOA
FROM (((tblEmployee LEFT JOIN qryUnitChief ON tblEmployee.UnitChief = qryUnitChief.UnitChiefID) LEFT JOIN qryEmployeeManager ON tblEmployee.Mgr_OrgNo = qryEmployeeManager.OrgCtr) INNER JOIN qryEmpRequiredLearning ON tblEmployee.BEMS = qryEmpRequiredLearning.BEMS) INNER JOIN (tblCourseClassification_lkup INNER JOIN tblCourseList_lkup ON tblCourseClassification_lkup.ClassificationRecID = tblCourseList_lkup.ClassificationRecID) ON qryEmpRequiredLearning.CourseRecID = tblCourseList_lkup.CourseRecID
GROUP BY qryUnitChief.UCBEMS, qryUnitChief.UnitChief, qryEmployeeManager.Org, qryEmployeeManager.MgrName, [tblEmployee].[LastName] & ", " & [tblEmployee].[Firstname], tblEmployee.BEMS, tblCourseList_lkup.StandardRequiredDt, tblCourseList_lkup.[Ilp Learning Cd], qryEmpRequiredLearning.CompletionDt, IIf(DateDiff("d",[ServiceDt],Date())<=60,"X",""), IIf(IsNull([xfrout]),IIf(DateDiff("d",[xfrIn],Date())<=60,"X","")), IIf(IsNull([LOA_StartDate])=False And IsNull([LOA_EndDate]),"X",""), tblEmployee.LastName, tblEmployee.FirstName, tblEmployee.MidName, tblCourseList_lkup.[Ilp Learning Cd], tblCourseList_lkup.OnXLS, tblCourseList_lkup.InActive
HAVING (((tblCourseList_lkup.OnXLS)<>0) AND ((tblCourseList_lkup.InActive)<>-1))
ORDER BY tblEmployee.LastName, tblEmployee.FirstName, tblEmployee.MidName, tblCourseList_lkup.[Ilp Learning Cd];
ASKER
cyberkiwi:
Your latest only returns Zero.
Thanks for your efforts,
K
Your latest only returns Zero.
Thanks for your efforts,
K
ASKER
GRayL:
Divided by Zero return Zero
Divided by Zero return Zero
ASKER
Ok lets simplified this some - after talking with my client - we need to accomplish the following To determine On Time (first step)
If the CompletionDate = Null then return 0
Else
if CompletionDate <= Required date the return 1
To calculate Percentage of OnTime then
Take the Sum(the 1s) and divide by the Sum of OnTime, Late, and Delinquent.
So what do I need to modify in my original equation to get this to happen?
karen
If the CompletionDate = Null then return 0
Else
if CompletionDate <= Required date the return 1
To calculate Percentage of OnTime then
Take the Sum(the 1s) and divide by the Sum of OnTime, Late, and Delinquent.
So what do I need to modify in my original equation to get this to happen?
karen
'These are the only possible success cases.
if CompletionDate is null and DateRequired => Date() then 1 (Can still be on time, but is in the future)
if CompletionDate <= DateRequired then 1 (This is on time)
/
'These are all cases
If CompletionDate is null and DateRequired < Date() then 0 (This is late)
if CompletionDate is null and DateRequired => Date() then 1 (Can still be on time, but is in the future)
if CompletionDate <= DateRequired then 1 (This is on time)
if CompletionDate > DateRequired then 0 (This was late)
so logically wouldn't it look like so?
sum(
([CompletionDt]<=[DateRequ ired]) +
(isnull(CompletionDate) = True and DateRequired => Date())
)
/
sum(
([CompletionDt]<=[DateRequ ired]) +
(isnull(CompletionDate) = True and DateRequired => Date())
([CompletionDt]>[DateRequi red]) +
(isnull(CompletionDate) = True and DateRequired < Date())
)
J
if CompletionDate is null and DateRequired => Date() then 1 (Can still be on time, but is in the future)
if CompletionDate <= DateRequired then 1 (This is on time)
/
'These are all cases
If CompletionDate is null and DateRequired < Date() then 0 (This is late)
if CompletionDate is null and DateRequired => Date() then 1 (Can still be on time, but is in the future)
if CompletionDate <= DateRequired then 1 (This is on time)
if CompletionDate > DateRequired then 0 (This was late)
so logically wouldn't it look like so?
sum(
([CompletionDt]<=[DateRequ
(isnull(CompletionDate) = True and DateRequired => Date())
)
/
sum(
([CompletionDt]<=[DateRequ
(isnull(CompletionDate) = True and DateRequired => Date())
([CompletionDt]>[DateRequi
(isnull(CompletionDate) = True and DateRequired < Date())
)
J
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Can you have a Manager with zero DateRequired?
ASKER
Please look at my latest post for revision to my request.
K
K
ASKER
Here is my latest attempt - get aggregate issue.
IIf(Not IsNull([CompletionDt]),[CompletionDt]<=[DateRequired],1)/(Sum(IIf(Not IsNull([CompletionDt]),[CompletionDt]<=[DateRequired],1)))+Sum(IIf(Not IsNull([CompletionDt]),[CompletionDt]>[DateRequired],1)+Sum(IIf([DateRequired]<Date(),1)))
ASKER
Here is my latest attempt - I broke up the queries first to get the totals of OnTime, Late, & Delinquent,
then Summary query to calculate the percentages, However, I am getting Divide by Zero errors.
What am I missing?
then Summary query to calculate the percentages, However, I am getting Divide by Zero errors.
What am I missing?
SELECT qryEmpTrain_TempSummary.UCBEMS, qryEmpTrain_TempSummary.UnitChief, qryEmpTrain_TempSummary.MgrName, qryEmpTrain_TempSummary.BEMS, qryEmpTrain_TempSummary.Org, qryEmpTrain_TempSummary.OnTime, qryEmpTrain_TempSummary.Late, qryEmpTrain_TempSummary.Del, Sum(([OnTime]+[Late]+[Del])) AS TotalCourseCt, IIf(Sum([Ontime]+[Late])<>0,([Ontime]+[Late])/([OnTime]+[Late]+[Del]),0) AS [Total%Completed], Sum(IIf(Not IsNull([OnTime]),[OnTime]/([OnTime]+[Late]+[Del]),0)) AS [%CompleteOnTime], Sum(IIf(Not IsNull([Late]),[Late]/([OnTime]+[Late]+[Del]),0)) AS [%CompleteLate], Sum(IIf(Not IsNull([Del]),[Del]/([OnTime]+[Late]+[Del]),0)) AS [%CompleteDel], qryEmpTrain_TempSummary.OrgCtr, qryEmpTrain_TempSummary.ManagerID
FROM qryEmpTrain_TempSummary
GROUP BY qryEmpTrain_TempSummary.UCBEMS, qryEmpTrain_TempSummary.UnitChief, qryEmpTrain_TempSummary.MgrName, qryEmpTrain_TempSummary.BEMS, qryEmpTrain_TempSummary.Org, qryEmpTrain_TempSummary.OnTime, qryEmpTrain_TempSummary.Late, qryEmpTrain_TempSummary.Del, qryEmpTrain_TempSummary.OrgCtr, qryEmpTrain_TempSummary.ManagerID;
ASKER
correct the Divide by Zero error by change the Groupby to Expression instead of Sum
BuT
Now I am getting some values with a Negative number from the 1st query - What am I am missing?
Karen
Sum(IIf(Not IsNull([CompletionDt]),[Co mpletionDt ]<=[DateRe quired],1) ) AS OnTime,
Sum(IIf(Not IsNull([CompletionDt]),[Co mpletionDt ]>[DateReq uired],1)) AS Late,
Sum(IIf([DateRequired]<Dat e(),1,0)) AS Del,
BuT
Now I am getting some values with a Negative number from the 1st query - What am I am missing?
Karen
Sum(IIf(Not IsNull([CompletionDt]),[Co
Sum(IIf(Not IsNull([CompletionDt]),[Co
Sum(IIf([DateRequired]<Dat
SELECT qryUnitChief.UCBEMS, tblEmployee.UnitChief, qryEmployeeManager.MgrName, tblEmployee.BEMS, qryEmployeeManager.Org, Sum(IIf(Not IsNull([CompletionDt]),[CompletionDt]<=[DateRequired],1)) AS OnTime, Sum(IIf(Not IsNull([CompletionDt]),[CompletionDt]>[DateRequired],1)) AS Late, Sum(IIf([DateRequired]<Date(),1,0)) AS Del, qryEmployeeManager.OrgCtr, qryEmployeeManager.ManagerID
FROM ((tblEmployee LEFT JOIN qryEmployeeManager ON tblEmployee.Mgr_OrgNo = qryEmployeeManager.OrgCtr) INNER JOIN qryUnitChief ON tblEmployee.UnitChief = qryUnitChief.UnitChiefID) INNER JOIN qryEmpRequiredLearning ON tblEmployee.BEMS = qryEmpRequiredLearning.BEMS
GROUP BY qryUnitChief.UCBEMS, tblEmployee.UnitChief, qryEmployeeManager.MgrName, tblEmployee.BEMS, qryEmployeeManager.Org, qryEmployeeManager.OrgCtr, qryEmployeeManager.ManagerID;
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for your efforts - I have not had time to get back to this post or problem - pulled on another project.
I awarded the points for your time.
I awarded the points for your time.
You want to change the part after the / (divide)?
It seems to already have a condition for DateRequired<=Date()
Do you mean this?
IIf(
Sum(IIf([CompletionDt]<=[D
0,
Sum(IIf([CompletionDt]<=[D
/
(Sum(IIf([CompletionDt]<=[
+Sum(IIf([CompletionDt]>[D
+Sum(IIf(IsNull(CompletionDt) OR [DateRequired]<Date(),1,0)
)