# Formula for matrix (dates in ascending order)

Firstly, apologies for this question but I couldn't really wrap my head on how to do this (ultimate aim is to put it in app scripts)

Let's say I have a table that is updated daily showing the fruit Tony eats on that day

For some reason, I need to find the number of days between eating Pear followed by Apple. Here it is manually done by subtracting the dates

How would I translate this into a formula? I tried reading up on VLOOKUP, INDEX,SMALL, XLOOKUP but didn't get a hang of the approach

I am aware the on SE, one needs to show what has been done by OP to solve but in this case, I am totally confused, sorry again. • This blog seemed to give a hint, but the exact table and formula resulted in a formula parse error May 21 at 17:51
• Would putting a conditional in the 'Days' column solve your problem, eg. =If(b2='pear',0,c1+1)? If it makes sense will add an answer. May 21 at 18:04
• @AndroidNovice I tried something similar but it didn't help; I will try again tomorrow May 21 at 18:06

``````=IF(\$B2="Apple",A2-SORT(FILTER(A:A,B:B="Pear", A:A<\$A2),1,FALSE),"")
`const cell = sh.getRange(`C:C\${sh.getLastRow()}`); cell.setFormula(`=IF(\$B2="Apple",A2-SORT(FILTER(A:A,B:B="Pear", A:A<\$A2),1,FALSE),"")`);`