Formula to summarize data in 2 tables

%3CLINGO-SUB%20id%3D%22lingo-sub-1505503%22%20slang%3D%22en-US%22%3EFormula%20to%20summarize%20data%20in%202%20tables%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1505503%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20a%20table%20with%202%20columns%2C%20city%20name%20and%20sales%20amount.%20There%20is%20another%20table%20with%202%20columns%20city%20name%20and%20country%20name.%26nbsp%3BBoth%20tables%20are%20added%20to%20the%20data%20model%20and%20a%20relationship%20is%20connected%20for%20city%20name%20in%20both%20tables.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20want%20to%20write%20a%20formula%20to%20find%20out%20total%20sales%20in%20one%20specific%20country.%20How%20do%20I%20do%20this%3F%26nbsp%3BSolution%20can%20be%20with%20or%20without%20using%20data%20model.%20Just%20need%20the%20formula%20result%20to%20come%20correctly.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1505503%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
New Contributor

I have a table with 2 columns, city name and sales amount. There is another table with 2 columns city name and country name. Both tables are added to the data model and a relationship is connected for city name in both tables.

 

I want to write a formula to find out total sales in one specific country. How do I do this? Solution can be with or without using data model. Just need the formula result to come correctly.

8 Replies

@yusufalikk 

Do you consider PivotTable as an option?

@Sergei Baklan 

Not Pivot Table. I need a formula...

@yusufalikk 

 

There is a potential problem with the kind of relationship you describe, in that cities in different countries sometimes share names. That's definitely true in the United States, where many states have a city named "Springfield," for example.

 

That aside, the less elegant way to do this would be to

  1. use VLOOKUP to get the sales for each city attached to the City and Country on your second table.
  2. then SUMIF to get the totals for each country

 

It's far less elegant than the Pivot Table, but it would get the answers.

@yusufalikk 

For such sample

image.png

list of countries is

=UNIQUE(tblCountry[Country/region])

and Sales per country

=SUM(SUMIFS(tblSales[Sales],tblSales[City],FILTER(tblCountry[City],tblCountry[Country/region]=J3)))

 

@yusufalikk 

Just in vase, if dynamic arrays are not available

image.png

to select the country

=IFERROR(INDEX(tblCountry[Country/region],
     AGGREGATE(15,6,
        1/(COUNTIFS($M$2:$M2,tblCountry[Country/region])=0)*
        (ROW(tblCountry[Country/region])-ROW(tblCountry[[#Headers],[City]])),
        1)),"")

Sales for it

=SUMPRODUCT(
     SUMIFS(tblSales[Sales],tblSales[City],tblSales[City])/
     COUNTIFS(tblSales[City],tblSales[City])*
     (INDEX(tblCountry[Country/region],MATCH(tblSales[City],tblCountry[City],0))=M3)
)

and drag both cells down till empty one appear.

@Sergei Baklan 

 

Thanks Sergei Baklan.

UNIQUE and FILTER are only in Office 365. Am using Office 2016. Not getting the exact results like you do. 

@yusufalikk 

Formula with SUMPRODUCT, you use it as array one, did you try it in your environment as regular (non-array) formula?

I tried both ways... Didn't get correct result...