Spreadsheet: 766162d1643787268-counting-days-of-absence-for-each-section-sample-data-5-ans
Sheet: sample data

Lp | ID | Name | absence From | absence To |  | Section ID | absence days | days to absence in section ID | days to subtract from section ID |  | days to subtract from section ID | days to add to section ID |  |  | ID | Section ID | Name | From | To | Section
1 | 215321 | [NAME] | 2019-06-24 00:00:00 | 2019-09-23 00:00:00 |  |  |  |  |  |  |  |  |  |  | 215321 | 50050916 | [NAME] | 2018-05-01 00:00:00 | 2019-09-22 00:00:00 | Financial section
2 | 215321 | [NAME] | 2019-12-11 00:00:00 | 2019-12-13 00:00:00 |  |  |  |  |  |  |  |  |  |  | 215321 | 50050922 | [NAME] | 2019-09-23 00:00:00 | 2022-01-18 00:00:00 | Accounting section
3 | 215321 | [NAME] | 2020-04-01 00:00:00 | 2020-04-16 00:00:00 |  |  |  |  |  |  |  |  |  |  | 215321 | 50050916 | [NAME] | 2022-01-19 00:00:00 | 9999-12-31 00:00:00 | Financial section
4 | 215321 | [NAME] | 2020-04-17 00:00:00 | 2020-05-11 00:00:00 |  |  |  |  |  |  |  |  |  |  | TABLE 1
5 | 215321 | [NAME] | 2020-05-12 00:00:00 | 2020-06-03 00:00:00
6 | 215321 | [NAME] | 2020-06-04 00:00:00 | 2020-06-24 00:00:00
7 | 215321 | [NAME] | 2020-06-25 00:00:00 | 2020-07-15 00:00:00
8 | 215321 | [NAME] | 2020-07-16 00:00:00 | 2020-07-29 00:00:00
9 | 215321 | [NAME] | 2020-07-30 00:00:00 | 2020-08-28 00:00:00
10 | 215321 | [NAME] | 2022-01-03 00:00:00 | 2022-01-10 00:00:00
11 | 215321 | [NAME] | 2022-01-11 00:00:00 | 2022-01-17 00:00:00
12 | 215321 | [NAME] | 2022-01-18 00:00:00 | 2022-01-21 00:00:00 |  |  |  |  |  |  |  |  | <-- wrong
13 | 215321 | [NAME] | 2022-01-28 00:00:00 | 2022-02-04 00:00:00 |  |  |  |  |  |  |  |  | <----- wrong should be taken 3rd position from table 1 (19.01.2022 to 31.12.9999) instead of first position (01.05.2018 to 22.09.2019) Because absence was from 28.01.2022 to 04.02.2022 which means that is Financtial section (19.01.2022 to 31.12.9999)
14 | 215321 | [NAME] | 2019-11-29 00:00:00 | 2019-11-29 00:00:00
15 | 215321 | [NAME] | 2019-07-03 00:00:00 | 2019-07-05 00:00:00
16 | 215321 | [NAME] | 2019-10-21 00:00:00 | 2019-10-25 00:00:00
17 | 215321 | [NAME] | 2019-12-17 00:00:00 | 2019-12-20 00:00:00
18 | 215321 | [NAME] | 2022-01-24 00:00:00 | 2022-01-27 00:00:00 |  |  |  |  |  |  |  |  | <-- wrong
19 | 215321 | [NAME] | 2019-12-23 00:00:00 | 2019-12-24 00:00:00
20 | 215321 | [NAME] | 2020-03-04 00:00:00 | 2020-03-05 00:00:00
21 | 215321 | [NAME] | 2021-12-30 00:00:00 | 2021-12-31 00:00:00
22 | 215321 | [NAME] | 2019-08-19 00:00:00 | 2019-09-01 00:00:00
23 | 215321 | [NAME] | 2021-01-16 00:00:00 | 2021-08-27 00:00:00
24 | 215321 | [NAME] | 2021-08-30 00:00:00 | 2021-09-26 00:00:00
25 | 215321 | [NAME] | 2021-09-27 00:00:00 | 2021-10-23 00:00:00
26 | 215321 | [NAME] | 2020-08-29 00:00:00 | 2021-01-15 00:00:00
27 | 215321 | [NAME] | 2019-07-08 00:00:00 | 2019-08-18 00:00:00
28 | 215321 | [NAME] | 2019-01-17 00:00:00 | 2019-01-17 00:00:00
29 | 215321 | [NAME] | 2019-02-14 00:00:00 | 2019-02-14 00:00:00
30 | 215321 | [NAME] | 2019-02-22 00:00:00 | 2019-02-22 00:00:00
31 | 215321 | [NAME] | 2019-03-06 00:00:00 | 2019-03-06 00:00:00
32 | 215321 | [NAME] | 2019-03-15 00:00:00 | 2019-03-15 00:00:00
33 | 215321 | [NAME] | 2019-05-02 00:00:00 | 2019-05-02 00:00:00
34 | 215321 | [NAME] | 2019-09-02 00:00:00 | 2019-10-01 00:00:00 |  |  |  |  |  |  |  |  | <---- its ok because absence from 02.09 to 01.10 which means during this time he was in Financial section untill 22.09 and 9 days in accounting section which need to be substracted from Financial section
35 | 215321 | [NAME] | 2019-12-06 00:00:00 | 2019-12-06 00:00:00
36 | 215321 | [NAME] | 2019-12-07 00:00:00 | 2019-12-07 00:00:00
37 | 215321 | [NAME] | 2020-02-06 00:00:00 | 2020-02-06 00:00:00
38 | 215321 | [NAME] | 2020-02-13 00:00:00 | 2020-02-14 00:00:00
39 | 215321 | [NAME] | 2020-02-27 00:00:00 | 2020-02-28 00:00:00
40 | 215321 | [NAME] | 2020-03-06 00:00:00 | 2020-03-06 00:00:00
41 | 215321 | [NAME] | 2020-03-10 00:00:00 | 2020-03-10 00:00:00
42 | 215321 | [NAME] | 2020-03-16 00:00:00 | 2020-03-27 00:00:00
43 | 215321 | [NAME] | 2020-03-30 00:00:00 | 2020-03-31 00:00:00
44 | 215321 | [NAME] | 2021-10-25 00:00:00 | 2021-12-29 00:00:00
45 | 215321 | [NAME] | 2019-12-31 00:00:00 | 2019-12-31 00:00:00
46 | 215321 | [NAME] | 2020-01-27 00:00:00 | 2020-01-27 00:00:00
47 | 215321 | [NAME] | 2020-01-28 00:00:00 | 2020-01-28 00:00:00