Forum Discussion
Find last camera status
Yes, you can do this in the M lanaguge, note it won't be very efficient for large files.
For step 1...
Let's say your main query is called MainQuery.
1. Click the Event Time column and under Add column choose Date Only
2. Right click your MainQuery, and choose reference.
3. Click the Event Time column, choose Transform -> Date -> Earliest. This should give a single non-table value of the earliest datetime
4. Rename query to StartDate
5. Add a blank query with the following line, this will generate all days for your events:
= #table(type table [Date = date], List.Zip({List.Generate(() => StartDate, each _ <= DateTime.Date(DateTime.LocalNow()), each _ + #duration(1, 0, 0,0))}))
6. Add a custom column with the name "Last Event" with
= List.Distinct(MainQuery[Entity source])
7. Expand list column
8. Add a custom column wtih:
= Table.AddColumn(#"Expanded Source entity", "MostRecentEvent", each Table.Max(Table.SelectRows(MainTable, (row) => row[Date] <= [Date]), "Event timestamp"))
8. Rename query to something more appropiate.
Result: You now have a table with every source for every day, and this includes any gaps where a device didn't report anything in a day. Additionally there is now a column with "Last Event" which is the last event that occured by the end of that day.
Next steps: Add a custom column which extract various stats from the table Main query using the event source and date column. The Last Even column is used to determine if the camera was on at the start of the day or off.
- Anonymous4 years agoNot applicable
im stuck at step 4. It gives me:
Query error ‘’. Expression.Error: We were unable to apply the operator < aos tipos Date e DateTime.
Detalhes:
Operator=<
Left=25/03/2022
Right=24/03/2022 17:14:20- artemus4 years agoMicrosoft Employee
Oh, just wrap the right side of =< with a DateTime.Date() call.
- Anonymous4 years agoNot applicable
im not shure if i undestand correctly, but still not working. Can i call you or you call me on teams? my mail is [email protected]
- Anonymous4 years agoNot applicable
Great! i made it to the step 8 and I'm stuck again..here is the query, I'm forgetting smth.
let
Events = let
Lista = #table(type table [Date = date], List.Zip({List.Generate(() => DateTime.Date(StartDate), each _ <= DateTime.Date(DateTime.LocalNow()), each _ + #duration(1, 0, 0,0))})),
#"Last Event" = Table.AddColumn(#"Lista", "Last Event", each List.Distinct([Source entity]))
in
Lista,
#"Last Event" = Table.AddColumn(Events, "Last Event", each List.Distinct(fStatus[Source entity])),
#"Valores Extraídos" = Table.TransformColumns(#"Last Event", {"Last Event", each Text.Combine(List.Transform(_, Text.From)), type text}),
#"Personalização Adicionada" = Table.AddColumn(#"Valores Extraídos", "Personalizar", each Table.AddColumn(#"Last Event", "MostRecentEvent", each Table.Max(Table.SelectRows(fStatus, (row) => row[Data] <= fStatus[Event timestamp]), "Event timestamp"))),
#"Personalizar Expandido" = Table.ExpandTableColumn(#"Personalização Adicionada", "Personalizar", {"Date", "Last Event", "MostRecentEvent"}, {"Date.1", "Last Event.1", "MostRecentEvent"}),
#"Valores Extraídos1" = Table.TransformColumns(#"Personalizar Expandido", {"Last Event.1", each Text.Combine(List.Transform(_, Text.From)), type text})
in
#"Valores Extraídos1"