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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Sort one by one column then by another then remove duplicates

I have a table in Excel like the one below. I can sort it by the left column and then by the right column. When I then remove duplicates in the left column, it leaves me with a single entry for that column and keeps the highest number from the right

column. I've pasted the Excel results here (I spaced the second column to show that the highest revision was definitely kept for the corresponding item in the first 2 columns). Is there a way to do this in PBI like I can Excel? Ultimately, I need to be able to find the highest revision number of each work order and then calculate the time it took for a work order to go from start to finish. I can't simply add the time between each revision because our system doesn't capture it that way. 

 

With Duplicates Duplicates Removed
WO #Revision # WO #Revision #
113564 113564
113563   
113562   
113561   
113560   
113641 113641
113640   
113663 113663
113662   
113661   
113660   
113921 113921
113920   
113983 113983
113982   
113981   
113980   
114073 114073
114072   
114071 114221
114070   
114221   
114220   
114332 114332
114331   
114330   
115286 115286
115285   
115284   
115283   
115282   
115281   
115280   

 

I appreciate any help. I also want to point out that while I can obviously do this in Excel, the point is to do as little manipulation in Excel as possible. Currently, every last report I have created works that way. It makes it easier for any other person to run the report rather than trying to understand how I did it in Excel and then transformed it to PBI. 

1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"WO #", Int64.Type}, {"Revision #", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"WO #"}, {{"Max", each List.Max([#"Revision #"]), type nullable number}, {"All", each _, type table [#"WO #"=nullable number, #"Revision #"=nullable number]}}),
    #"Expanded All" = Table.ExpandTableColumn(#"Grouped Rows", "All", {"Revision #"}, {"Revision #"}),
    #"Added Custom" = Table.AddColumn(#"Expanded All", "Custom", each [Max]=[#"Revision #"]),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Custom] = true)),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Max", "Custom"})
in
    #"Removed Columns"

Hope this helps.

Untitled.png


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

View solution in original post

5 REPLIES 5
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"WO #", Int64.Type}, {"Revision #", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"WO #"}, {{"Max", each List.Max([#"Revision #"]), type nullable number}, {"All", each _, type table [#"WO #"=nullable number, #"Revision #"=nullable number]}}),
    #"Expanded All" = Table.ExpandTableColumn(#"Grouped Rows", "All", {"Revision #"}, {"Revision #"}),
    #"Added Custom" = Table.AddColumn(#"Expanded All", "Custom", each [Max]=[#"Revision #"]),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Custom] = true)),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Max", "Custom"})
in
    #"Removed Columns"

Hope this helps.

Untitled.png


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

Hi @Anonymous ,

with DAX you can achieve this as follows:

 

04-06-_2021_23-34-55.png

 

With kind regards from the town where the legend of the 'Pied Piper of Hamelin' is at home
FrankAT (Proud to be a Datanaut)

 

 

Anonymous
Not applicable

For whatever reason, I can't get this to work. ...doh...it (my known column to verify it was returning properly)was set as sum and not do not summarize. Works fine. 

 

Anonymous
Not applicable

Ok, I figured how to edit the query to sort by WO then by Revision. But, when I then remove duplicates, it keeps the lowest revision number vice the highest. Maybe another line in the query editor? 

 

#"Sorted Rows" = Table.Sort(#"Reordered Columns",{{"PO", Order.Ascending},{"Revision", Order.Descending}})

Anonymous
Not applicable

For whatever reason, the cut and paste didn't hold. Just know that the WO in the 2 instance is the highest remaining by revision after dupes are removed. 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.