用边条件计算不同的值

时间:2017-08-19 13:58:04

标签: excel excel-formula

我在excel中有一个名为tableName的表格,如下所示:

ColumnA ColumnB
 1111    aaaa
 1111    bbbb
 1111    aaaa
 1111    cccc
 1111    aaaa
 1111    aaaa
 1111    aaaa
 2222    aaaa
 2222    aaaa
 2222    ffff
 2222    aaaa
 2222    aaaa
 3333    bbbb
 3333    bbbb
 4444    aaaa
 4444    bbbb

我已经得到了这个(在单元格B2中):

{=SUM(IF((tableName[ColumnB]=B$1)*1;1/COUNTIFS(tableName[ColumnA];tableName[ColumnA];tableName[ColumnB];B$1)))}

单元格aaaa中的B1用于计算ColumnA ColumnBaaaa包含ColumnA ColumnB 1111 aaaa <----is counted lets call it 1 1111 bbbb <--------not counted because ColumnB isn't aaaa 1111 aaaa <----not counted because duplicate of 1 1111 cccc <--------not counted because ColumnB isn't aaaa 1111 aaaa <----not counted because duplicate of 1 1111 aaaa <----not counted because duplicate of 1 1111 aaaa <----not counted because duplicate of 1 2222 aaaa <----is counted lets call it 2 2222 aaaa <----not counted because duplicate of 2 2222 ffff <--------not counted because ColumnB isn't aaaa 2222 aaaa <----not counted because duplicate of 2 2222 aaaa <----not counted because duplicate of 2 3333 bbbb <--------not counted because ColumnB isn't aaaa 3333 bbbb <--------not counted because ColumnB isn't aaaa 4444 aaaa <----is counted lets call it 3 4444 bbbb <--------not counted because ColumnB isn't aaaa 的不同值。这里的结果是3(1111和2222和4444),标记的列被计算在内:

bbbb

现在我在单元格A2中有ColumnA并希望计算相同数量,但A2和{{1}中必须有另一行具有相同的值ColumnB中的(= bbbb)。

ColumnA ColumnB
 1111    aaaa      <----is counted because x exists
 1111    bbbb      <--------this is x
 1111    aaaa
 1111    cccc
 1111    aaaa
 1111    aaaa
 1111    aaaa
 2222    aaaa      <----not counted because no pair (2222, bbbb) exists
 2222    aaaa
 2222    ffff
 2222    aaaa
 2222    aaaa
 3333    bbbb
 3333    bbbb
 4444    aaaa      <----is counted because y exists
 4444    bbbb      <--------this is y

我不知道该怎么做。

我的完整目标是创建一个列联表:

     aaaa bbbb cccc ffff
aaaa  3    2    1    1    
bbbb  2    3    1    0
cccc  1    1    1    0
ffff  1    0    0    1

1 个答案:

答案 0 :(得分:3)

试试这个:

=SUMPRODUCT((TableName[ColumnB]=B1)*  
  (0<COUNTIFS(TableName[ColumnA],TableName[ColumnA],TableName[ColumnB],A2))
/COUNTIFS(TableName[ColumnA],TableName[ColumnA],TableName[ColumnB],TableName[ColumnB]))

P.S。它不是 CSE 公式,不需要Ctrl + Shift + Enter,虽然它也能正常工作。

enter image description here