英文:
Calculate current streak in Excel Row with Conditions
问题
=MAX(FREQUENCY(IF((D2:BH2=0)*(D2:BH2<>IF(D2=0,D2,1))*(D2:BH2<>IF(INDEX(D2:BH2,1,COLUMNS(D2:BH2))=0,INDEX(D2:BH2,1,COLUMNS(D2:BH2)),1)),COLUMN(D2:BH2)), IF((D2:BH2=0)*(D2:BH2<>IF(D2=0,D2,1))*(D2:BH2<>IF(INDEX(D2:BH2,1,COLUMNS(D2:BH2))=0,INDEX(D2:BH2,1,COLUMNS(D2:BH2)),1)),0,COLUMN(D2:BH2))))
英文:
I have a list of numbers (ex. 5, 7, 10, 11, etc.) and 0s in excel rows from D2:BH2
, and I want to calculate the longest streak of 0s in each row (with 2 conditions).
The first condition is to ignore streaks that start the row with 0
Ex. (0 0 0 1 5 6 0 0 1
) -> this would have a longest streak of 2
instead of 3
due to the first condition
Ex. ('1 0 0 0 1 5 6 0 0 1') -> this would have a longest streak of '3'
The second condition is to ignore streaks that end the row with 0
Ex. (0 1 5 6 0 0 1 0 0 0 0
) -> this would have a longest streak of 2
instead of 4
due to the second condition.
Ex. (' 0 1 5 6 0 0 1 0 0 0 0 1') -> this would have a longest streak of '4'
Is there a simple way of calculating the streak of 0s based on these two conditions for each row (in one cell formula)?
Currently I'm using a formula:
=MAX(FREQUENCY(IF(D2:BH2=0,COLUMN(D2:BH2)),IF(D2:BH2=0,0,COLUMN(D2:BH2)
This calculates the longest streak; however, does not take into account the two conditions.
答案1
得分: 3
根据此答案中的方法,我们可以仅修剪前导/尾随空格,您可以尝试:
在 L1
中的公式:
=BYROW(A1:J4, LAMBDA(a, LET(x, CONCAT(SIGN(a)), y, TEXTSPLIT(x, 0,, 1), IFERROR(MAX(LEN(DROP(DROP(TEXTSPLIT(0&x&0,,y), 1), -1))), 0)))
英文:
Based on this answer where we could trim only leading/trailing spaces, you could try:
Formula in L1
:
=BYROW(A1:J4,LAMBDA(a,LET(x,CONCAT(SIGN(a)),y,TEXTSPLIT(x,0,,1),IFERROR(MAX(LEN(DROP(DROP(TEXTSPLIT(0&x&0,,y),1),-1))),0))))
答案2
得分: 2
这似乎也适用于稍微修改频率公式,以便将所有前导和尾随零的计数汇总到频率数组的第一个和最后一个单元格,然后删除这些单元格:
=MAX(DROP(DROP(FREQUENCY(IF(A1:J1=0,COLUMN(A1:J1)),IF(A1:J1<>0,COLUMN(A1:J1))),1),-1))
如果所有单元格都是零或仅有一个非零值,它将出现错误,但正确的答案应为零,因此更新的公式应为:
=IFERROR(MAX(DROP(DROP(FREQUENCY(IF(A1:J1=0,COLUMN(A1:J1)),IF(A1:J1<>0,COLUMN(A1:J1))),1),-1),0)
当然,您也可以通过行进行操作:
=BYROW(A1:J6,LAMBDA(r,IFERROR(MAX(DROP(DROP(FREQUENCY(IF(r=0,COLUMN(r)),IF(r<>0,COLUMN(r))),1),-1),0)))
英文:
This also seems to work if you modify the frequency formula slightly so that the counts of all leading and trailing zeroes are gathered into the first and last cells of the Frequency array then drop those cells:
=MAX(DROP(DROP(FREQUENCY(IF(A1:J1=0,COLUMN(A1:J1)),IF(A1:J1<>0,COLUMN(A1:J1))),1),-1))
If you have all zeroes or only one non-zero value it will error but the correct answer should be zero so updated formula should be:
=IFERROR(MAX(DROP(DROP(FREQUENCY(IF(A1:J1=0,COLUMN(A1:J1)),IF(A1:J1<>0,COLUMN(A1:J1))),1),-1)),0)
Of course you can byrow it:
=BYROW(A1:J6,LAMBDA(r,IFERROR(MAX(DROP(DROP(FREQUENCY(IF(r=0,COLUMN(r)),IF(r<>0,COLUMN(r))),1),-1)),0)))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论