SOLVED

Pivot sort

Iron Contributor

Using the "pivot" command, there doesn't appear to be a way to sort the columns... am I wrong? 

 

For example:

Table

| project Column_A, SomeValue

| where SomeValue > 0

| evaluate pivot(Column_A, sum(SomeValue))

In this example, the columns pivoted from "Column_A" will show up in no discernible order. Is there a way to sort the columns?

 

8 Replies

Hi Scott,

I believe you're right and the order is not controlled. I'll forward that question to verify with the developers.

Also, please note the documentation of pivot mentions that since the resulting schema is based on  changing result-sets, it is not a stable source for automation processes:

"The output schema of the pivot plugin is based on the data and therefore query may produce different schema for any two runs. This also means that query that is referencing unpacked columns may become 'broken' at any time. Due to this reason - it is not advised to use this plugin for automation jobs."

Interesting. I'm using this for a dashboard tile at the moment.

Hey, verified that and indeed no way to control the order of the pivot-based columns :\

Enhancement request! :)

@Scott Allison 

Seconding the enhancement request, and thank you for this post as it was exactly what I was looking for!

best response confirmed by Scott Allison (Iron Contributor)
Solution

@hdsheena , @Scott Allison  - you can now use project-reorder to set the order you want, and if column names are not known (since the data set here is dynamic) you can use asc / desc.

@Noa Kuperberg,

 

Working with similar kind of problem with below query:

 

availabilityResults
| where timestamp < now(5d)
| summarize successCount = count(success) by bin(timestamp, 1h)
| project date_of_month = format_datetime(timestamp, 'yyyy-MM-dd'), hour = strcat("Hour", datetime_part("hour", timestamp)), successCount
| evaluate pivot(hour, sum(successCount))
 
Here I gets result like below (column names)

date_of_month  |  Hour0  |  Hour1  |  Hour10  |  Hour11 ....... Hour9

 

What I expect:

date_of_month  |  Hour0  |  Hour1  |  Hour2  |  Hour3 ...... Hour23

 

If I try to use column name like Hour0.... Hour23 getting below error 

 

project-reorder: Failed to resolve attribute as a column entity: Hour0

@Akash_Hande 

 

You can use granny_asc now https://docs.microsoft.com/en-gb/azure/data-explorer/kusto/query/projectreorderoperator

 

Heartbeat
| summarize successCount = count(Computer) by bin(TimeGenerated, 1h)
| project date_of_month = format_datetime(TimeGenerated, 'yyyy-MM-dd'), hour = strcat("Hour", datetime_part("hour", TimeGenerated)), successCount
| evaluate pivot(hour, sum(successCount))
| project-reorder date_*, Hour* granny-asc 

 

Result:

Clive_Watson_0-1643279363116.png

 

 

1 best response

Accepted Solutions
best response confirmed by Scott Allison (Iron Contributor)
Solution

@hdsheena , @Scott Allison  - you can now use project-reorder to set the order you want, and if column names are not known (since the data set here is dynamic) you can use asc / desc.

View solution in original post