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
susheeltyagi
Helper I
Helper I

How to get the find the recent records from the duplicate records using Power Query Editor

HI,

 

I need small help. I am importing the data from SQL Server database. I would like to get the most recent record on the basese of  Serial Number and UserTime using Power Query at the stage of Transformation of data.

How to do it ?

 

Input Data as below :

(Table : Device)

susheeltyagi_0-1680259194809.png

 

 

Expected output after Transform of data using Power Editor :

susheeltyagi_1-1680259229155.png

 

Thanks in advance

 

Susheel

1 REPLY 1
jennratten
Super User
Super User

Hello - this is how you can do it.

BEFORE

jennratten_0-1680260812505.png

AFTER

jennratten_1-1680260838414.png

SCRIPT

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jc5bCsQwCAXQrRS/2+FezcO4ldD9b6MGhs7HFGYgSFSOOqdQdhkoQG09v15HRrUVwHHAD+pGDSDfC4Cc+xT9rbjRAh5st6p/7UrVg36rln1jZm2UNaD7KhT/YmbBz4n9mdUnpm92Xg==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Id = _t, SerialNumber = _t, RemaingItems = _t, ReadingRemaining = _t, UserTimer = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Id", Int64.Type}, {"SerialNumber", Int64.Type}, {"RemaingItems", Int64.Type}, {"ReadingRemaining", Int64.Type}, {"UserTimer", type datetime}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"SerialNumber"}, {{"MaxTimer", each List.Max([UserTimer]), type nullable datetime}, {"Data", each _, type table [Id=nullable number, SerialNumber=nullable number, RemaingItems=nullable number, ReadingRemaining=nullable number, UserTimer=nullable datetime]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "MaxValue", each Table.SelectRows ( [Data], (x) => x[UserTimer] = [MaxTimer] )),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"MaxValue"}),
    #"Expanded MaxValue" = Table.ExpandTableColumn(#"Removed Other Columns", "MaxValue", {"Id", "SerialNumber", "RemaingItems", "ReadingRemaining", "UserTimer"}, {"Id", "SerialNumber", "RemaingItems", "ReadingRemaining", "UserTimer"})
in
    #"Expanded MaxValue"

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.

Top Solution Authors
Top Kudoed Authors