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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply

Table.Distinct power query with a condition

Hello

I speak about power queries on desktop power bi.

Here is my table records

[Field1 = "A", Field2 = "1", Field3= 20231201],

[Field1 = "A", Field2 = "1", Field3= 20240101],

[Field1 = "B", Field2 = "2", Field3= 20231201]

Is it possible to supress duplicates records on Field1 and Field2 with a condition on Field 3 asking for example to keep the newer date?

The result should be;

[Field1 = "A", Field2 = "1", Field3= 20240101],

[Field1 = "B", Field2 = "2", Field3= 20231201]

Is it possible with Table.Distinct or a kind of Power query like that?

Thanks!

1 ACCEPTED SOLUTION

Hi

I Found by myself.

The solutions proposed with grouping and expanding rows can't suppress duplicates fields.

We have to

  • sort [COLLABORATEURId], [CODE PROJETId] [DATE FIN] on descending [DATE FIN] (because I want to keep only Max [DATE FIN] for duplicates [COLLABORATEURId], [CODE PROJETId]
  • and then do a Table.Distinct

It works fine!

🙂

View solution in original post

10 REPLIES 10

Can somebody help please?

Hi

I Found by myself.

The solutions proposed with grouping and expanding rows can't suppress duplicates fields.

We have to

  • sort [COLLABORATEURId], [CODE PROJETId] [DATE FIN] on descending [DATE FIN] (because I want to keep only Max [DATE FIN] for duplicates [COLLABORATEURId], [CODE PROJETId]
  • and then do a Table.Distinct

It works fine!

🙂

And please don't forget I'm using Power BI Desktop not excel.

I have readen carrefully and did exactly this=>

= Table.Group(#"Colonnes renommées4", {"COLLABORATEURId", "CODE PROJETId"}, {{"COUPLE COLLAB PROJET", each List.Max([DATE FIN]), type datetime}})

And it works for Grouping.

But after there is nothing to expand by clicking on icons at top of column of [COUPLE COLLAB PROJET].

If I try to write directly  this =>

#"Expanded All" = Table.ExpandRecordColumn(#"Grouped Rows", "All", {"Field2", "Field3"}, {"Field2", "Field3"})

Do I have to put the name of all my 7 columns this way=>

#"Expanded All" = Table.ExpandRecordColumn(#"Grouped Rows", "All", {"Field2", "Field3","Field4","Field5","Field6","Field7" }, {"Field2", "Field3","Field4","Field5","Field6","Field7" })

?

Thanks.

You obviously have not see my solution carefully at all.  Where have i used List.Max????  I have used Table.Max.  Also, there is no type datetime in the Table.Group step of my solution.  Please study my solution carefully


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Field1", type text}, {"Field2", Int64.Type}, {"Field3", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Field1"}, {{"All", each Table.Max(_,"Field3")}}),
    #"Expanded All" = Table.ExpandRecordColumn(#"Grouped Rows", "All", {"Field2", "Field3"}, {"Field2", "Field3"})
in
    #"Expanded All"

Hope this helps.

Ashish_Mathur_0-1701473970820.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi Ashish

Thank you for you help. I succeeded to group. But It did not succeed on expanding all the other fields.

Screen1PBI.jpg

I got this when grouping.

Screen2PBI.jpg

How can i manage the Table.ExpandRecordColum so as to get back all the other fields?

Thanks.

 

You are welcome.  You just have to click on the icon in the header and select the fields that you want to expand. 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi Ashish

There is nothing to expand when i click these ways, have a look =>

Screen1PBI.jpg

Screen2PBI.jpg

I cannot help you by looking at screenshots.  Read my solution carefully again and retry.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.