Counting Across Tables

leefletcher

New Member
Joined
Mar 22, 2018
Messages
29
Office Version
  1. 2019
Platform
  1. Windows
Hello!

I am trying to count items but they are linked across tables.

Sheet 1 - Has the schools listed by city
CitiesSchools
BostonMaple Elementary
BostonElm Middle School
BostonCedar Elementary
Los AngelesBamboo Elementary
New YorkPine High School
New YorkFicus High School

Sheet 2 - Shows which grade levels are in the school
SchoolsGrades
Woodruff Elementary1st-5th
Cedar Elementary5th-6th
Elm Middle School6th-8th
Ficus High School9th-10th
Landrum High School9th-12th
Northwest Elementaryk-5th

Sheet 3 - Tells me the grade level types that each grade level is assigned
GradesTypes
1st-5the
5th-6thm
6th-8thm
9th-10thh
9th-12thh
k-5the

How do I count the number of "types" in each town? Like this:
emh
Boston
1​
2​
0​
New York
0​
0​
2​
Los Angeles
1​
0​
0​
 
I see. Thank you for clarifying. I think I've fixed it.

Sheet 1 - Has the schools listed by city
CitiesSchools
BostonMaple Elementary
BostonElm Middle School
BostonCedar Elementary
Los AngelesBamboo Elementary
New YorkPine High School
New YorkFicus High School

Sheet 2 - Shows which grade levels are in the school
SchoolsGrades
Maple Elementary1st-5th
Cedar Elementary5th-6th
Elm Middle School6th-8th
Ficus High School9th-10th
Pine High School9th-12th
Northwest Elementaryk-5th

Sheet 3 - Tells me the grade level types that each grade level is assigned
GradesTypes
1st-5the
5th-6thm
6th-8thm
9th-10thh
9th-12thh
k-5the

How do I count the number of "types" in each town? Like this:
emh
Boston210
New York002
Los Angeles100
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
CitiesSchoolsSchoolsGradesGradesTypesCitiesemh
BostonMaple ElementaryMaple Elementary1st-5th1st-5theBoston12
BostonElm Middle SchoolCedar Elementary5th-6th5th-6thmNew York2
BostonCedar ElementaryElm Middle School6th-8th6th-8thm
Los AngelesBamboo ElementaryFicus High School9th-10th9th-10thh
New YorkPine High SchoolPine High School9th-12th9th-12thh
New YorkFicus High SchoolNorthwest Elementaryk-5thk-5the

all your "tables" should be as Excel Tables (Ctrl+T) then doesn't matter where they are
here is the M step by step
Power Query:
// Table1
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content]
in
    Source

Power Query:
// Table2
let
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content]
in
    Source

Power Query:
// Table3
let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content]
in
    Source

Power Query:
// Merge1
let
    Source = Table.NestedJoin(Table1,{"Schools"},Table2,{"Schools"},"Table2",JoinKind.LeftOuter),
    Expand = Table.ExpandTableColumn(Source, "Table2", {"Grades"}, {"Grades"})
in
    Expand

Power Query:
// Merge2
let
    Source = Table.NestedJoin(Merge1,{"Grades"},Table3,{"Grades"},"Table3",JoinKind.LeftOuter),
    Expand = Table.ExpandTableColumn(Source, "Table3", {"Types"}, {"Types"}),
    Group = Table.Group(Expand, {"Cities", "Types"}, {{"Count", each Table.RowCount(_), type number}}),
    Filter = Table.SelectRows(Group, each ([Types] <> null)),
    Pivot = Table.Pivot(Filter, List.Distinct(Filter[Types]), "Types", "Count", List.Sum)
in
    Pivot
 
Last edited:
Upvote 0
Thank you. I'm too much of a "baby user" to know how to do anything with Power Qwerys, but I will look into it. I thought it might be too complicated to do with formulas.
 
Upvote 0
It looks complicated but it isn't, just merge tables by suitable columns, that's all :cool:
I suggest to learn Power Query from here
almost everything is there (y)

but if you want formula solution maybe someone else will do that for you

Have a nice day :)
 
Upvote 0
@sandy666 has given you a great solution. PowerQuery should be much more efficient than any formula solution. Nevertheless, here's a formula solution. For simplicity, though, I have assumed that all of the relevant tables are located on the same worksheet, starting at cell A1. You'll need to amend the references accordingly.

Enter the following formula in cell B30, confirm with CONTROL+SHIFT+ENTER, and copy across and down:

=SUM(IF($A$3:$A$8=$A30,IF(ISNUMBER(MATCH($B$3:$B$8,IF(ISNUMBER(MATCH($B$12:$B$17,IF($B$21:$B$26=B$29,$A$21:$A$26),0)),$A$12:$A$17),0)),1)))

Sheet 1 - Has the schools listed by city
CitiesSchools
BostonMaple Elementary
BostonElm Middle School
BostonCedar Elementary
Los AngelesBamboo Elementary
New YorkPine High School
New YorkFicus High School
Sheet 2 - Shows which grade levels are in the school
SchoolsGrades
Maple Elementary1st-5th
Cedar Elementary5th-6th
Elm Middle School6th-8th
Ficus High School9th-10th
Pine High School9th-12th
Northwest Elementaryk-5th
Sheet 3 - Tells me the grade level types that each grade level is assigned
GradesTypes
1st-5the
5th-6thm
6th-8thm
9th-10thh
9th-12thh
k-5the
How do I count the number of "types" in each town? Like this:
emh
Boston120
New York002
Los Angeles000

Hope this helps!
 
Upvote 0
@sandy666 has given you a great solution. PowerQuery should be much more efficient than any formula solution. Nevertheless, here's a formula solution. For simplicity, though, I have assumed that all of the relevant tables are located on the same worksheet, starting at cell A1. You'll need to amend the references accordingly.

Enter the following formula in cell B30, confirm with CONTROL+SHIFT+ENTER, and copy across and down:

=SUM(IF($A$3:$A$8=$A30,IF(ISNUMBER(MATCH($B$3:$B$8,IF(ISNUMBER(MATCH($B$12:$B$17,IF($B$21:$B$26=B$29,$A$21:$A$26),0)),$A$12:$A$17),0)),1)))

Sheet 1 - Has the schools listed by city
CitiesSchools
BostonMaple Elementary
BostonElm Middle School
BostonCedar Elementary
Los AngelesBamboo Elementary
New YorkPine High School
New YorkFicus High School
Sheet 2 - Shows which grade levels are in the school
SchoolsGrades
Maple Elementary1st-5th
Cedar Elementary5th-6th
Elm Middle School6th-8th
Ficus High School9th-10th
Pine High School9th-12th
Northwest Elementaryk-5th
Sheet 3 - Tells me the grade level types that each grade level is assigned
GradesTypes
1st-5the
5th-6thm
6th-8thm
9th-10thh
9th-12thh
k-5the
How do I count the number of "types" in each town? Like this:
emh
Boston120
New York002
Los Angeles000

Hope this helps!
Thank you. Do you see any problems with this working since the tables are on different worksheets?
 
Upvote 0
Actually, since you have the latest version of Excel, you should have access to dynamic array formulas, which are usually more efficient than regular array formulas. Therefore, you could try the following formula instead...

B30, confirmed with just ENTER, and copy across and down:

=IFERROR(ROWS(FILTER($B$3:$B$8,($A$3:$A$8=$A30)*(ISNUMBER(MATCH($B$3:$B$8,FILTER($A$12:$A$17,ISNUMBER(MATCH($B$12:$B$17,FILTER($A$21:$A$26,($B$21:$B$26=B$29)),0))),0))))),0)

Hope this helps!
 
Last edited:
Upvote 0
Actually, since you have the latest version of Excel, you should have access to dynamic array formulas, which are usually more efficient than regular array formulas. Therefore, you could try the following formula instead...

B30, confirmed with just ENTER, and copy across and down:

=IFERROR(ROWS(FILTER($B$3:$B$8,($A$3:$A$8=$A30)*(ISNUMBER(MATCH($B$3:$B$8,FILTER($A$12:$A$17,ISNUMBER(MATCH($B$12:$B$17,FILTER($A$21:$A$26,($B$21:$B$26=B$29)),0))),0))))),0)

Hope this helps!
Thank you. The other one worked, but when I try this I get the "That function isn't valid" error and it highlights "Filter". We are using Microsoft Office Professional Plus 2019 (as of Monday, lol).

There are duplicate school names in my much larger data sheet (B3:B8, A12:A17), but they are in different districts. If I add districts into a C column, could I use ((B3:B8)*(C3:C8)) and ((A12:A17)*(C12:C17))?
 
Upvote 0
Just checked, and apparently the Filter function is only available in Office 365. So stick with the first one. :)
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,203
Members
452,617
Latest member
Narendra Babu D

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top