• azurecoder

Kusto Lightning Fact 3: .create function

In the previous lightning fact we looked at using variables through the let command. In this tutorial we'll look at using functions which give us a a bunch of reusability.

.create function ifnotexists 
with (docstring = 'A simple function to get aggregates of countries', folder='CountryFunctions')
AllCountrySummaryState()  {
sales 
| project Country, Profit = Total_Profit / 1000000, Earnings = Total_Revenue / 1000000, Year = getyear(Order_Date)
| summarize Earnings = round(sum(Earnings)), Profit = round(sum(Profit)), TransactionCountPerYear = count() by Country, Year
| order by Country, Year asc
| extend EarningsRatio = round((Profit / Earnings) * 100, 2)
}

This will create a function if it doesn't already exist with a description and putting it into a folder called CountryFunctions.


There's a few interesting caveats in this function. We get profit, earnings per country per year + the number of transactions in each year. From this we can create an earnings ratio to see how much variance there has been over the years in earnings per country.


You can also use .drop function to drop the function as you would expect.


A feature here is also the keyword extend which allows you to add to the project query without projecting out everything else again.

81 views0 comments

Recent Posts

See All

I had a great time last weekend with a garden party with my friend and one of my longstanding tech leads David. It was great to spend the afternoon with friends and work colleagues. Great food, great

I've taken the last week off. For the first time in about a decade I haven't responded to emails (at least I've picked and chosen), Teams messages (same) and I've signed out of all of my customer acco

I was mulling over whether to derive a new jocular term for a Data Mesh. I pondered Data Mess but that seemed to obvious so I've opted for Data Smoosh which isn't as poetic but kind of reminds me of S