在Excel行中根据条件计算当前连续周期

huangapple go评论53阅读模式
英文:

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

根据此答案中的方法,我们可以仅修剪前导/尾随空格,您可以尝试:

在Excel行中根据条件计算当前连续周期

L1 中的公式:

=BYROW(A1:J4, LAMBDA(a, LET(x, CONCAT(SIGN(a)), y, TEXTSPLIT(x, 0,, 1), IFERROR(MAX(LEN(DROP(DROP(TEXTSPLIT(0&amp;x&amp;0,,y), 1), -1))), 0)))
英文:

Based on this answer where we could trim only leading/trailing spaces, you could try:

在Excel行中根据条件计算当前连续周期

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&amp;x&amp;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&lt;&gt;0,COLUMN(A1:J1))),1),-1))

在Excel行中根据条件计算当前连续周期

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&lt;&gt;0,COLUMN(A1:J1))),1),-1)),0)

在Excel行中根据条件计算当前连续周期

Of course you can byrow it:

=BYROW(A1:J6,LAMBDA(r,IFERROR(MAX(DROP(DROP(FREQUENCY(IF(r=0,COLUMN(r)),IF(r&lt;&gt;0,COLUMN(r))),1),-1)),0)))

huangapple
  • 本文由 发表于 2023年2月16日 01:55:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75463705.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定