Formula in a Query in the Feild


Well-known Member
In design mode you can enter a column and make it a formula (example: MONTH1_PD: IIf([PDIR Completed] Is Null,DateDiff("d",[PDIR Due Date],Now()),"") This is just an example

I'm need a rather complex formula

I need a formula that says IIF [PDIR Completed] >= [PDIR Due Date], "1","0" But that's the easy part, what I need the formula to do first is check the date in the [PDIR Due Date] field. If its in the month 3 months earlier than today, then do the above. So if today is 9/13/2019, I want to check to see if the [PDIR Due Date] is in June (between 6/1/2019 and 6/30/2019. that's the part I am struggling with. Any help is appreciated

PS I don't want to put a date criteria in the query for [PDIR Due Date]. I need a Dynamic formula that I do not have to change the query criteria every month.



MrExcel MVP, Junior Admin
You should be able to nest an IIF statement to do the second (two IIF functions nexted within each other), and use DateDiff to get the difference in months.

See here for some explanations and examples for nested IIF:

I'll let you take a crack at it, and seehow you do. It should be pretty straightforward.
Post back and let us know if you figure it out or still need help!


Well-known Member
IIf statement to for the formula isn't difficult, its doing the first condition in the formula to see if the date is 3 months earlier (6/1/2019-6/30/2019) I thought maybe do some kind of EMonth(NOW,-4)+1 and Eomonth NOW(,3) but could get to a working formula


MrExcel MVP, Junior Admin
Why can't you just do a DATEDIFF on those two dates, and see if the difference is 3 months?
If you choose months ("m") instead of days ("d"), you shouldn't need to worry about day of the month (as it will ignore days), i.e.
6/1/2019 - 9/30/2019
6/15/2019 - 9/30/2019
6/30/2019 - 9/1/2019
will all return 3.


Well-known Member
I see, so use the Date Diff as my condition (Iff DatedDiff=3)

I was going a much longer route IIF(([PDIR Due Date]>DateSerial(Year(Now()-90),Month(Now()-90),1)) And ([PDIR Due Date]<DateSerial(Year(Now()-90),Month(Now()-90)+1,0)),1,0)


MrExcel MVP, Junior Admin
I see, so use the Date Diff as my condition (Iff DatedDiff=3)

I was going a much longer route IIF(([PDIR Due Date]>DateSerial(Year(Now()-90),Month(Now()-90),1)) And ([PDIR Due Date]
Yep, no need to covercomplicate it!:)


Well-known Member
Don't forget that if expressions start getting real hairy, you can call a function from a calculated query field as well.

Some videos you may like

This Week's Hot Topics

  • Get External Data (long shot question!)
    This is likely a long shot but I am wondering if it is at all possible for Excel to somehow 'change' the contents of a URL that is being linked to...
  • Importing multiple excel files into one spreadsheet
    Hi, I'm trying to import multiple excel files (with the same format into a single spreadsheet) so that each day's file is listed underneath the...
  • Cell Formatting
    Good Morning, I need to format a few different cells in the following manners: A1 has to always add a colon (:) after whatever is typed in by a...
  • How to copy multiple rows using If
    Hi all, I'm very new to VBA and have written this simple code to copy certain cells if a certain cell within that row contains any data. I need...
  • Workbook_Change stopped working !
    I am working on an app to speed up & automate processing of Credit Cards statements. After data is input from a CSV file, it is presented to the...
  • VBA If statement
    Dear All, I have two dates, where I'd like a message box to pop, if the dates are between this criteria. [CODE] sDate1 = #10/1/2019#...