Help with Iif calculation (1 Viewer)

guinness

Registered User.
Local time
Today, 09:40
Joined
Mar 15, 2011
Messages
249
Total Hours: IIf([Time]="All Day",[Work Days]*7.4,[Work Days]*3.7)

So what I am looking to return in English is: If the time entered in the Time field is All day then multiply the figure in the Work Days field by 7.4. If the entry in Time isn't All Day then multiply the figure in the Work Days field by 3.7

Bet I'm missing something brutally obvious.

Been too close to this for too many hours.

Thanks in advance

Guinness
 

guinness

Registered User.
Local time
Today, 09:40
Joined
Mar 15, 2011
Messages
249
The result of the Iif statement will create the figure in the new Total Hours column. I'm using the expression builder function.

Does that help?
 

guinness

Registered User.
Local time
Today, 09:40
Joined
Mar 15, 2011
Messages
249
Sorry Dan. Got you now. I'm working in a query. Creating a calculated value based on other fields in the same query.
 

guinness

Registered User.
Local time
Today, 09:40
Joined
Mar 15, 2011
Messages
249
See if the attached PDF helps
 
Last edited:

pr2-eugin

Super Moderator
Local time
Today, 17:40
Joined
Nov 30, 2011
Messages
8,494
Copy the SQL and provide it here !

 

guinness

Registered User.
Local time
Today, 09:40
Joined
Mar 15, 2011
Messages
249
Here you go:

SELECT Resourcing.Project_Title, Resourcing.Start_Date, Resourcing.End_Date, Resourcing.Time, Resourcing.Trainer_Name, Resourcing.Training_Type, dhCountWorkdaysA([Start_Date],[End_Date]) AS [Work Days], Resourcing.Activity, IIf([Time]="All Day",[Work Days]*7.4,[Work Days]*3.7) AS [Total Hours]
FROM Resourcing;

Thanks all for taking time to look at this
 

pr2-eugin

Super Moderator
Local time
Today, 17:40
Joined
Nov 30, 2011
Messages
8,494
Try this,
Code:
SELECT Resourcing.Project_Title, Resourcing.Start_Date, Resourcing.End_Date, Resourcing.[Time], 
Resourcing.Trainer_Name, Resourcing.Training_Type, 
dhCountWorkdaysA(Resourcing.Start_Date, Resourcing.End_Date) AS [Work Days], 
Resourcing.Activity, IIf(Resourcing.[Time] = "All Day", [Work Days]*7.4, [Work Days]*3.7) AS [Total Hours]
FROM Resourcing;
 

guinness

Registered User.
Local time
Today, 09:40
Joined
Mar 15, 2011
Messages
249
Thanks Paul.. I see where you were going with that but unfortunately it still returns the same error.
 

pr2-eugin

Super Moderator
Local time
Today, 17:40
Joined
Nov 30, 2011
Messages
8,494
I have seen you have the habit of using Lookup fields in the tables. So I am guessing that the Time field might also be lookup? In that case you are trying to compare Number to a String, which might be a reason for the error?
 

guinness

Registered User.
Local time
Today, 09:40
Joined
Mar 15, 2011
Messages
249
As in the screenshots from earlier. It runs the query but shows #ERROR instead of a number in the result
 

namliam

The Mailman - AWF VIP
Local time
Today, 18:40
Joined
Aug 11, 2003
Messages
11,695
*brrrr* the column names Time, Work days, etc... It is good practict that one doesnt use reserved words or special characters

Trouble here is you are re-using a column that is a calculation itself, dont think you can re-use a column in the same query
try
Code:
Select *, IIf([Time]="All Day",[Work Days]*7.4,[Work Days]*3.7) AS [Total Hours]
from (
SELECT Resourcing.Project_Title, Resourcing.Start_Date, Resourcing.End_Date, Resourcing.Time, Resourcing.Trainer_Name, Resourcing.Training_Type, dhCountWorkdaysA([Start_Date],[End_Date]) AS [Work Days], Resourcing.Activity
FROM Resourcing)
 

pr2-eugin

Super Moderator
Local time
Today, 17:40
Joined
Nov 30, 2011
Messages
8,494
Trouble here is you are re-using a column that is a calculation itself, dont think you can re-use a column in the same query
Think that only applies for TOTALS queries. Where you can still use the calculation in aggregation, it just you have to use the actual calculation again in the TOTALS clause instead of using the alias.
 

guinness

Registered User.
Local time
Today, 09:40
Joined
Mar 15, 2011
Messages
249
Thanks All

Paul gets the prize. I do use lookup tables where possible to ensure the correct entry format but when I took the lookup off the original Iif calculation worked without any amendments. That was driving me nuts.

Thanks again
 

pr2-eugin

Super Moderator
Local time
Today, 17:40
Joined
Nov 30, 2011
Messages
8,494
Time for some medication to cure your amateur DB skills. ;)
I do use lookup tables where possible to ensure the correct entry format
Absolutely NOT a reason to have Lookup in table still. You use Form's to enter data into table, that is where you will be using a ComboBox/Lookups. The table should have only the raw data, barely numbers. Yes it will not be easily understandable; but will make your DB more smooth, without much bloat, efficient, and quicker.

Here is a great article on the Evils of Lookup fields ! Good luck ! :)
 

CazB

Registered User.
Local time
Today, 17:40
Joined
Jul 17, 2013
Messages
309
another option is you 'repeat' the calculation for work days within the Iif, ie

Code:
 SELECT Resourcing.Project_Title, Resourcing.Start_Date, Resourcing.End_Date, Resourcing.[Time], 
Resourcing.Trainer_Name, Resourcing.Training_Type, 
dhCountWorkdaysA(Resourcing.Start_Date, Resourcing.End_Date) AS [Work Days], 
Resourcing.Activity, IIf(Resourcing.[Time] = "All Day", dhCountWorkdaysA(Resourcing.Start_Date, Resourcing.End_Date)*7.4, dhCountWorkdaysA(Resourcing.Start_Date, Resourcing.End_Date)*3.7) AS [Total Hours]
FROM Resourcing;
 

Users who are viewing this thread

Top Bottom