Forum Discussion

MKDK's avatar
MKDK
Frequent Visitor
3 years ago
Solved

Identify start and end date in a snapshot date column

Hi Community, I have a table which contains snapshot of users which are having a specific license on a specific date. A user can have license for a month and then never have it again, or the user ca...
  • slorin's avatar
    3 years ago

    Hi

     

    1. Group by ID, sort by Date and add Index

    2. LocalGroup  if DateMax-DateMin = IndexMax-IndexMin then same group, else new group

     

    let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dY7BCcAwEMN28TvQxHfpMkf2XyOhrxZR8EtI4CoNNfVxnbk7tFrJREH0hGZohmYYDINW0kpak9bkiS/KH+um9UJrAw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"User ID" = _t, #"Snapshot Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"User ID", Int64.Type}, {"Snapshot Date", type date}}),
    #"Group1-Sort+Index" = Table.Group(
    #"Changed Type",
    {"User ID"},
    {{"Data", each Table.AddIndexColumn(Table.Sort(_,{{"Snapshot Date", Order.Ascending}}), "Index"),
    type table [User ID=nullable number, Snapshot Date=nullable date, Index=Int64.Type]}}),
    #"Group2-LocalGroup" = Table.AddColumn(
    #"Group1-Sort+Index",
    "Data2",
    each Table.Group(
    [Data],
    {"User ID", "Snapshot Date", "Index"},
    {{"Start", each List.Min([Snapshot Date]), type nullable date},
    {"End", each List.Max([Snapshot Date]), type nullable date}},
    GroupKind.Local,
    (x,y)=> if(y[Index]-x[Index])=Duration.Days(y[Snapshot Date]-x[Snapshot Date]) then 0 else 1
    )[[User ID],[Start],[End]]),
    Data2 = Table.Combine(#"Group2-LocalGroup"[Data2])
    in
    Data2

     

    Stéphane