Complicated date math help

woodycanuck

Board Regular
Joined
Dec 18, 2009
Messages
54
Office Version
  1. 365
I have a challenging problem involving date math. For context, this data has to do with calculating a criminal defendant's prior convictions to figure out sentencing. The task is, for each prior conviction, figure out how many days it was before the current offense date. But we need to exclude any time the person was violating probation. Then count how many of those old offenses are less than 2 years old after the excluded time.

Here is a sample. "Start date" shows 3 prior conviction dates, "End Date" is the current offense date, and there are two spans of time that must be excluded from the calculation. Notice that sometimes the exclusion period overlaps an old offense date, so of course we only exclude the portion after the old offense date (it could also extend past the current offense date, same idea). I need to be able to do this for any number of priors, and any set of excluded date ranges. Is it possible to do this in a formula to answer the question: For an offense date of X, how many countable priors does this person have?

HelpMeDodger.xlsx
BCDEFG
22Start DatesEnd DateExclude fromExclude to
231/01/151/01/186/01/167/01/16
241/01/161/01/146/01/15
251/01/17
Sheet1
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
So here is my take on your challenge.
1. Is there an "End Date"[Column D]. If so then proceed with calculation, If not then no result.
2. Determine Overlap Days to exclude.
--If Exclude From>=Start Date & Exclude to <= End Date then Exclude to - Exclude From = Days to exclude
----In the scenario where first statement is false evaluate 1 more time for partial overlap
--If Exclude to >=Start Date then Exclude to - Start date = Days to exclude.
3. "End Date" - "Start Date" - Excluded days (overlap) = Days counted

See if this helps.
Excel Formula:
=(D2-B2)-IF(AND(F2>=B2,G2<=D2),G2-F2+1,IF(G2>=B2,G2-B2+1,0))
 
Upvote 0
Here's another take:

Book1
ABCDEFGHI
22Start DatesIncludeEnd DateExclude fromExclude toPriors
231/1/2015FALSE1/1/20186/1/20167/1/20162
241/1/2016TRUE1/1/20146/1/2015
251/1/2017TRUE
26TRUE
27TRUE
28TRUE
29TRUE
30TRUE
Sheet11
Cell Formulas
RangeFormula
I23I23=SUMPRODUCT(--(D23-B23:B30<732),--C23:C30)
C23:C30C23=MIN((B23-F$23:F$30)*(B23-G$23:G$30))>=0


I added the C column to decide if the date in column B falls within any of the ranges in F23:G30. I probably could have included it in the final formula, but it would have been pretty messy. Then the final formula in I23 is pretty simple. Only 2 conditions, if the date is within 732 days of the date in D23, and if the date should be included by using the C column.

Depending on what version of Excel you have, you may need to confirm the C23 formula with Control+Shift+Enter, or this might work:

Excel Formula:
=AGGREGATE(15,6,(B23-F$23:F$30)*(B23-G$23:G$30),1)>=0

If you could update your profile with the version of Excel you're using, it would help us know what functions we can suggest.



Hope this helps!
 
Upvote 0
Thanks for the ideas guys, these are really helpful! I'm going to sit down over the weekend and try to sort the whole thing out.
 
Upvote 0

Forum statistics

Threads
1,215,641
Messages
6,125,983
Members
449,276
Latest member
surendra75

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