Azure Application Insights Query - How to calculate percentage of total

ε祈祈猫儿з 提交于 2021-01-27 04:28:09

问题


I'm trying to create a row in an output table that would calculate percentage of total items:

Something like this:
ITEM   |   COUNT   |   PERCENTAGE
item 1 |     4     |   80
item 2 |     1     |   20 

I can easily get a table with rows of ITEM and COUNT, but I can't figure out how to get total (5 in this case) as a number so I can calculate percentage in column %.

someTable
| where name == "Some Name"
| summarize COUNT = count() by ITEM = tostring( customDimensions.["SomePar"])
| project ITEM, COUNT, PERCENTAGE = (C/?)*100 

Any ideas? Thank you.


回答1:


It's a bit messy to create a query like that.

I've done it bases on the customEvents table in AI. So take a look and see if you can adapt it to your specific situation.

You have to create a table that contains the total count of records, you then have to join this table. Since you can join only on a common column you need a column that has always the same value. I choose appName for that.

So the whole query looks like:

let totalEvents = customEvents
//  | where name contains "Opened form"
    | summarize count() by appName
    | project appName, count_ ;
customEvents
//  | where name contains "Opened form"
    | join kind=leftouter totalEvents  on appName
    | summarize count() by name, count_
    | project name, totalCount = count_ , itemCount = count_1,  percentage = (todouble(count_1) * 100 / todouble(count_))     

If you need a filter you have to apply it to both tables.

This outputs:




回答2:


I think following is more intuitive. Just extend the set with a dummy property and do a join on that...

requests
| summarize count()
| extend a="b" 
| join (
    requests
    | summarize count() by name
    | extend a="b"    
) on a 
| project name, percentage = (todouble(count_1) * 100 / todouble(count_)) 



回答3:


It is not even necessary to do a join or create a table containing your totals Just calculate your total and save it in a let like so.

let totalEvents = toscalar(customEvents
| where timestamp > "someDate"
    and name == "someEvent"
| summarize count());

then you can simply add a row to your next table, where you need the percentage calcualtion by doing:

| extend total = totalEvents

This will add a new column to your table filled with the total you calculated. After that you can calculate the percentages as described in the other two answers.

| extend percentages = todouble(count_)*100/todouble(total)

where count_ is the column created by your summarize count() which you presumably do before adding the percentages.

Hope this also helps someone.



来源:https://stackoverflow.com/questions/40003713/azure-application-insights-query-how-to-calculate-percentage-of-total

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!