Power Query Challenge - Find Latest Value and filter out the rest

Nothnless

Board Regular
Joined
Apr 28, 2016
Messages
142
Hi, I'm Using Get&Transform in Excel which is equivalent to Power Query in Power BI. It even uses the same M language.

Is there a way to use the query to get a result that displays the following?

If there are duplicate errors then keep the latest error of those duplicates and filter out the rest.

Original table:
TimeNameError
1:00JohnY
2:00JohnY
3:00SmithN
4:00DaveY
3:00JohnY

Result:
TimeNameError
3:00SmithN
4:00DaveY
3:00JohnY

In this case the duplicate John's with errors were removed except for the John at 3:00

Thank you,
 
I other words, I don't really care about the Error code column or the last name column but it should still be displayed, when grouping it hides these for some reason.
 
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
I think if you follow my post #7 you should achieve your aim. :)
 
Upvote 0
like this?

gro.jpg


I think you should decide what you want to achieve

anyway you got a template so try yourself
 
Upvote 0
Now I see what you actually want to achieve, its possible, but a bit more complicated!

After expanding your columns in the Powerquery editor, add a conditional column and compare the Max value with Time set the result to True when they are the same and False when not.
Filter the new column so only True is showing, delete the new column.

Close and Load as before.

These were the steps I used, obviously column names will be different:

Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Time", type number}, {"Name", type text}, {"Error", type text}, {"Rubbish", type text}, {"Stuff", type text}, {"Nonsense", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Name", "Error"}, {{"Max", each List.Max([Time]), type number}, {"Other Rows", each _, type table [Time=number, Name=text, Error=text, Rubbish=text, Stuff=text, Nonsense=text]}}),
    #"Expanded Other Rows" = Table.ExpandTableColumn(#"Grouped Rows", "Other Rows", {"Time", "Rubbish", "Stuff", "Nonsense"}, {"Time", "Rubbish", "Stuff", "Nonsense"}),
    #"Added Conditional Column" = Table.AddColumn(#"Expanded Other Rows", "Custom", each if [Time] = [Max] then true else false),
    #"Filtered Rows" = Table.SelectRows(#"Added Conditional Column", each ([Custom] = true)),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Custom"})
in
    #"Removed Columns"
 
Upvote 0
Now I see what you actually want to achieve, its possible, but a bit more complicated!

After expanding your columns in the Powerquery editor, add a conditional column and compare the Max value with Time set the result to True when they are the same and False when not.
Filter the new column so only True is showing, delete the new column.

Close and Load as before.

These were the steps I used, obviously column names will be different:

Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Time", type number}, {"Name", type text}, {"Error", type text}, {"Rubbish", type text}, {"Stuff", type text}, {"Nonsense", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Name", "Error"}, {{"Max", each List.Max([Time]), type number}, {"Other Rows", each _, type table [Time=number, Name=text, Error=text, Rubbish=text, Stuff=text, Nonsense=text]}}),
    #"Expanded Other Rows" = Table.ExpandTableColumn(#"Grouped Rows", "Other Rows", {"Time", "Rubbish", "Stuff", "Nonsense"}, {"Time", "Rubbish", "Stuff", "Nonsense"}),
    #"Added Conditional Column" = Table.AddColumn(#"Expanded Other Rows", "Custom", each if [Time] = [Max] then true else false),
    #"Filtered Rows" = Table.SelectRows(#"Added Conditional Column", each ([Custom] = true)),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Custom"})
in
    #"Removed Columns"

Thanks so much! Sorry I should have made it clear but didn't realize it would ungroup like that. I really appreciate the help, I was getting ready to duplicate the table and do a Left Outer join on the grouped table to see if that would work. Thanks again!
 
Upvote 0
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Type = Table.TransformColumnTypes(Source,{{"Time", type time}}),
    Group = Table.Group(Type, {"Name", "Error", "Last Name"}, {{"Time", each List.Max([Time]), type time}, {"Count", each _, type table}}),
    ErrCode = Table.AddColumn(Group, "Error Code", each List.Last(List.Distinct(Table.Column([Count],"Error Code"))))
in
    ErrCode
grp.jpg
 
Last edited:
Upvote 0
Thanks Nothnless, a pleasure to help and thanks for the feedback.
 
Upvote 0
Code:
// Table1
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Type = Table.TransformColumnTypes(Source,{{"Time", type time}, {"Name", type text}, {"Error", type text}, {"Last Name", type text}, {"Error Code", type text}}),
    Group = Table.Group(Type, {"Name", "Error", "Last Name"}, {{"Time", each List.Max([Time]), type time}, {"Count", each _, type table}}),
    ErrCode = Table.AddColumn(Group, "Error Code", each List.Last(List.Distinct(Table.Column([Count],"Error Code"))))
in
    ErrCode
View attachment 5297

Thanks Sandy, I'll take a look at this but if I had 30 more columns would you have to duplicate your code in ErrCode 30 times?
 
Upvote 0

Forum statistics

Threads
1,223,809
Messages
6,174,761
Members
452,582
Latest member
ruby9c

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top