Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

July 28 - August 9 | Final Round of the Power BI Dataviz World Championships. This is your chance. Learn more

Reply
basic
Frequent Visitor

Remove duplicates selectively based on priority order

Finally gave up looking for examples to learn from, and decided to post my first question to the community.

 

Hello, i have a table similar to this:

 

Row#     Col A     Col B

1            A1          B1

2            A1          B2

3            A1          B3

4            A2          B2

5            A2          B3

6            A3          B1

7            A3          B3

 

I would like to remove duplicates on Col A such that -- whenever Col B = B1, I want to keep that row.  So between rows 1, 2 and 3, I would like to keep row 1.  Between rows 6 and 7, I will keep row 6.

 

When Col A has duplicated and Col B doesn't have any B1 (a specific value), I don't care which row I keep as long as I keep only one.  So between rows 4 and 5, I could keep either, doesn't matter.  

 

My resulting table should look like this:

 

Row#     Col A     Col B

1            A1          B1

4            A2          B2

6            A3          B1

 

OR

 

Row#     Col A     Col B

1            A1          B1

5            A2          B3

6            A3          B1

 

(as i said above, rows 4 and 5 mean the same to me)

 

Can anyone help out please?

1 ACCEPTED SOLUTION

Thanks Zubair.  Sorry I didn't specify that B1 in my data could appear on any row -- not necessarily on the first row all the time.  That makes the remove duplicates function not suitable for my requirement.  

I got help from a colleague and ended up learning a bit of M to get the job done.  My code looks something like this:

 

let
     Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
     #"B1 rows" = Table.Distinct(Table.SelectRows(Source, each ([Col B] = "B1")), {"Col A"}),
     #"A1 list" = Table.Column(#"B1 rows", "Col A"),
     #"Non B1 Rows" = Table.Distinct(Table.SelectRows(#"Table 1", each not List.Contains(#"A1 list", [DNS])), {"Col A"}),
     #"Appended Query" = Table.Combine({#"B1 rows", #"Non B1 Rows"})
in
     #"Appended Query"

 

The exception join (not List.Contains) must not be efficient because my query runs 10 minutes or longer.  But it gets the job done, and that's what i care about.

View solution in original post

3 REPLIES 3
Zubair_Muhammad
Community Champion
Community Champion

@basic 

 

If your data is sorted  (as in your sample data) i.e. B1 is the first row for each unique ColA item

 

you simple have to select Col A>>right click>>remove duplicates

 

removeduplicates.png

Thanks Zubair.  Sorry I didn't specify that B1 in my data could appear on any row -- not necessarily on the first row all the time.  That makes the remove duplicates function not suitable for my requirement.  

I got help from a colleague and ended up learning a bit of M to get the job done.  My code looks something like this:

 

let
     Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
     #"B1 rows" = Table.Distinct(Table.SelectRows(Source, each ([Col B] = "B1")), {"Col A"}),
     #"A1 list" = Table.Column(#"B1 rows", "Col A"),
     #"Non B1 Rows" = Table.Distinct(Table.SelectRows(#"Table 1", each not List.Contains(#"A1 list", [DNS])), {"Col A"}),
     #"Appended Query" = Table.Combine({#"B1 rows", #"Non B1 Rows"})
in
     #"Appended Query"

 

The exception join (not List.Contains) must not be efficient because my query runs 10 minutes or longer.  But it gets the job done, and that's what i care about.

Thanks Zubair.  Sorry I didn't specify that B1 in my data could appear on any row -- not necessarily on the first row all the time.  That makes the remove duplicates function is not suitable for my requirement.  

I got help from a colleague and ended up learning a bit of M to get the job done.  My code looks something like this:

 

let
   Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
   #"B1 rows" = Table.Distinct(Table.SelectRows(Source, each ([Col B] = "B1")), {"Col A"}),
   #"A1 list" = Table.Column(#"B1 rows", "Col A"),
   #"Non B1 Rows" = Table.Distinct(Table.SelectRows(#"Table 1", each not List.Contains(#"A1 list", [DNS])), {"Col A"}),
   #"Appended Query" = Table.Combine({#"B1 rows", #"Non B1 Rows"})
in
   #"Appended Query"

 

The exception join (not List.Contains) must not be efficient because my query runs 10 minutes or longer.  But it gets the job done, and that's what i care about.

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

Fabric Community Sticker Design Challenge Barcelona Carousel

Fabric Community Sticker Challenge - Barcelona 2026

If you love stickers, then you will definitely want to check out our community sticker challenge, Barcelona edition!

July Power BI Update Carousel

Power BI Monthly Update - July 2026

Check out the July 2026 Power BI update to learn about new features.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.