Rob Gilliam
Rob Gilliam

Reputation: 2950

Is there a way to "flatten" KQL results into summary columns?

Given the following dataset, is there a simple/efficient way to produce a summary table in like the following using KQL, ideally without knowing the actual colours to be used in advance (i.e. column names are generated from the data values encountered)?

datatable ( name: string, colour: string )[
  "alice", "blue",
  "bob", "green",
  "bob", "blue",
  "alice", "red",
  "charlie", "red",
  "alice", "blue",
  "charlie", "red",
  "bob", "green"
]

+---------+------+-------+-----+
| name    | blue | green | red |
+---------+------+-------+-----+
| alice   |    2 |     0 |   1 |
| bob     |    1 |     2 |   0 |
| charlie |    0 |     0 |   2 |
+---------+------+-------+-----+

Upvotes: 1

Views: 190

Answers (1)

David דודו Markovitz
David דודו Markovitz

Reputation: 44941

Pivot plugin

datatable ( name: string, colour: string )[
  "alice", "blue",
  "bob", "green",
  "bob", "blue",
  "alice", "red",
  "charlie", "red",
  "alice", "blue",
  "charlie", "red",
  "bob", "green"
]
| evaluate pivot(colour, count(), name)
name blue green red
alice 2 0 1
bob 1 2 0
charlie 0 0 2

Fiddle

Upvotes: 1

Related Questions