在Application Insights分析中,如何查询特定异常影响用户的占比?
如何计算特定异常影响用户的占比?
Got it, let's break this down to get the percentage of users impacted by each exception relative to your total user base. You can combine your two existing queries into a single, streamlined query using the toscalar() function to pull in the total user count directly into your exception stats.
Here's the complete query that does exactly what you need:
let totalUsers = toscalar( customEvents | where application_Version == "xyz" | summarize dcount(user_Id) ); exceptions | where application_Version == "xyz" | summarize count_=count(itemCount), impactedUsers=dcount(user_Id), impactedUsersPercentage = iif(totalUsers == 0, 0.0, round(todouble(impactedUsers) / totalUsers * 100, 2)) by problemId, type, method, outerMessage, innermostMessage | order by impactedUsersPercentage desc
Quick breakdown of what's happening here:
let totalUsers = toscalar(...): This creates a variable that stores your total user count as a single scalar value. Thetoscalar()function lets you use this number seamlessly in the main exception query.impactedUsersPercentagecalculation: We convert counts to doubles to avoid integer division issues, then compute the percentage. Theiif(totalUsers == 0, 0.0, ...)handles edge cases where there might be no total users (prevents division-by-zero errors).round(..., 2): Keeps the percentage to 2 decimal places for readability—feel free to adjust this number if you want more or less precision.order by impactedUsersPercentage desc: Sorts results from the most impactful exceptions (by percentage) to the least. You can switch back toimpactedUsersif you prefer sorting by raw user count instead.
If you need to adjust any part—like adding more filters, changing rounding, or including extra fields—just tweak the query accordingly!
内容的提问来源于stack exchange,提问作者kopieczek.dev
相关产品推荐
相关产品推荐

