首页 > 解决方案 > 第 1 列的最大值,其中第 2 列的值匹配某些条件

问题描述

假设我在表格中有以下内容:

     A   |  B  |  desired_output
   ----------------------------
1 |  10  |  1  |  0
2 |  20  |  7  |  0
3 |  30  |  3  |  0
4 |  20  |  2  |  0
5 |  30  |  5  |  1

我想为desired_output列中的每个单元格找到一个公式,该公式查看 B1:B5 的最大值,但仅适用于 A = max(A1:A5) 的行

如果不清楚,我会尝试换一种说法:

for all the rows in A1:A5 that are equal to max(A1:A5) // so that's rows 3 and 5
    find the one which has the max value on B // so between B3 and B5, that's B5
        output 1 for this one, 0 for the other

如果存在这样的功能,我会说会有一个where地方,就像= if(B=(max(B1:B5) where A = max(A1:A5)), 1, 0)但我找不到如何去做......


我可以用一个技巧在两列中做到这一点:

     A   |  B  |  C  | D
   ----------------------------
1 |  10  |  1  |     |  0
2 |  20  |  7  |     |  0
3 |  30  |  3  |  3  |  0
4 |  20  |  2  |     |  0
5 |  30  |  5  |  5  |  1

Cn = if(An=max(A$1:A$5),Bn,"")和_Dn = if(Cn = max(C$1:C$5), 1, 0)

但我仍然无法在一栏中找到如何做到这一点

标签: google-sheets

解决方案


=ARRAYFORMULA(IF(LEN(A1:A), IF(IFERROR(VLOOKUP(CONCAT(A1:A&"×", B1:B),
 JOIN("×", QUERY(A1:B, "order by A desc, B desc limit 1")), 1, 0), )<>"", 1, 0), ))

0

或更短:

=ARRAYFORMULA(IF(A:A<>"",N(A:A&"×"&B:B=JOIN("×",SORTN(A:B,1,,1,0,2,0))),)) 

=ARRAYFORMULA(IF(A:A<>"",N(A:A&B:B=JOIN(,SORTN(A:B,1,,1,0,2,0))),))

推荐阅读