Excel formula for Beginners – How to Extract common values from two lists in Excel

Hits: 111

 

(Excel examples for Beginners)

In this end-to-end excel example, you will learn – Excel formula for Beginners – How to Extract common values from two lists in Excel.

 

Excel formula for Beginners – How to Extract common values from two lists in Excel

Generic formula

=FILTER(list1,COUNTIF(list2,list1))

Explanation

To compare two lists and extract common values, you can use a formula based on the FILTER and COUNTIF functions. In the example shown, the formula in F5 is:

=FILTER(list1,COUNTIF(list2,list1))

where list1 (B5:B15) and list2 (D5:D13) are named ranges. The result, values that appear in both lists, spills into the range F5:F11.

How this formula works

The FILTER function accepts an array of values and an “include” argument which filters the array based on a logical expression or value.

In this case, the array is provided as the named range “list1”, which contains all values in B5:B15. The include argument is delivered by the COUNTIF function, which is nested inside FILTER:

=FILTER(list1,COUNTIF(list2,list1))

COUNTIF is set up with list2 as range, and list1 as criteria. Because we give COUNTIF eleven criteria values, COUNTIF returns eleven results in an array like this:

{1;1;0;1;0;1;0;1;0;1;1}

Notice the 1’s correspond to items in list2 that appear in list1.

This array is delivered directly to the FILTER function as the “include” argument:

=FILTER(list1,{1;1;0;1;0;1;0;1;0;1;1})

The FILTER function filters list1 using the values provided by COUNTIF. Values associated with zero are removed; other values are preserved.

The final result is an array of values that exist in both lists, which spills into the range F5:F11.

Extended logic

In the above formula, we use the raw results from COUNTIF as the filter. This works because Excel evaluates any non-zero value as TRUE, and zero as FALSE. If COUNTIF returns a count greater than 1, the filter will still work properly.

To force TRUE and FALSE results explicitly, you can use “>0” like this:

=FILTER(list1,COUNTIF(list2,list1)>0)

Remove duplicates or sort

To remove duplicates, just nest the formula inside the UNIQUE function:

=UNIQUE(FILTER(list1,COUNTIF(list2,list1)))

To sort results, nest in the SORT function:

=SORT(UNIQUE(FILTER(list1,COUNTIF(list2,list1))))

List values missing from list2

To output values in list1 missing from list2, you can reverse the logic like this:

=FILTER(list1,COUNTIF(list2,list1)=0)

Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners

Please do not waste your valuable time by watching videos, rather use end-to-end (Python and R) recipes from Professional Data Scientists to practice coding, and land the most demandable jobs in the fields of Predictive analytics & AI (Machine Learning and Data Science).

The objective is to guide the developers & analysts to “Learn how to Code” for Applied AI using end-to-end coding solutions, and unlock the world of opportunities!