Lookup a value if the dates fall between two dates
89,650
Solution 1
Assuming you have multiple "windows" listed in order with no gaps perhaps try LOOKUP like
=LOOKUP(D1,A1:C10)
see example
Solution 2
Cell
a1 = 1/1/2014 8:00
b1 = 1/4/2014 10:00
c1 = 11
d1 = 1/3/2014 7:00
e1 =IF(AND($D$1-A1>0,B1-$D$1>0),C1,"outside")
Solution 3
You can use this, it will work even if the dates are not in order, as long as values in C column are numeric. It will return 0 if no match
=SUMPRODUCT((D1>=A1:A10)*(D1<=B1:B10)*C1:C10)
Author by
user3084662
Updated on July 09, 2022Comments
-
user3084662 almost 2 years
I have window dates in two cells. For example:
- a1 = 1/1/2014 8:00
- b1 = 1/4/2014 10:00
- c1 = 11
Where
- A is the start date
- B is the end date
- C is the code for the window
In cell d1 I have the date: 1/3/2014 7:00
I would like to look up the date in a/b and if d falls between those dates then I would like for it to return the code for the window.
-
user3084662 over 10 yearsI have 52 windows...so I would have to do 52 if statements that are nested
-
barry houdini over 10 yearsThat's why LOOKUP is good - you can check all 52 windows in one go - see my answer
-
Johnny over 10 yearsthis doesn't check if it is in range?
-
Johnny over 10 yearsyour lookup doesn't check if it is between two dates.
-
Johnny over 10 yearsYour question could be clearer. Do you just want to look up which windows, across multiple rows that don't exist in your question eg. a1:c52 ? each row being a different window?
-
barry houdini over 10 yearsAssuming no gaps between ranges and ranges in ascending order you don't really need to check within ranges - the Lookup matches D1 with the relevant column A value and returns the correct number from column C. If D1 may not be in any range then you can add a simple IF, like
=IF(D1>B10,NA(),LOOKUP(D1,A1:C10))
-
Johnny over 10 yearsif you want to do a lookup do one to find the first row where $Cn==$En.
-
barry houdini over 10 yearsI was waiting for somebody to suggest SUMPRODUCT or SUMIFS, +1 - personally I always prefer to use "lookup" type formulas when a single value is required - they can also be used for retrieving text values...but this approach has advantages, e.g. can have gaps, don't need to be sorted as you say
-
barry houdini over 10 years52 windows? Are they weeks?
-
CRondao over 10 yearsYeh, eventually, if column C is not numeric we add a match to look for value 1 and then use index to get the value. But, in that case, it will be an array formula...