Formula in a Query in the Feild

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
1,690
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.

Thanks
 

Some videos you may like

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
52,094
Office Version
365
Platform
Windows
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:
http://www.simply-access.com/Nested_IIF_Statement.html
https://support.office.com/en-us/article/iif-function-32436ecf-c629-48a3-9900-647539c764e3

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!
 

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
1,690
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
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
52,094
Office Version
365
Platform
Windows
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.
 

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
1,690
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)
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
52,094
Office Version
365
Platform
Windows
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!:)
 

Micron

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

Forum statistics

Threads
1,089,488
Messages
5,408,560
Members
403,215
Latest member
DblDocWhitaker

This Week's Hot Topics

  • help please
    SORRY NOT ANY GOOD AT EXCEL SO HELP WOULD BE MUCH APPRECIATED this formula is in a sheet called ignore...
  • two formulas needed
    Hello, I'll try my best to explain this: First formula needed in Sheet1 cell A2: If Sheet1 cell B2 = Sheet2 cell B2 then return a 1. If not then...
  • Dynamic Counts
    Good afternoon, we are tidying up some data & the data seems to be growing quicker than we are tidying it up! What we confirm (by reviewing it...
  • Help Excel formula eliminate duplicate values and keep only 2 identical rows.
    as picture below column A has a duplicate value. but the values are not the same as the rule. sometimes 4 rows, sometimes 10 rows or 7 or 9...
  • Macro Compile Error Sub or Function not defined
    Hello, I am trying to run macros from a validation list, all macros have been created and run perfectly on there own but I'm getting a compile...
  • Last row combined with Current Region VBA
    I'm generally happy finding the last row of data through something like Lastrow = Cells(Rows.Count, "D").End(xlUp) but I don't always receive data...
Top