Kusto: Apply function on multiple column values during bag_unpack

Viewed 242

Given a dynamic field, say, milestones, it has value like: {"ta": 1655859586546, "tb": 1655859586646},

How do I print a table with columns like "ta", "tb" etc, with the single row as unixtime_milliseconds_todatetime(tolong(taValue)), unixtime_milliseconds_todatetime(tolong(tbValue)) etc.

I figured that I'll need to write a function that I can call, so I created this:-

let f = view(a:string ){
unixtime_milliseconds_todatetime(tolong(a))
};

I can use this function with a normal column as:- project f(columnName).

However, in this case, its a dynamic field, and the number of items in the list is large, so I do not want to enter the fields manually. This is what I have so far.

log_table
| take 1
| evaluate bag_unpack(milestones, "m_") // This gives me fields as columns
// | project-keep m_* // This would work, if I just wanted the value, however, I want `view(columnValue)
| project-keep f(m_*) // This of course doesn't work, but explains the idea.
1 Answers

Based on the mv-apply operator

// Generate data sample. Not part of the solution.
let log_table = materialize(range record_id from 1 to 10 step 1 | mv-apply range(1, 1 + rand(5), 1) on (summarize milestones = make_bag(pack_dictionary(strcat("t", make_string(to_utf8("a")[0] + toint(rand(26)))), 1600000000000 + rand(60000000000)))));
// Solution Starts here.
log_table
|   mv-apply kv = milestones on 
    (
            extend k = tostring(bag_keys(kv)[0]) 
        |   extend v = unixtime_milliseconds_todatetime(tolong(kv[k]))
        |   summarize milestones = make_bag(pack_dictionary(k, v))
    )
|   evaluate bag_unpack(milestones)
record_id ta tb tc td te tf tg th ti tk tl tm to tp tr tt tu tw tx tz
1 2021-07-06T20:24:47.767Z
2 2021-05-09T07:21:08.551Z 2022-07-28T20:57:16.025Z 2022-07-28T14:21:33.656Z 2020-11-09T00:54:39.71Z 2020-12-22T00:30:13.463Z
3 2021-12-07T11:07:39.204Z 2022-05-16T04:33:50.002Z 2021-10-20T12:19:27.222Z
4 2022-01-31T23:24:07.305Z 2021-01-20T17:38:53.21Z
5 2022-04-27T22:41:15.643Z
7 2022-01-22T08:30:08.995Z 2021-09-30T08:58:46.47Z
8 2022-03-14T13:41:10.968Z 2022-03-26T10:45:19.56Z 2022-08-06T16:50:37.003Z
10 2021-03-03T11:02:02.217Z 2021-02-28T09:52:24.327Z 2021-04-09T07:08:06.985Z 2020-12-28T20:18:04.973Z
9 2022-02-17T04:55:35.468Z
6 2022-08-02T14:44:15.414Z 2021-03-24T10:22:36.138Z 2020-12-17T01:14:40.652Z 2022-01-30T12:45:54.28Z 2022-03-31T02:29:43.114Z

Fiddle

Related