How to write a reusable Application Insights function that groups the data?

Viewed 151

Consider the following KQL query:

datatable(Value:int, ts:datetime) [
    10, datetime(2022-03-30),
    20, datetime(2022-03-30),
    10, datetime(2022-03-31),
    20, datetime(2022-03-31),
]
| where ts == '2022-03-30'
| summarize count() by Value

I would like to save it as a function without the timestamp filter, of course. Suppose this function would be named MyFunc. It should accept the timestamp as a parameter and called like this:

MyFunc('2022-03-30')

The Microsoft documentation implies it is possible to save a function with parameters, but I do not understand how exactly am I to define such a function.

So, what am I missing? How can we save a function with parameters? This way I could have a reusable function that does grouping.

EDIT 1

So I modified the code:

datatable(Value:int, ts:datetime) [
    10, datetime(2022-03-30),
    20, datetime(2022-03-30),
    10, datetime(2022-03-31),
    20, datetime(2022-03-31),
]
| where ts == p_ts
| summarize count() by Value

But when I am trying to save it, the dialog has no parameters: enter image description here

What am I missing?

1 Answers

Choose a name for your parameter, and use it in your query (in this example I'll be using p_ts)


Here is the query, with the parameter:

range ts from now() to ago(24h) step -1h
| where ts >= p_ts
| summarize count() by bin(ts,4h)

Here is the function definition, including the p_ts parameter definition

Definition


Here are couple of invocation examples:

1.

Default Parameter


2.

ago(10h)

Related