Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Kusto query which calculates percentages of values by keys

I want to calculate percentages of values by keys. An example would be, given a table like:

datatable (key: string, value: string) 
[ 
    "a","1",
    "a","2",
    "b","x",
    "b","x",
    "b","x",
    "b","y",
]

I want to get results like:

[
    "a","1",.5,
    "a","2",.5,
    "b","x",.75,
    "b","y",.25,

]

I understand how to use as and toscalar to get percentages of values across all keys, but I can’t figure out how to make that work by keys.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

We need to use join between aggregations in two levels

datatable (key: string, value: string) 
[ 
    "a","1",
    "a","2",
    "b","x",
    "b","x",
    "b","x",
    "b","y",
]
| summarize count() by key, value
| as summarize_by_key_value
| summarize sum(count_) by key
| join kind=inner summarize_by_key_value on key
| project key, value, percentage = 1.0 * count_ / sum_count_
key value percentage
a 1 0.5
a 2 0.5
b x 0.75
b y 0.25

Fiddle

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading