Forum Discussion
MKDK
3 years agoFrequent Visitor
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...
- 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
Data2Stéphane
slorin
Super User
3 years agoHi
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
MKDK
3 years agoFrequent Visitor
Hi Stéphane, I have pasted your m-code but I am not getting expected result. Each user has 1 record with duration of 1 day. The expected output is 5 records as shown in table in my initial post.
Could you share screenshot of the output on your side?