How to calculate the number of days between two dates dependent on criteria

JENKELLY

New Member
Joined
Jan 22, 2019
Messages
5
Hi
I have the following data (this is an example - there are approx 45000 rows in the original doc which will only increase with time!):
[TABLE="width: 348"]
<colgroup><col><col><col></colgroup><tbody>[TR]
[TD]Cust No[/TD]
[TD]End of Period Date[/TD]
[TD] Carried Forward Balance[/TD]
[/TR]
[TR]
[TD]JH3[/TD]
[TD]2019-01-07[/TD]
[TD="align: right"]1032.06[/TD]
[/TR]
[TR]
[TD]JH3[/TD]
[TD]2018-12-07[/TD]
[TD="align: right"]1130.82[/TD]
[/TR]
[TR]
[TD]JH3[/TD]
[TD]2018-12-07[/TD]
[TD="align: right"]1732.06[/TD]
[/TR]
[TR]
[TD]JH3[/TD]
[TD]2018-12-07[/TD]
[TD="align: right"]1132.06[/TD]
[/TR]
[TR]
[TD]JH3[/TD]
[TD]2018-11-12[/TD]
[TD="align: right"]1230.82[/TD]
[/TR]
[TR]
[TD]RB5[/TD]
[TD]2019-01-07[/TD]
[TD="align: right"]1182.76[/TD]
[/TR]
[TR]
[TD]RB5[/TD]
[TD]2018-12-07[/TD]
[TD="align: right"]1082.76[/TD]
[/TR]
[TR]
[TD]RB5[/TD]
[TD]2018-12-07[/TD]
[TD="align: right"]982.76[/TD]
[/TR]
[TR]
[TD]RB5[/TD]
[TD]2018-12-07[/TD]
[TD="align: right"]40.14[/TD]
[/TR]
[TR]
[TD]RB5[/TD]
[TD]2018-11-12[/TD]
[TD="align: right"]611.45[/TD]
[/TR]
[TR]
[TD]RB5[/TD]
[TD]2018-10-03[/TD]
[TD="align: right"]-1.17[/TD]
[/TR]
[TR]
[TD]RB5[/TD]
[TD]2018-09-04[/TD]
[TD="align: right"]570.14[/TD]
[/TR]
[TR]
[TD]RB5[/TD]
[TD]2018-08-14[/TD]
[TD="align: right"]40.14[/TD]
[/TR]
[TR]
[TD]RB5[/TD]
[TD]2018-07-04[/TD]
[TD="align: right"]-1.17[/TD]
[/TR]
[TR]
[TD]YT6[/TD]
[TD]2019-01-07[/TD]
[TD="align: right"]1020.53[/TD]
[/TR]
[TR]
[TD]YT6[/TD]
[TD]2018-12-07[/TD]
[TD="align: right"]1020.53[/TD]
[/TR]
[TR]
[TD]YT6[/TD]
[TD]2018-12-07[/TD]
[TD="align: right"]1020.53[/TD]
[/TR]
[TR]
[TD]YT6[/TD]
[TD]2018-12-07[/TD]
[TD="align: right"]510.25[/TD]
[/TR]
[TR]
[TD]YT6[/TD]
[TD]2018-11-12[/TD]
[TD="align: right"]1020.53[/TD]
[/TR]
[TR]
[TD]YT6[/TD]
[TD]2018-10-03[/TD]
[TD="align: right"]510.25[/TD]
[/TR]
[TR]
[TD]YT6[/TD]
[TD]2018-09-04[/TD]
[TD="align: right"]-0.03[/TD]
[/TR]
[TR]
[TD]YT6[/TD]
[TD]2018-08-14[/TD]
[TD="align: right"]510.25[/TD]
[/TR]
[TR]
[TD]YT6[/TD]
[TD]2018-07-04[/TD]
[TD="align: right"]-0.03[/TD]
[/TR]
[TR]
[TD]YT6[/TD]
[TD]2018-06-05[/TD]
[TD="align: right"]-0.03[/TD]
[/TR]
</tbody>[/TABLE]

I am trying to find the length of time in days that the customer has been in arrears from the most recent end of period date - i.e. back to where their account goes into credit (if it does) or until their account was opened if it has always been in debt. I'm only interested in the current instance of debt (i.e. it doesn't matter if they have been in arrears before) and would like the data to look like this:
[TABLE="width: 162"]
<colgroup><col><col></colgroup><tbody>[TR]
[TD]Cust No[/TD]
[TD]Days in arrears[/TD]
[/TR]
[TR]
[TD]JH3[/TD]
[TD="align: right"]56[/TD]
[/TR]
[TR]
[TD]RB5[/TD]
[TD="align: right"]56[/TD]
[/TR]
[TR]
[TD]YT6[/TD]
[TD="align: right"]96[/TD]
[/TR]
</tbody>[/TABLE]

The data is exported out and refreshed, so needs to be able to cope with this.
Any help is much appreciated!

Thank you :)

Jenny
 

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. Before anyone can really answer where does 56 come from? Im struggling to see how the first two customers can both produce the same number based on your data and explanation.
 
Upvote 0
try this, revise range to suit


Book1
ABCDEF
1Cust NoEnd of Period DateCarried Forward Balance
2JH307/01/20191032.06JH356
3JH307/12/20181130.82RB556
4JH307/12/20181732.06YT696
5JH307/12/20181132.06
6JH312/11/20181230.82
7RB507/01/20191182.76
8RB507/12/20181082.76
9RB507/12/2018982.76
10RB507/12/201840.14
11RB512/11/2018611.45
12RB503/10/2018-1.17
13RB504/09/2018570.14
14RB514/08/201840.14
15RB504/07/2018-1.17
16YT607/01/20191020.53
17YT607/12/20181020.53
18YT607/12/20181020.53
19YT607/12/2018510.25
20YT612/11/20181020.53
21YT603/10/2018510.25
22YT604/09/2018-0.03
23YT614/08/2018510.25
24YT604/07/2018-0.03
25YT605/06/2018-0.03
Sheet3
Cell Formulas
RangeFormula
F2{=MAXIFS($B$2:$B$25,$A$2:$A$25,E2)-IFERROR(INDEX($B$2:$B$25,MATCH(TRUE,IF($A$2:$A$25=E2,$C$2:$C$25)<0,0)-1),MINIFS($B$2:$B$25,$A$2:$A$25,E2))}
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0
Hi
Thanks for responding so quickly.

The first customer (JH3) has always been in debt, so the days are calculated by 2019-01-07 take away 2018-11-12 is 56 days.

The second customer (RB5) has only been in debt since 2018-11-12. Prior to that, their account was in credit. I just need to know the period they are in debt so, again, 2018-11-12 taken away from 2019-01-07 is 56 days.

Does that make sense?

Thanks :)
Jenny
 
Upvote 0
Hi AlanY,

That is pretty fricking awesome! Thank you very much - works like a charm! It's been driving me bonkers for some time now. I have no idea how that works, but it's great!

Wishing I had your brain...

Jenny
 
Upvote 0
Hi AlanY
I've just spotted an anomaly...which is my fault as I didn't include this in the original data. Where a customer is not in arrears currently (i.e. showing a negative amount on 07/01/2019), the array produces an incorrect result. Is there a way round this?
Thanks again.
Jenny
 
Upvote 0
modified to suit


Book1
ABCDEF
1Cust NoEnd of Period DateCarried Forward Balance
2JH307/01/20191032.06JH356
3JH307/12/20181130.82RB556
4JH307/12/20181732.06YT696
5JH307/12/20181132.06AY1---
6JH312/11/20181230.82
7RB507/01/20191182.76
8RB507/12/20181082.76
9RB507/12/2018982.76
10RB507/12/201840.14
11RB512/11/2018611.45
12RB503/10/2018-1.17
13RB504/09/2018570.14
14RB514/08/201840.14
15RB504/07/2018-1.17
16YT607/01/20191020.53
17YT607/12/20181020.53
18YT607/12/20181020.53
19YT607/12/2018510.25
20YT612/11/20181020.53
21YT603/10/2018510.25
22YT604/09/2018-0.03
23YT614/08/2018510.25
24YT604/07/2018-0.03
25YT605/06/2018-0.03
26AY107/01/2019-1032.06
27AY107/12/2018-1130.82
28AY107/12/20181732.06
Sheet3
Cell Formulas
RangeFormula
F2{=IF(INDEX($C$2:$C$28,MATCH(E2&"|"&MAXIFS($B$2:$B$28,$A$2:$A$28,E2),$A$2:$A$28&"|"&$B$2:$B$28,0))<0,"---",MAXIFS($B$2:$B$28,$A$2:$A$28,E2)-IFERROR(INDEX($B$2:$B$28,MATCH(TRUE,IF($A$2:$A$28=E2,$C$2:$C$28)<0,0)-1),MINIFS($B$2:$B$28,$A$2:$A$28,E2)))}
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0
That's excellent. Thank you so much for your help. I will endeavour to try and understand what it all means!!! Jenny :)
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,304
Members
452,633
Latest member
DougMo

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