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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
evebarratt
Helper I
Helper I

Latest and second latest date

Hi, I need to change a query from table A to table B as below.

 

evebarratt_2-1656484000575.png

Basically I need to get the difference between the latest and the second latest date for each product ID.

 

Is there any way to do this in Advanced Editor?

1 ACCEPTED SOLUTION
Vijay_A_Verma
Super User
Super User

See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIyMDLUBUKlWB1kASMsIsYQISOIkJGuqS6agLmuOaqABboKoICBUmwsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Product ID" = _t, #"Sold Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Product ID", Int64.Type}, {"Sold Date", type date}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Product ID"}, {{"Gap between last 2 days", each Duration.Days(List.Last(_[Sold Date])-List.First(List.LastN(_[Sold Date],2))), Int64.Type}})
in
   #"Grouped Rows"

View solution in original post

2 REPLIES 2
Vijay_A_Verma
Super User
Super User

See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIyMDLUBUKlWB1kASMsIsYQISOIkJGuqS6agLmuOaqABboKoICBUmwsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Product ID" = _t, #"Sold Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Product ID", Int64.Type}, {"Sold Date", type date}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Product ID"}, {{"Gap between last 2 days", each Duration.Days(List.Last(_[Sold Date])-List.First(List.LastN(_[Sold Date],2))), Int64.Type}})
in
   #"Grouped Rows"

It works great! Thank you.

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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

Top Solution Authors
Top Kudoed Authors