Find the Closest Date Match

kdsanderson30

New Member
Joined
Jun 12, 2014
Messages
17
I have been tasked to help with a tax project for my company and I am stuck.

We have a worksheet that lists multiple trucks, the date they filled their truck with fuel, gallons of fuel and various other data. The dilemma is we need to find the line for each Truck out of 21467 Lines that is closest to a specific date. So lets say column B list truck 000034 4 times with 4 separate dates, how do you pull the date closest to January 1. (the reports shows 2 weeks prior and 2 weeks after Jan 1) some trucks got gas 10 times during this period and others maybe one to five times.

Column A is a spacer for totals (from an import)

so the worksheet goes as follows:

B- Vehicle #
C- Vehicle Description
D- First Name
E- Last Name
F- Driver
G Trans Date (transaction)
H- Trans Time (transaction)
I- Odometer
J Gallons
K Dollars
L- Unit Cost
M Product (Unleaded or Diesel)
N Department
O Department level
P Business Unit
Q Product Line
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Maybe this will give you a starting point.
This is an array formula and must be entered with
Excel Workbook
ABCDEF
1TruckDateClosest1/1/2015
211/12/2015
3212/28/2014TruckDate
411/14/2015112/28/2014
521/4/201521/4/2015
6412/30/2014312/29/2014
7112/27/2014412/30/2014
8312/29/2014
941/12/2015
10112/28/2014
11
CTRL-SHIFT-ENTER.
 
Upvote 0
My approach would be to create a key of Vehicle# and Trans date in column R =B3&"_&G3
sort the dataset by R ascending


the use =INDEX(B5:R21467,IF(ISNA(MATCH(Z1,R5:R21467,0)),MATCH(Z1,R5:R21467,1),MATCH(Z1,R5:R21467,0)),10)

What this does is sees if there is an exact match for a vehicle on the day, and if not the next closest to the date (but Prior to the date such 31/12/14)

and returns column 10 (dollars)

where Z1 = the Vehicle Number and 01/01/15 looking like 05643_42005


This should work
 
Last edited:
Upvote 0
Maybe this will give you a starting point.
This is an array formula and must be entered with
Excel Workbook
ABCDEF
1TruckDate*Closest1/1/2015*
211/12/2015****
3212/28/2014*TruckDate*
411/14/2015*112/28/2014*
521/4/2015*21/4/2015*
6412/30/2014*312/29/2014*
7112/27/2014*412/30/2014*
8312/29/2014****
941/12/2015****
10112/28/2014****
11******
CTRL-SHIFT-ENTER.



When I tried this is gave me the answer of the truck #. I am not sure what I did wrong, but there is over 1500 trucks here so that one is kind of tricky.
 
Upvote 0
This was just an example, you would need to change to match your ranges. So, if your dates are in column G the Index range would need to be column G not B as in the example. You would also need to change the column A in my example to your column B if that's where the vehicle # is.
 
Upvote 0
the purpose of this is to find the odometer reading for the vehicle closest to 1/1/15 out of a 4 week period 2 weeks prior and after 1/1/15 for tax purposes. I guess I was not very clear, but the example you gave would tell the closest date for the truck, but would not tell the ODO reading etc.
 
Upvote 0
see below.
You will need to make changes to ranges for your date.
The first part of the INDEX function will determine the data it brings in.
Code:
=INDEX([COLOR=#ff0000]$I$2:$I$10[/COLOR],MATCH(MIN(IF($B$2:$B$10=$B16,ABS($G$2:$G$10-$C$13))),IF($B$2:$B$10=$B16,ABS($G$2:$G$10-$C$13)),0))
Change part in red for data you want.

Formula can just be copied down if you are careful in how you lock the columns and rows.

It is an array function and must be entered with
Excel Workbook
ABCDEFGHIJ
1Vech. #Vech DescFNLNDriverTran. DateTran, TOdometerGal.
21vech11/12/2015125000
32vech212/28/201467000
41vech11/14/2015130000
52vech21/4/201575000
64vech412/30/201488000
71vech112/27/2014102000
83vech312/29/201480000
94vech41/12/2015105000
101vech112/28/2014102500
11
12
13Closest1/1/2015
14
15TruckDateOdometer
16112/28/14102500
1721/4/1575000
18312/29/1480000
19412/30/1488000
20
CTRL-SHIFT-ENTER.
 
Upvote 0
Can you use this. I first found out the unique items. Then I used data validation for this item in my formula to list the dates for item 1. Then a formula to list all the ODO's for item . Then I created formulas find the nearest date and related ODO.


Excel 2012
ABCDEFG
1ItemDateODO1/10/2015Closest Date01/07/15
2101/07/15100ODO100
3212/02/1475
4302/23/15100Item1
5101/15/15125
6212/29/14100DateODO
733/12/21520001/07/15100
8201/17/1515001/15/15125
9101/14/1512001/14/15120
10201/03/15160
11301/17/1575
12301/06/1550
13
141
152
163
Sheet1
Cell Formulas
RangeFormula
F1{=MIN(IF($E$7:$E$12<=$D$1,E$7:E$12))}
F7{=IFERROR(INDEX(C$2:C$12,SMALL(IF($A$2:$A$12=$F$4,ROW(C$2:C$12)-ROW(C$2)+1),ROWS(F$7:F7))),"")}
E7{=IFERROR(INDEX(B$2:B$12,SMALL(IF($A$2:$A$12=$F$4,ROW(B$2:B$12)-ROW(B$2)+1),ROWS(E$7:E7))),"")}
A14{=IFERROR(INDEX($A$2:$A$12,SMALL(IF(FREQUENCY(IF($A$2:$A$12<>"",MATCH($A$2:$A$12,$A$2:$A$12,0)),ROW($A$2:$A$12)-ROW($A$2)+1),ROW($A$2:$A$12)-ROW($A$2)+1),ROWS($A$14:A14))),"")}
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0
see below.
You will need to make changes to ranges for your date.
The first part of the INDEX function will determine the data it brings in.
Code:
=INDEX([COLOR=#ff0000]$I$2:$I$10[/COLOR],MATCH(MIN(IF($B$2:$B$10=$B16,ABS($G$2:$G$10-$C$13))),IF($B$2:$B$10=$B16,ABS($G$2:$G$10-$C$13)),0))
Change part in red for data you want.

Formula can just be copied down if you are careful in how you lock the columns and rows.

It is an array function and must be entered with
Excel Workbook
ABCDEFGHIJ
1*Vech. #Vech DescFNLNDriverTran. DateTran, TOdometerGal.
2*1vech1***1/12/2015*125000*
3*2vech2***12/28/2014*67000*
4*1vech1***1/14/2015*130000*
5*2vech2***1/4/2015*75000*
6*4vech4***12/30/2014*88000*
7*1vech1***12/27/2014*102000*
8*3vech3***12/29/2014*80000*
9*4vech4***1/12/2015*105000*
10*1vech1***12/28/2014*102500*
11**********
12**********
13*Closest1/1/2015*******
14**********
15*TruckDateOdometer******
16*112/28/14102500******
17*21/4/1575000******
18*312/29/1480000******
19*412/30/1488000******
20**********
CTRL-SHIFT-ENTER.



this worked PERFECTLY!!!! thank you so much for your very valuable time! It was VERY MUCH APPRECIATED!!! thank you!!!
 
Upvote 0
You're welcome. Glad that worked for you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,326
Messages
6,124,268
Members
449,149
Latest member
mwdbActuary

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top