Search code examples
azure-application-insightskql

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


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?


Solution

  • Per the MS docs:

    Classic Application Insights resources don't support parameterized functions. If you have a workspace-based Application Insights resource, you can create parameterized functions from your Log Analytics workspace. For information on migrating your Classic Application Insights resource to a workspace-based resource, see Migrate to workspace-based Application Insights resources.

    Not sure what you have, but this was my issue.