Daniel Dror
Daniel Dror

Reputation: 2507

How to generate a markdown from a Kusto (ADX) query result

I'm trying to build dashboard based on a KQL query. The dashboarding tool I use knows how to render markdown, but I don't know how to generate a markdown based on query results within Kusto (without using external tooling).

Upvotes: 3

Views: 1716

Answers (1)

Daniel Dror
Daniel Dror

Reputation: 2507

I've made a utility function to create a markdown based on a query result.

.create-or-alter function with (folder = "formatting", docstring = "Table to Markdown", skipvalidation = "true") TableToMarkdown(t:(*)) {
let schema = t | getschema;
let headers = schema | project ColumnName | summarize make_list(ColumnName) | extend String = strcat('| ', strcat_array(list_ColumnName, ' | '), ' |') | project String, Order=1;
let upperDivider = schema | project ColumnName, Sep = '---' | summarize Cols=make_list(Sep) | extend String = strcat('| ', strcat_array(Cols, ' | '), ' |') | project String, Order=2;
let data = t | extend Cols=pack_array(*) | extend String = strcat('| ', strcat_array(Cols, ' | '), ' |') | project String, Order=3;
headers 
| union upperDivider
| union data
| order by Order asc 
| summarize Rows=make_list(String) 
| project array_strcat(Rows, '\r\n')
} 

Which can then be invoked for a query like so:

let T = print x=1, y=2;
T | invoke TableToMarkdown()

Upvotes: 5

Related Questions