Compare Two Tables with the Power Query in Excel – 2 Examples

There’s a dataset showcasing student’s IDs and names,

Students' Records Dataset

and another one containing the Passed Students’ Records in Term 1 and Term 2.

Term Wise Students' Records to Compare Two Tables with Power Query

To to find the students who passed in both terms and the students who passed only in one term:


Example 1 – Comparing Two Tables and Merging All Values

Find the students who passed in one term only:

Steps:

  • Convert the datasets into tables.
  • Click any cell in the Students Record range >> go to Insert >> Tables >> Table.

Convert Range to Table to Compare with Power Query

  • In the Create Table window, enter B4:C14 >> check My table has headers >> click OK.

Create Table Window

  • Students Record is converted into a table.

Students Record Table

  • To name the table, click any value inside the table >> go to Table Design >> enter Students in Table Name:.

Rename the Table

  • Create two other tables: Term_1 and Term_2.

Two Table to Compare with Power Query

  • Go to the Power Query window, right-click  any cell inside the Students Record table and choose Get Data from Table/Range.

Open the Power Query Window to Compare Two Tables

  • The Students table is displayed in the Power Query.

Students Table in the Power Query Window

  • In the Home tab of the Power Query, click Close & Load >> choose Close & Load To…

Choose Close & Load To Option

  • In the Import Data window, select Only Create Connection and click OK.

Import Data Window

  • Repeat the previous procedure for the Term_1 and Term_2 tables to import data to the Power Query.

Tables to Compare in Power Query

  • Right-click the Queries pane >> choose New Query >> Other Sources>> Blank Query.

Create a New Blank Query to Compare Two Tables in Power Query

  • A new query will be created.
  • Name it “Merge All Values”.

Rename the Query

  • Click the Merge All Values query and enter the following formula to compare the two tables and merge values.
=List.Union({Term_1[Name],Term_2[Name]},Comparer.OrdinalIgnoreCase)
  • Press Enter.

Formula to Compare Two Tables and Merge Values in Power Query

  • You have compared and merged the two tables and found the students who passed in one term.
  • To get the IDs, go to Query Settings  >> APPLIED STEPS >> right-click Source >> choose Insert Step After.

Query Settings Pane

  • A new step will be added.
  • Name it “Insert IDs”.

Rename the Created Step

  • Click the Insert IDs step and enter the following formula in the formula bar.
=Table.SelectRows(Students,eachList.ContainsAny({[Name]},Source))
  • Press Enter.

Formula to Show Respective Student IDs

  • You will see the students’ IDs.

You have compared and merged the two tables.

This is the final output.

Compared and Merged Two Tables in Power Query


Example 2 – Comparing Two Tables with the Power Query and Finding the Common Values

To find the students who passed in both terms:

Steps:

  • Follow the first 16 procedures in Example 1 to prepare tables for the power query and create a new query: “Find Common Values”.

Create New Query to Compare and Intersect Two Tables in Power Query

  • Click Find Common Values and enter the following formula in the formula bar.
=List.Intersect({Term_1[Name],Term_2[Name]},Comparer.OrdinalIgnoreCase )
  • Press Enter.

Formula to Compare Two Tables and Find Common Values in Power Query

  • You find the students who passed in both terms.
  • To add their IDs, go to Query Settings >> APPLIED STEPS >> right-click Source >> choose Insert Step After.

Query Settings Pane

  • A new step is created.
  • Name it “Insert IDs”.
  • Click the Insert Ids step >> enter the following formula in the formula bar >> press Enter.
=Table.SelectRows(Students,each List.ContainsAny({[Name]},Source))
  • Students’ IDs are imported.

Formula to Import Students' IDs

This is the output.

Compared Two Tables and Found the Common Values in Power Query


Download Practice Workbook

Download the practice workbook here.


Related Articles


<< Go Back to Power Query Excel | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Tanjim Reza
Tanjim Reza

Md. Tanjim Reza Tanim, a BUET graduate in Naval Architecture & Marine Engineering, contributed over one and a half years to the ExcelDemy project. As an Excel & VBA Content Developer, he authored 100+ articles and, as Team Leader, reviewed 150+ articles. Tanim, leading research, ensures top-notch content on MS Excel features, formulas, solutions, tips, and tricks. His expertise spans Microsoft Office Suites, Automating Finance Templates, VBA, Python, and Developing Excel Applications, showcasing a multifaceted commitment to the... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo