Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Application Insights Query - How to calculate percentage of total

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.

like image 899
mpnap Avatar asked Oct 12 '16 16:10

mpnap


People also ask

How do you find QA percentage?

To determine the percentage, we have to divide the value by the total value and then multiply the resultant by 100.


2 Answers

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.

like image 88
Maverick1st Avatar answered Oct 19 '22 00:10

Maverick1st


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:

enter image description here

like image 43
Peter Bons Avatar answered Oct 19 '22 02:10

Peter Bons