jeudi 9 avril 2015

Perform a function based on an unknown number of IF statements

I need help with the following scenario:

Column A contains an encounter in a department.

Column B contains a date of first administration.

Column C contains a date of last administration.


In general, I want to subtract column B from C for a given A. Straightforward. However, column A has duplicates. If column A has a duplicate (meaning the same encounter in the same department), then I want to subtract B1 from C2 (i.e the last administration in the same A from the first) IF B2>=C1, and generate a zero in D1. If not (i.e the dates do not overlap), then subtract B1 from C1 and B2 from C2. Also not too bad.


However, let's say Column A has three duplicates and C1 overlaps with B2 and C2 overlaps with B3. Then I want to subtract B1 from C3, and generate zeros in D1 and D2.


Column A can have multiple duplicates with multiple overlapping dates (let's say not infinite but up to five), though they don't always overlap.


I'm pretty sure this involves nested IFs (a lot of them), but I'm not getting the right numbers. My last attempt looked something like:



=IF(AND(E131<>E130,E131<>E132),(M131-L131)+1,IF(AND(E131=E126,P131=P126,L131<=M126),(M131-L126)+1,IF(AND(E131=E127,P131=P127,L131<=M127),(M131-L127)+1,IF(AND(E131=E128,P131=P128,L131<=M128),(M131-L128)+1,IF(AND(E131=E129,P131=P129,L131<=M129),(M131-L129)+1,IF(AND(E131=E130,P131=P130,L131<=M130),(M131-L130)+1,IF(AND(E131=E136,P131=P136,M131>=L136),(M136-L131)+1,IF(AND(E131=E135,P131=P135,M131>=L135),(M135-L131)+1,IF(AND(E131=E134,P131=P134,M131>=L134),(L134-M131)+1,IF(AND(E131=E133,P131=P133,M131>=L133),(L133-M131)+1,IF(AND(E131=E132,P131=P132,M131>=L132),(L132-M131)+1,(M131-L131)+1)))))))))))



But I know that is wrong, because it is not telling the cells that are in the middle of the duplicate chain to be zero. That involves something like:



=IF(AND(F131<>F130,F131<>F132),(N131-M131)+1,IF(AND(F131=F130,F131<>F132,N130>=M131),N131-M130,IF(AND(F131=F130,F131=F132,N130>=M131,N131>=M132),0,(N132-M130)+1)))



I have a feeling this requires a loop function or something I don't know how to do.


Aucun commentaire:

Enregistrer un commentaire