SOLVED

How to aggregate results from a query

%3CLINGO-SUB%20id%3D%22lingo-sub-1672519%22%20slang%3D%22en-US%22%3EHow%20to%20aggregate%20results%20from%20a%20query%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1672519%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EI%20have%20built%20a%20simple%20query%20to%20show%20the%20result%20of%20a%20specific%20conditional%20access%20policy%2C%20but%20I%20am%20struggling%20with%20device%20info.%20Cannot%20aggregate%20the%20info%20into%20one%20row.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3EFirst%2C%20let's%20start%20with%20the%20most%20simple%20query%20and%20filter%20on%20the%20specific%20sign-in%20event%20(CorrelationId).%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powerquery%22%3E%3CCODE%3ESigninLogs%0A%7C%20where%20TimeGenerated%20%26gt%3B%20ago(1d)%0A%7C%20mvexpand%20PolicyResults%20%3D%20ConditionalAccessPolicies%0A%7C%20mvexpand%20Device%20%3D%20DeviceDetail%0A%7C%20where%20(PolicyResults.id%20%3D%3D%20%22%23%23%23%23-004016656ca4%22%20or%20PolicyResults.id%20%3D%3D%20%22%23%23%23%23-a2e429a5325d%22)%20%20and%20PolicyResults.result%20!%3D%20%22reportOnlyNotApplied%22%0A%7Cwhere%20CorrelationId%20%3D%3D%20%22%23%23%23%23%23-10c620ed816d%22%0A%7Cproject%20Mail%3DUserPrincipalName%2C%20User_Name%3DUserDisplayName%2C%20Application_name%3DAppDisplayName%2C%20Protocol%3DClientAppUsed%2C%20Device%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIt%20gives%20me%20such%20results%3A%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22q1.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F218714i567F8E2476C17B05%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22q1.png%22%20alt%3D%22q1.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CDIV%3E%3CDIV%3EIf%20I%20refer%20to%20specific%20properties%20in%20the%20Device%20and%20get%20them%20in%20separate%20columns%2C%20still%20got%20multiply%20raws%20as%20result.%26nbsp%3B%3C%2FDIV%3E%3C%2FDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powerquery%22%3E%3CCODE%3E%7Cproject%20Mail%3DUserPrincipalName%2C%20User_Name%3DUserDisplayName%2C%20Application_name%3DAppDisplayName%2C%20Protocol%3DClientAppUsed%2C%20Device.displayName%2C%20Device.operatingSystem%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CDIV%3E%3CSPAN%3EBut%20still%20got%20results%20in%20multiply%20rows.%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22q2.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F218712i8CEB3A0801EDBB7C%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22q2.png%22%20alt%3D%22q2.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3EI%20guess%20%22summarize%22%20can%20help%20but%20don't%20know%20how%20to%20use%20it.%26nbsp%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3EAny%20tips%3F%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1672519%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ELog%20Analytics%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EQuery%20Language%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1675329%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20aggregate%20results%20from%20a%20query%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1675329%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F258718%22%20target%3D%22_blank%22%3E%40Michal_Z%3C%2FA%3E%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20order%20to%20summarize%20you%20first%20need%20to%20figure%20out%20what%20are%20the%20expected%20results%2C%20and%20how%20you%20want%20to%20summarize%20your%20results%2C%20I%20couldn't%20figure%20out%20from%20your%20description%20what%20is%20the%20desired%20result%20set.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20summarize%20you%20should%20use%20an%20aggregation%20function%20(if%20you%20don't%20use%20one%20the%20functionality%20is%20the%20same%20as%20the%20'distinct'%20operator).%3C%2FP%3E%0A%3CP%3EI'd%20suggest%20here%20the%20simplest%20function%20which%20is%20'count()'%2C%20it%20will%20give%20you%20the%20count%20of%20unique%20combinations%20occurrences%20of%20the%20columns%20mentioned%20on%20the%20right%20hand%20of%20the%20expression%20(after%20'by')%2C%20try%20adding%20the%20following%20line%20to%20the%20end%20of%20your%20query%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%7C%20summarize%20count()%20by%20Mail%2C%20User_Name%2C%20Application_name%2C%20Protocol%2C%20tostring(Device.displayName)%2C%20tostring(Device.operatingSystem)%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHere%20is%20a%20link%20to%20'summarize'%20documentation%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fdata-explorer%2Fkusto%2Fquery%2Fsummarizeoperator%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fdata-explorer%2Fkusto%2Fquery%2Fsummarizeoperator%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThanks%2C%3C%2FP%3E%0A%3CP%3ERafi%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Contributor

I have built a simple query to show the result of a specific conditional access policy, but I am struggling with device info. Cannot aggregate the info into one row. 

First, let's start with the most simple query and filter on the specific sign-in event (CorrelationId). 

 

 

 

SigninLogs
| where TimeGenerated > ago(1d)
| mvexpand PolicyResults = ConditionalAccessPolicies
| mvexpand Device = DeviceDetail
| where (PolicyResults.id == "####-004016656ca4" or PolicyResults.id == "####-a2e429a5325d")  and PolicyResults.result != "reportOnlyNotApplied"
|where CorrelationId == "#####-10c620ed816d"
|project Mail=UserPrincipalName, User_Name=UserDisplayName, Application_name=AppDisplayName, Protocol=ClientAppUsed, Device

 

 

It gives me such results:

q1.png

If I refer to specific properties in the Device and get them in separate columns, still got multiply raws as result. 

 

 

 

|project Mail=UserPrincipalName, User_Name=UserDisplayName, Application_name=AppDisplayName, Protocol=ClientAppUsed, Device.displayName, Device.operatingSystem

 

 

 

But still got results in multiply rows.
q2.png
I guess "summarize" can help but don't know how to use it. 
 
Any tips?
 
5 Replies

Hi @Michal_Z,

 

In order to summarize you first need to figure out what are the expected results, and how you want to summarize your results, I couldn't figure out from your description what is the desired result set.

 

For summarize you should use an aggregation function (if you don't use one the functionality is the same as the 'distinct' operator).

I'd suggest here the simplest function which is 'count()', it will give you the count of unique combinations occurrences of the columns mentioned on the right hand of the expression (after 'by'), try adding the following line to the end of your query:

| summarize count() by Mail, User_Name, Application_name, Protocol, tostring(Device.displayName), tostring(Device.operatingSystem)

 

Here is a link to 'summarize' documentation: https://docs.microsoft.com/en-us/azure/data-explorer/kusto/query/summarizeoperator

 

Thanks,

Rafi 

Hi @Rafi_Rabo 

Sorry if I were not clear about my goal and thank you for your tip. 

If I run a simple query:

 

 

SigninLogs
    |where CorrelationId == "###-#-#-10c620ed816d"

 

I got 36 rows as a result. They are all looking the same, referring to the same sign-in event, comes from the same device. They differ with some details about conditional access policy results. 
So what I was trying to achieve is filter out the desired policy ID from ConditionalAccessPolicies and list the user, app, and device details. 
And I wanted to present it that way, that if some of the rows are sign-ins from the same app and device, list it only once. 
The result of this query would be presented to the ServiceDesk, where they instruct the user which device and which app triggers the conditional access before they block the user. 
 
I hope it is more clear now. 
best response confirmed by Michal_Z (Contributor)
Solution

@Michal_Z 

 

instead of mv-expanding DeviceDetail (you're creating new rows) you should extend it to new columns, by using the extend operator, such as

 

| extend OS = DeviceDetail.operatingSystem, Browser = DeviceDetail.browser

@hspinto 

Thank you very much.

I have combined it to this (maybe not so elegant) query and it does what I wanted. 

SigninLogs
| where TimeGenerated > ago(14d)
| mvexpand PolicyResults = ConditionalAccessPolicies
| where PolicyResults.id == "e60d3233-####-004016656ca4" and PolicyResults.result != "reportOnlyNotApplied"
| extend OS = DeviceDetail.operatingSystem, Browser = DeviceDetail.browser,  displayName=tostring(DeviceDetail.displayName),deviceId= tostring(DeviceDetail.deviceId), trustType= tostring(DeviceDetail.trustType)
|summarize count() by   Mail=UserPrincipalName, User_Name=UserDisplayName, Application_name=AppDisplayName, Protocol=ClientAppUsed, displayName, deviceId, trustType , tostring(OS), tostring(Browser)

 

@Michal_Z 

 

FYI, https://docs.microsoft.com/en-us/azure/data-explorer/kusto/query/mvexpandoperator   

mv-expand has replaced mvexpand 

 

 Note

The legacy and obsolete form of the operator mvexpand has a default row limit of 128.