How to count from an looked up range in excel

2019-09-01 05:03发布

I have a pivot table that has a row for a set of cities. I want to count the number of cities in a certain state. It's not wanted to include the state in the pivot table (although that would make things simpler).

I tried using the following formula:

={countif(index(city_ref[state], match(G15:G30,city_ref[city_name],0)),"=CA")}

Where:

  • city_ref is a table that has at least the columns [city_name] and [state]

  • G15:G30 are the rows of the pivot table

The data looks like this (simplified):

city_ref:

city_name   | state
====================
Los Angeles | CA
Seattle     | WA
Portland    | OR
etc.

G15:G30:

     city_name
     ===========
G15: Los Angeles
G16: Seattle
     etc. (but not all of city_ref)

I do realize that you could include the state in a pivot table but a) that's not wanted (even hiding it) and b) there is other, dis-related data that when the state is included the pivot table makes additional rows which messes up the count.

The index match lookup seems to return an array if used on its own; if I use the fill handle it does create an array of cells with the lookup values between G15:G30. However including this in countif does not seem to do anything.

In the help page for countif that I have in excel (unfortunately I cannot find an online reference to link to) it says that range (the first argument) can be an array.

I want to be able to take an arbitrary array of values, look them up in another table and count the ones that match my criteria.

标签: excel countif
2条回答
贪生不怕死
2楼-- · 2019-09-01 05:13

Something like this ?

enter image description here

{=SUM((IFERROR(MATCH($D$2:$D$7,INDEX(IF($B$2:$B$13=$F$2,$A$2:$A$13),),0),0)>0)*1)}

查看更多
女痞
3楼-- · 2019-09-01 05:18

Why don't you just make a copy of the PivotTable, add a Slicer so users can filter it on a particular State, and add the State field to the Values area so that it gets counted? Seems counter productive to point a formula at PivotTable 1 when you could simply have PivotTable2 do the sum by State.

查看更多
登录 后发表回答