Excel : Automate COUNTIF (based on another table) - excel

I have a table that looks like this:
name nation
aaa ESP
bbb FRA
ccc ESP
ddd BEL
eee FRA
fff ITA
I have another table with :
country count
ESP -
BEL -
FRA -
ITA -
I would like my "count" column of the second table to count how many instances I have of that nation in the first table.
So that I get :
country count
ESP 2
BEL 1
FRA 2
ITA 1
I could enter in each cell of the "count" column:
COUNTIF('my 1st table nation column',"name of the targeted country")
The thing is I have 150 countries and I can't possibly edit all the country codes in each and every cell of the "count" column.
I also tried this workaround:
I selected the first cell of "count" and then highlighted the whole column. Then I typed:
COUNTIF('my 1st table nation column',"name of the country of the first country cell")
This way, I expected each cell of the "count" column to refer to the name of the "country" of its table row.
But it didn't work.
Is there any other way (with a formula, not VB) I can automate the process so that each "count" cell reads its "country" and swipes through the "nation" column and count how many instances there are ?
Thank you.

=COUNTIF($B$2:$B$7,"="&D2)
name
nation
country
count
aaa
ESP
ESP
2
bbb
FRA
BEL
1
ccc
ESP
FRA
2
ddd
BEL
ITA
1
eee
FRA
fff
ITA

Use a Pivot Table, field nation into rows section and field name into values section.
That way you can make a list of unique countries and count how many instances of each you got in like 3 seconds....
Create a PivotTable to analyze worksheet
data
No need of VBA.

Related

Sum distinct values in a column based one criteria in another column in excel

i have a df which includes the columns "country" and "label" and i want to sum the distinct values in the "label" column based on the values in the column "country". The desired output is shown in the column " final output". I have tried the countifs with 2 criteria but this just count the distinct the values . I also tried this SUM(IF(FREQUENCY(IF($A$2:$A$12=D2,MATCH($B$2:$B$12,$B$2:$B$12,0)),ROW($B$2:$B$12)-ROW($B$2)+1),$B$2:$B$12)) but it didnt work. I can do it with pivot but i prefer to do to it with formulas. Can anyone help? thank you
country
label
Final Output
Scotland
AAA
2
Scotland
AAA
2
Scotland
BBB
2
Spain
AAA
1
Spain
AAA
1
France
BBB
3
France
AAA
3
France
CCC
3
With ms365, you could try:
Formula in C2:
=MAP(A2:A9,LAMBDA(x,ROWS(UNIQUE(FILTER(A2:B9,A2:A9=x)))))
What about this solution?
You count for every code how much times it appears for that country, and you take the inverse of it.
Like this, when you add it, you get the amount of distinct values, as you can easily see here:
Criterium
Value
Count
1/count
Sum of 1/count for this criterium
crit
A
5
1/5
1/5+1/5+1/5+1/5+1/5+1/3+1/3+1/3+1 = 3
crit
A
5
1/5
crit
A
5
1/5
crit
A
5
1/5
crit
A
5
1/5
crit
B
3
1/3
crit
B
3
1/3
crit
B
3
1/3
crit
C
1
1
Hence, you get the following screenshot:
Used formulae:
cell "C2" : =1/COUNTIFS(A$2:A$9,A2,B$2:B$9,B2)
cell "D2" : =SUMIF(A$2:A$9,A2,C$2:C$9)
(I bet there's a way to simplify this, but I haven't enough coffee yet to find it :-) )
Have fun
Count Unique
=LET(Countries,A2:A9,Labels,B2:B9,
uCountries,UNIQUE(Countries),
uFinals,BYROW(uCountries,LAMBDA(uCountry,
ROWS(UNIQUE(FILTER(Labels,Countries=uCountry))))),
INDEX(uFinals,XMATCH(Countries,uCountries)))

Multiple Unique Lists from Master List in Excel

I have a list of companies with multiple contacts for each company, but I would like to create separate lists with 1 unique contact per company. E.g.
Original list
Name Company Email
John AAA john#aaa.com
David AAA david#aaa.com
Jane BBB jane#bbb.com
Julia CCC julia#ccc.com
Craigh CCC craig#ccc.com
John CCC john#ccc.com
In this case, to have 1 unique record from each company in a separate list, I want to end up with 3 separate lists:
List 1
Name Company Email
John AAA john#aaa.com
Jane BBB jane#bbb.com
Julia CCC julia#ccc.com
List 2
Name Company Email
David AAA david#aaa.com
Craigh CCC craig#ccc.com
List 3
Name Company Email
John CCC john#ccc.com
As you can see in each list there is only 1 record for each company.
Any help on how to do this would be most appreciated.
I have tried advance filter to list unique records only by selecting the entire original list as the range and setting the company column as the criteria see the unique records which I could then select visible cells and cut to a different list, and then re-run the filter again to create the next set of unique records etc...but the advance filter doesn't give the results as expected.
-
-
This might be a silly idea but still worth trying... Add a helper column with Count formula:
=COUNTIF($B$1:B2,B2)
Apply Filter and Sort your data set by the fourth column (Sort Smallest to Largest):
This seems to match your Table 1, 2 & 3 output.
Alternatively, if you still want to create separate tables, you can use the helper column and array formulas (Ctrl+Shift+Enter) as per example below:
=IFERROR(INDEX($A$1:$C$7,SMALL(IF($D$1:$D$7=1,ROW($D$1:$D$7)),ROW()-1),COLUMN()-5),"")
Change $D$1:$D$7=1 to =2 and =3 to replicate the 2nd and the 3rd table.
Edit: with additional "Location" column
=COUNTIFS($B$1:B2,B2,$D$1:D2,D2)

Can you normalize data with embedded sections in excel?

I was provided with some data in excel that I'm trying to transform to make it filterable. The data is in a specific order and looks like this:
Group ID RowType Name
1 Section Name1
1 Row AAA
1 Row BBB
1 Row CCC
1 Section Name2
1 Row DDD
1 Row EEE
2 Section ...
I want to take the name of the Row Type "Section" and transform it into a constant in it's own column. The final product would look like:
Group ID Section RowType Name
1 Name1 Row AAA
1 Name1 Row BBB
1 Name1 Row CCC
1 Name2 Row DDD
1 Name2 Row EEE
2 ...
I feel like this has probably be done but I haven't been able to put together the right terms to search... Any help would be appreciated.

Sum the value in one column based on the value in another column - excel 2013

I have 3 Columns, B,C and D.
Columns B,C,D will be updated periodically and the number of rows will be increased day by day.
Columns B contains NAME, C contains Quantity and D has Date value.
Name Qt Date
SSS 20
SSS 30
NNN 50
PPP 40 13-Jul-15
PPP 20 13-Jul-15
AAA 20
CCC 100
GGG 300
FFF 200
BBB 50 28-Aug-15
AAA 20
GGG 100
BBB 30
JJJ 50
BBB 30
FFF 50
FFF 25 24-Aug-15
CCC 75
JJJ 30
FFF 50
JJJ 36 24-Aug-15
FFF 50 24-Aug-15
I need the result as below.
Name Qt
AAA 40
BBB 30
CCC 175
FFF 300
GGG 400
JJJ 80
NNN 50
SSS 50
Rows with date field(column D) having a value should be omitted from adding.
Name field can contain any name and future names can vary from current names.
The Result can be in the same sheet or in the different sheet.
This may not meet your exact needs but it should get you the data you want in the first instance.
In Excel select the Insert tab and select Pivot Table.
Set the table range to be A1 to C60000 (or whatever the max number of rows is)
Select New Worksheet and click OK.
Add the Name and Qt fields to the report. It should automatically sum Qt.
Add Date to the Filters. This should add a bar above the pivot table that says something like:
Date (All)
Click on (All) and select (blank), and that should exclude the rows with dates specified.
use helper cells next to the date range =isblank(d2) this will return TRUE in E2
put AAA in G6, in H6 put the following formula =SUMIFS(C:C,E:E,"TRUE",B:B,G6)
if there is trash data in the blank cells like random spaces and whatnot use =isblank(trim(d2))
i loooove pivot tables but sometimes they just make file sizes too big if using a bunch of them in a single workbook, i also get tired of refreshing them.

Create a excel formula that combines 2 tables

I have 2 tables. Table 1 has employees and Table 2 has codes and their values.
For each employee row in Table 1 by looking up the last 5 characters and matching it Table 2 Column header,I want to insert new rows with all of the Table 2 Code rows and correspondnig "Plan" column value.
For Example, in Table1 1st row EE_Plan1, the last 5 characters "Plan1" should match the 2nd column in Table2, get the plan values (123,879) and insert new code rows as shown below in END RESULT.
Really appreciate any help with creating a formula. Thank you!!
TABLE1
Employee
--------
EE_Plan1
EE_Plan2
EE_Plan3
TABLE2
Code Plan1 Plan2 Plan3
---- ----- ----- -----
DND 123 456 jgh
ABC 879 978 ajs
END RESULT
Employee Code Plan Desc
-------- ---- ---------
EE_Plan1 DND 123
EE_Plan1 ABC 879
EE_Plan2 DND 456
EE_Plan2 ABC 978
EE_Plan3 DND jgh
EE_Plan3 ABC ajs
Vlookups are your friend here. Use a VLOOKUP to locate the corresponding value on the other table and place a cell's value in a different table by reference of Employee. Sorting the sheet before a lookup can help tremendously and set Range Looup to False.
=VLOOKUP(lookup_value,table_array,col_index_num,range_lookup)
For returning multiple values see this article from Microsoft.

Resources