Keep and Remove Duplicates in Power Query Editor in Power BI

In the Power Query Editor, we can keep or remove duplicates.

Let’s we have the following table named Students.

Keep and Remove Duplicates in Power Query Editor

The M code for creating the above table:

Example: Creating a table of student marks.

Power Query M

let
    StudentMarks = #table(
        {"Student ID", "Name", "Subject", "Marks"},
        {
            {101, "Ashish", "Math", 85},
            {101, "Ashish", "Math", 85},
            {102, "Neha", "Science", 92},
            {103, "Raj", "Math", 85},
            {104, "Divya", "Science", 92},
            {105, "Ashish", "Math", 75},
            {106, "Meena", "English", 88},
            {107, "Neha", "Science", 92}
        }
    )
in
    StudentMarks 

This code creates a sample table named StudentMarks with student IDs, names, subjects, and marks.

a) Keep Duplicates

The Keep Duplicates operation returns all the rows which are duplicated in the selected columns.

Step 1: Select the table icon, then from the context menu, select Keep Duplicates.

Keep Duplicates in Power Query Editor

Now we can see the rows which are duplicated in the table.

Duplicated Rows in Power Query

Step 2: Alternatively, select the columns from the table, then choose Keep Duplicates from the Keep Rows dropdown.

Keep Duplicates from Keep Rows Dropdown

The output is shown below:

Output After Keeping Duplicates

b) Remove Duplicates

The Remove Duplicates option removes duplicate rows and keeps only unique rows in the table.

Step 1: Select the table dropdown, then choose Remove Duplicates from the context menu.

Remove Duplicates Option in Power Query

The output after removing duplicates is shown below:

Output After Removing Duplicates

Step 2: You can also select a specific column (or multiple columns) and then choose Remove Duplicates.

Remove Duplicates by Column

After removing the duplicates, the final output is shown below:

Final Output After Removing Duplicates

Internally remove duplicates uses the Table.Distinct M function.

Warning: Power Query is case-sensitive. When working with duplicate values, Power Query considers the case of the text, which might lead to undesired results. As a workaround, users can apply an Uppercase or Lowercase transform prior to removing duplicates.