Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
1 year ago
Solved

Getting End Date from The Row With Index - 1

I have a table similar to the following which lists job titles for different workers:

WORKER_IDEFFECTIVE_DATEDESCRIPTION
101/03/2024C
101/02/2024A
101/01/2024B
201/01/2025B
201/02/2025

A

 

 

I want to end up with the same table with an END_DATE column which is defined by the effective date of the job title which occured after the 'current' one. For example

WORKER_IDEFFECTIVE_DATEDESCRIPTIONEND_DATE
101/03/2024C 
101/02/2024A01/03/2024
101/01/2024B01/02/2024
201/01/2025B01/02/2025
201/02/2025A 

 

I've got my table to stage where it's grouped by WORKER_ID with a Index e.g

WORKER_IDEFFECTIVE_DATEDESCRIPTIONINDEX
101/03/2024C1
101/02/2024A2
101/01/2024B3

How do I add a custom column per WORKER_ID group which will give the effective date of the row with the index - 1?

 

Thank you

  •  

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIw1Dcw1jcyMDIBcpyVYnUQwkYwYUcUYUOYsBNY2AhZ2BRD2AgmDDQkFgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [WORKER_ID = _t, EFFECTIVE_DATE = _t, DESCRIPTION = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"EFFECTIVE_DATE", type date}},"en-GB"),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "END_DATE", (k)=> List.Min(Table.SelectRows(#"Changed Type",each [WORKER_ID]=k[WORKER_ID] and  [EFFECTIVE_DATE]>k[EFFECTIVE_DATE])[EFFECTIVE_DATE]),type date)
    in
        #"Added Custom"

    How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the entire Source step with your own source.

     

     

3 Replies

  •  

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIw1Dcw1jcyMDIBcpyVYnUQwkYwYUcUYUOYsBNY2AhZ2BRD2AgmDDQkFgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [WORKER_ID = _t, EFFECTIVE_DATE = _t, DESCRIPTION = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"EFFECTIVE_DATE", type date}},"en-GB"),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "END_DATE", (k)=> List.Min(Table.SelectRows(#"Changed Type",each [WORKER_ID]=k[WORKER_ID] and  [EFFECTIVE_DATE]>k[EFFECTIVE_DATE])[EFFECTIVE_DATE]),type date)
    in
        #"Added Custom"

    How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the entire Source step with your own source.

     

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thank you so much! worked great

  • Hi Anonymous 

     

    Another possibility

    let
    Source = Table.NestedJoin(Your_Source, {"WORKER_ID"}, Your_Source, {"WORKER_ID"}, "END_DATE", JoinKind.Inner),
    End_Date = Table.ReplaceValue(
    Source,
    each [EFFECTIVE_DATE],
    each [END_DATE][EFFECTIVE_DATE],
    (x,y,z) => List.Min(List.Select(z, each _ > y)),
    {"END_DATE"})
    in
    End_Date

    Stéphane