Match Date to Nearest Month + Match with text field

Caleeco

Well-known Member
Joined
Jan 9, 2016
Messages
980
Office Version
  1. 2010
Platform
  1. Windows
BCDEFGHIJ

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]
[TD="align: center"]Year-Month[/TD]
[TD="align: center"]Product[/TD]
[TD="align: center"]Date[/TD]
[TD="align: right"][/TD]
[TD="align: center"]Year-Month[/TD]
[TD="align: center"]Product[/TD]
[TD="align: center"]Date[/TD]
[TD="align: center"]Defects[/TD]
[TD="align: center"]Desired output[/TD]

[TD="align: center"]2[/TD]
[TD="align: center"]201909[/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]15/09/2019[/TD]
[TD="align: right"][/TD]
[TD="align: center"]201910[/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]17/10/2019[/TD]
[TD="align: center"]13[/TD]
[TD="align: right"]201909[/TD]

[TD="align: center"]3[/TD]
[TD="align: center"]201907[/TD]
[TD="align: center"]B[/TD]
[TD="align: center"]29/07/2019[/TD]
[TD="align: right"][/TD]
[TD="align: center"]201907[/TD]
[TD="align: center"]D[/TD]
[TD="align: center"]15/07/2019[/TD]
[TD="align: center"]15[/TD]
[TD="align: right"]201906[/TD]

[TD="align: center"]4[/TD]
[TD="align: center"]201907[/TD]
[TD="align: center"]C[/TD]
[TD="align: center"]28/07/2019[/TD]
[TD="align: right"][/TD]
[TD="align: center"]201907[/TD]
[TD="align: center"]C[/TD]
[TD="align: center"]02/07/2019[/TD]
[TD="align: center"]4[/TD]
[TD="align: right"]201907[/TD]

[TD="align: center"]5[/TD]
[TD="align: center"]201907[/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]01/07/2019[/TD]
[TD="align: right"][/TD]
[TD="align: center"]201906[/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]27/06/2019[/TD]
[TD="align: center"]14[/TD]
[TD="align: right"]201806[/TD]

[TD="align: center"]6[/TD]
[TD="align: center"]201906[/TD]
[TD="align: center"]D[/TD]
[TD="align: center"]05/06/2019[/TD]
[TD="align: right"][/TD]
[TD="align: center"]201906[/TD]
[TD="align: center"]B[/TD]
[TD="align: center"]18/06/2019[/TD]
[TD="align: center"]8[/TD]
[TD="align: right"]201907[/TD]

[TD="align: center"]7[/TD]
[TD="align: center"]201906[/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]03/06/2019[/TD]
[TD="align: right"][/TD]
[TD="align: center"]201905[/TD]
[TD="align: center"]E[/TD]
[TD="align: center"]01/05/2019[/TD]
[TD="align: center"]13[/TD]
[TD="align: right"]201905[/TD]

</tbody>
Sheet1

Hello,

Im struggling with a data-matching problem (which I can solve using VBA) but would prefer a formula solution.

Data Setup
  • Table 1 (Column B:D) is static data. Shows different products made each month (Year-Month)
  • Table 2 (Column E:J) is static data aside from Column J which needs to evaluate Table 1

The Problem
For each row I need Column J to look at Table 1 and ask the following questions:


  • Have I made Part X (Column B) in the same Month as the defects occured (Column H)? > If YES, return Matched YEARAP (From Table 1 or 2)
  • If I have not made Part X (Column B) in the same month as the defects occurred, find the nearest matching month that product was made > return that matched Year-AP (from Table 1)
  • If Part X is not found in Table 1, return Year-AP from Column F

Worked Example
Row 2 - Part A. Was not made in October (Table 1) > It is found in Table 1 > Nearest Matching month is September > Return 201909.


Is this possible? Any help is appreciated as always
Many Thanks

Caleeco
 

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).
Hi,

I can't match all your desired results for the obvious reason being I don't see 201806 in any of your table's.
Take a look at this:


Book1
ABCDEFGHIJKL
11Year-MonthProductDateYear-MonthProductDateDefectsDesired outputFormula Output
22201909A15-9-2019201910A17-10-201913201909201909
33201907B29-7-2019201907D15-7-201915201906201906
44201907C28-7-2019201907C2-7-20194201907201907
55201907A1-7-2019201906A27-6-201914201806201907
66201906D5-6-2019201906B18-6-20198201907201907
77201906A3-6-2019201905E1-5-201913201905201905
Sheet2
Cell Formulas
RangeFormula
L2{=IFERROR(INDEX($B$2:$B$7,MATCH(INDEX(IF($C$2:$C$7=$G2,$D$2:$D$7,999999),MATCH(MIN(ABS(IF($C$2:$C$7=$G2,$D$2:$D$7,999999)-$H2)),ABS(IF($C$2:$C$7=$G2,$D$2:$D$7,999999)-$H2),0)),$D$2:$D$7,0)),F2)}
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Last edited:
Upvote 0
Take a look at this:
I actually posted something along the same lines but then realised that it wasn't robust so removed it. I think the same occurs with yours - see J5 with my changed sample data below. By my understanding that should be 201906.

Excel Workbook
BCDEFGHIJ
1Year-MonthProductDateYear-MonthProductDateDefectsOutput
2202009Z15/09/2019201910X17/10/201913201910
3201907A15/09/2019201910A17/10/201913202009
4201907C28/07/2019201907C2/07/20194201907
5201909A1/07/2019201906A27/06/201914201909
6201906D5/06/2019201906B18/06/20198201906
7201906A3/06/2019201905E1/05/201913201905
Match Near (2)
 
Last edited:
Upvote 0
Hi @Peter_SSs

Thoughts are similar but i didn't change the request of the OP being "nearest date". If OP added "past nearest date" or had intention to ask but forgot to mention it, my solution isn't right.
Let's wait and see what OP comes back with.

Any way past nearest date can also be solved so ....
 
Upvote 0
Added to post #4 .


Book1
ABCDEFGHIJKLM
11Year-MonthProductDateYear-MonthProductDateDefectsDesired outputNearest DateNearest Past Date
22201909A15-9-2019201910A17-10-201913201909201909201909
33201907B29-7-2019201907D15-7-201915201906201906201906
44201907C28-7-2019201907C2-7-20194201907201907201907
55201907A1-7-2019201906A27-6-201914201806201907201906
66201906D5-6-2019201906B18-6-20198201907201907201906
77201906A3-6-2019201905E1-5-201913201905201905201905
Sheet2
Cell Formulas
RangeFormula
L2{=IFERROR(INDEX($B$2:$B$7,MATCH(INDEX(IF($C$2:$C$7=$G2,$D$2:$D$7,999999),MATCH(MIN(ABS(IF($C$2:$C$7=$G2,$D$2:$D$7,999999)-$H2)),ABS(IF($C$2:$C$7=$G2,$D$2:$D$7,999999)-$H2),0)),$D$2:$D$7,0)),F2)}
M2{=IFERROR(INDEX($B$2:$B$7,MATCH(MAX((IF($C$2:$C$7=$G2,$D$2:$D$7,999999))*IF($C$2:$C$7=$G2,$D$2:$D$7,999999)),$D$2:$D$7,0)),F2)}
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0
First, I had some errors in my sample data in that previous post so please ignore my comments there. I'll try again here.

Excel Workbook
BCDEFGHIJ
1Year-MonthProductDateYear-MonthProductDateDefectsOutput
2202009Z15/09/2019201910X17/10/201913201910
3201907A15/07/2019201910A17/10/201913201909
4201907C28/07/2019201907C2/07/20194201907
5201909A1/09/2019201906A27/06/201914201907
6201906D5/06/2019201906B18/06/20198201906
7201906A3/06/2019201905E1/05/201913201905
Match Near (2)




.. i didn't change the request of the OP being "nearest date".
Actually, the OP didn't request "nearest date" but ..
Have I made Part X (Column B) in the same Month as the defects occured (Column H)? > If YES, return Matched YEARAP (From Table 1 or 2)
So for that row 5 example, there was product A made in June 2019 so that should be the result.

And if there wasn't a product made in the same month, the request was ..
If I have not made Part X (Column B) in the same month as the defects occurred, find the nearest matching month that product was made > return that matched Year-AP (from Table 1)
.. not nearest date.

I'm not trying to be critical here, especially since I haven't been able to come up with a suitable formula, but trying to discuss to get more clarity. :)
 
Upvote 0
@Peter_SSs @jorismoerings

Sorry for the delayed response, I've been pretty ill this week!

Thanks you so much for both your solutions! They both work great, although Peter is closest to my original brief (closest month, not date).

Kind Regards
Caleeco :)
 
Upvote 0
Sorry for the delayed response, I've been pretty ill this week!

Thanks you so much for both your solutions! They both work great
You're welcome. Hope you are now fully recovered. :)
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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