How to create calculated columns with extend in KQL
The extend operator in KQL adds calculated columns to a table, based on the columns you already have, without changing the source data. It is ideal for producing totals, percentages, margins or durations at query time, and it is one of the first skills worth mastering when you work with Real-Time Analytics. Below is a simple, ready-to-copy example that shows how to create these columns step by step.
Prerequisites
- A KQL environment: a KQL Queryset in Microsoft Fabric, a database in Azure Data Explorer, or the public help cluster.
- Knowing how to run a simple query, for example filtering rows with the
whereoperator. - Nothing to install: the example creates its own data.
Step 1: Prepare sample data
So that you do not depend on any existing table, we will create sample data with the datatable operator. Paste and run this query in your KQL editor:
let Vendas = datatable(Produto:string, Preco:real, Quantidade:int)
[
"Teclado", 25.0, 4,
"Rato", 15.0, 10,
"Monitor", 120.0, 2
];
Vendas
You should see three rows with the Produto, Preco and Quantidade columns. We will reuse this Vendas table in all the following steps.
Step 2: Create your first calculated column
Now we will calculate the total value of each row, that is price times quantity, and store the result in a new column called Total. The extend syntax is always the same: the name of the new column, an equals sign, and the expression that calculates it.
let Vendas = datatable(Produto:string, Preco:real, Quantidade:int)
[
"Teclado", 25.0, 4,
"Rato", 15.0, 10,
"Monitor", 120.0, 2
];
Vendas
| extend Total = Preco * Quantidade
The result keeps the original columns and adds the Total column at the end. Notice that the Vendas table is not modified: extend only affects the result of this query, never the stored data.
Tip: useextendwhen you want to add columns. If you only need to pick or rename columns, the right operator isproject.
Step 3: Add several columns at once
You can create more than one column in the same extend, separating the expressions with commas. In the next example we add the total with tax and a text label (keep the let Vendas line from Step 1 at the top of the query):
Vendas
| extend Total = Preco * Quantidade,
ComIVA = (Preco * Quantidade) * 1.23,
Origem = "loja online"
Each row now has three new columns. As you can see, you can mix numeric calculations with quoted text values in the same statement.
Step 4: Reuse the calculated column in the pipeline
A big advantage of extend is that the new column becomes immediately available in the following steps. For example, we can use the new Total column to keep only the highest sales:
Vendas
| extend Total = Preco * Quantidade
| where Total > 120
Only the Rato (150) and Monitor (240) rows remain; the Teclado row (100) is removed. Without extend, you could not filter by a value that did not yet exist in the original table.
Check the result
To confirm everything is correct, check three things: the new columns appear to the right of the original ones; the Total values match price times quantity; and, in Step 4, the result shows only two rows. If you get a "column not found" error, make sure you typed the column names exactly as in the datatable, because KQL is case sensitive.
Conclusion
With the extend operator you can now enrich any KQL query with calculated columns, ready to use in filters, sorting or charts. A good next step is to combine extend with summarize to aggregate those new columns and build metrics. Which calculated column would be most useful for your own data?