简体   繁体   中英

How to count from an looked up range in excel

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.

Something like this ?

在此处输入图片说明

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM