Forum Discussion
Formula to count a row, reset if 3 values found within last 24 days, display count since reset
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | Since Reset |
| F | F | F | F | DO | DO | F | F | F | DO | F | F | DO | F | F | F | 3 |
So using the basis of the table above, I am trying to come up with a formula to calculate total days worked since 3 "DO" occurred in last 24 day period crossing month dates, I have the months on seperate tabs for the year, labeled as the month January, February, March, etc.
I've used CountA(A2:AG2) to get cells not empty,
CountBlank(A2:AG2) to get empty cells,
CountIf(A2:AG2,"DO") to get number of DO in row
I'm sure it's a simple solution that is just eluding me.
8 Replies
- m_tarlerBronze Contributor
Here is a more complicated solution that has multiple tabs and windows across those tabs.
for this example I made the "window" 5 days to see it easier. The red highlight the 3-5day window that caused the reset and the green is the days since and 2 of the examples go back to prior tabs.
=LET(all_data,HSTACK(January:December!A1:AG10), window, 5, tday, DAYS(TODAY(),DATE(YEAR(TODAY()),1,1)), data, TAKE(DROP(FILTER(all_data,TAKE(all_data,1)<>""),1),,tday), seqA, SEQUENCE(tday), seqB, SEQUENCE(,tday-window+1), winMatrix, (seqA >= seqB)*(seqA <= (window-1 + seqB)), results, (MMULT(--(data="DO"),winMatrix)>=3)*DROP(data="DO",,window-1)*(window-1 + seqB), out,BYROW(results, LAMBDA(r, tday-MAX(r))), out)to explain:
row 1 collects all the data across all the tabs (assumes you have January - December tabs)
row 2 is the 'window' size to look for the "DO". you will set this back to 24.
row 3 is the 'test' date to use. In this case I used today which is 86 day of the year
row 4 takes all the data from row 1 and removed the empty columns (e.g. FEB should only have 1-28 or 1-29 listed in row 1 so will remove the empty columns coming from that tab) and then removes that top line of day numbers and then only takes the columns up to the 'test' date (tday)
rows 5,6,7 just create the sliding window
row 8 does the matix multiplication to find the locations where a "DO" causes a reset and sets those places = to the day of the year
row 9 goes row by row and returns the days since the last reset event
row 10 spits out row 9
- Patrick2788Silver Contributor
This is a solution for Excel 365:
=LET( TotalDays, LAMBDA(each_row, LET( k, COUNTIF(each_row, "DO"), split, TEXTAFTER(CONCAT(each_row), "DO", -1, , , ""), IF(k >= 3, LEN(split), "") ) ), BYROW(grid, TotalDays) )- GrendalanCopper Contributor
Hey Patrick,
Thank you for the quick response, I tried to implement your formula and sometimes it is correct, others it is off, so not sure where it is going wrong?
I notice that if I go through and change values to change the count then start from 0 days off then go back and change values it seems to count correctly after that
- Patrick2788Silver Contributor
Do you have a sample workbook you're able to share?