Add column of totals pr. field value

2019-08-18 04:00发布

问题:

I start with a list of failures that take place in locations

failureName, failureLocation
failure a, location 1
failure b, location 1
failure a, location 2
failure a, location 1
<etc>

I can transform that into this table by using summarize count() by location

failureName, failureLocation, count
failure a, location 1, 100
failure a, location 2, 50
failure b, location 1, 10
<etc>

I'd like to transform the counts into percent on a per. failure basis, so I need to add a sum per failure name. My goal is to end up with this table:

failureName, failureLocation, count, sumPerFailureName
failure a, location 1, 100, 150
failure a, location 2, 50, 150
failure b, location 1, 10, 10
<etc>

Suggestions?

回答1:

Try this, to take you from your 2nd table to the 3rd (and extend a calculated column of the percentage):

let T = datatable(failureName:string, failureLocation:string, ['count']:long) [ 'failure a', 'location 1', 100, 'failure a', 'location 2', 50, 'failure b', 'location 1', 10, ] ; T | summarize sumPerFailureName = sum(['count']) by failureName | join ( T ) on failureName | project failureName, failureLocation, ['count'], sumPerFailureName, percentage = round(100.0 * ['count'] / sumPerFailureName, 2)