cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Helper I
Helper I

Replace values by latest date Power Query

I am trying to replace the Street Address with the Street Address of 2020 (latest date). Some rows include City, State and Zip but I already have columns for those so i just want to keep one standard address. THis will help when i have to pivot later but need this first to give me one row per store.Capture.JPG

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Super User II
Super User II

Re: Replace values by latest date Power Query

Check out the M code below to do this:

It turns this

2020-04-23 12_13_18-Untitled - Power Query Editor.png

into this:

2020-04-23 12_13_31-Untitled - Power Query Editor.png

 

M code:

1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUUosTkkDUkYGRgZKsToIMSgGSxlagqWSQBwTYyNjE0OjQmMTZF0gqZKMDKCe9HIghuuKBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Customer = _t, Address = _t, Year = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer", type text}, {"Address", type text}, {"Year", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Customer"}, {{"All Rows", each _, type table [Customer=text, Address=text, Year=number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Latest Address", each Table.Max([All Rows],"Year")[Address]),
    #"Expanded All Rows" = Table.ExpandTableColumn(#"Added Custom", "All Rows", {"Address", "Year"}, {"Address", "Year"}),
    #"Removed Other Columns" = Table.SelectColumns(#"Expanded All Rows",{"Customer", "Latest Address", "Year"})
in
    #"Removed Other Columns"


Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

1 REPLY 1
Highlighted
Super User II
Super User II

Re: Replace values by latest date Power Query

Check out the M code below to do this:

It turns this

2020-04-23 12_13_18-Untitled - Power Query Editor.png

into this:

2020-04-23 12_13_31-Untitled - Power Query Editor.png

 

M code:

1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUUosTkkDUkYGRgZKsToIMSgGSxlagqWSQBwTYyNjE0OjQmMTZF0gqZKMDKCe9HIghuuKBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Customer = _t, Address = _t, Year = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer", type text}, {"Address", type text}, {"Year", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Customer"}, {{"All Rows", each _, type table [Customer=text, Address=text, Year=number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Latest Address", each Table.Max([All Rows],"Year")[Address]),
    #"Expanded All Rows" = Table.ExpandTableColumn(#"Added Custom", "All Rows", {"Address", "Year"}, {"Address", "Year"}),
    #"Removed Other Columns" = Table.SelectColumns(#"Expanded All Rows",{"Customer", "Latest Address", "Year"})
in
    #"Removed Other Columns"


Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

Helpful resources

Announcements
August 2020 Community Challenge: Can You Solve These?

August 2020 Community Challenge: Can You Solve These?

We're excited to announce our first cross-community 'Can You Solve These?' challenge!

Community Blog

Community Blog

Visit our Community Blog for articles, guides, and information created by fellow community members.

Upcoming Events

Upcoming Events

Wondering what events you could join or have an event to promote yourself? Check out our Upcoming Events.

Community Summit Australia – Join Online!

Community Summit Australia – Join Online!

Be a part of the leading Microsoft Business Applications digital event, curated for the APAC community.

Top Solution Authors
Top Kudoed Authors