KQL query for pivot output

Raj Bhat 21 Reputation points Microsoft Employee
2022-10-03T04:13:34.247+00:00

Hi, I'm new to KQL. I need to pivot the data in this detail data on multiple columns into a pivot format :

Detail Table:
246859-image.png

Pivot Table:
246883-image.png

Azure Data Explorer
Azure Data Explorer
An Azure data analytics service for real-time analysis on large volumes of data streaming from sources including applications, websites, and internet of things devices.
576 questions
0 comments No comments
{count} votes

Accepted answer
  1. ShaikMaheer-MSFT 38,546 Reputation points Microsoft Employee Moderator
    2022-10-06T10:47:19.827+00:00

    Hi @Raj Bhat ,

    Thank you for posting query in Microsoft Q&A Platform.

    Kindly use below query. I tried its working fine.

    //created a table with sample data.  
      
    .create table SampleTable(  
    Name:string, OSStatus:string, DBLevel:string, DBStatus:string)  
      
    // inserting sample data in to table  
    .ingest inline into table SampleTable <|  
    ABC,OSStatus-Completed,Hourly,Fail  
    ABC,Completed,Incr,Fail  
    XYZ,Completed,Incr,Success  
    XYZ,Completed,Hourly,Success  
      
    //Actual query for pivoting table  
    let OSStatusPivot = SampleTable  
    | evaluate pivot(OSStatus, count(), Name):(Name:string, Completed:string, Failed:string )  
    | project Name, ["OSStatus-Completed"]=Completed, ["OSStatus-Failed"]=Failed;  
    let DBStatusPivot = SampleTable  
    | project Name, DBStatus=strcat('DBStatus-',DBLevel, '-' ,DBStatus)  
    | evaluate pivot(DBStatus, count()):  
            (Name:string ,   
            ["DBStatus-Hourly-Fail"]:string,  
            ["DBStatus-Hourly-Success"]:string,  
            ["DBStatus-Incr-Fail"]:string,  
            ["DBStatus-Incr-Success"]:string,  
            ["DBStatus-Full-Success"]:string,  
            ["DBStatus-Full-Fail"]:string);  
    OSStatusPivot | join  DBStatusPivot on Name  
    

    248019-image.png

    Hope this helps.

    ------------

    Please consider hitting Accept Answer and up-vote button. Accepted answers help community as well.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Spencer Cruz 6 Reputation points
    2022-10-03T04:50:11.083+00:00

    Hello, I personally don't know KQL however I did find this article that may help structure your query!

    https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/pivotplugin

    Hopefully someone with more experience can provide a actual example of what you're looking for! :)

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.