Transforming data with PySpark, SQL and KQL

By Hatnest · Reviewed by Saurav Raj Joshi · Updated Nov 30, 2026

Three languages cover most Fabric transformations, and each is tied to an engine. PySpark and Spark SQL run on Spark in notebooks, mostly against lakehouse Delta tables. T-SQL runs in a warehouse. KQL runs in an eventhouse (KQL database). The concepts are the same; the skill is knowing which engine holds your data and how each language expresses the step.

PySpark and Spark SQL

In a notebook you can mix languages by starting a cell with a magic command: %%pyspark for Python and %%sql for Spark SQL. A DataFrame written in Python and a SQL query over the same table do the same work, so choose whichever the team reads best. Write results back as a Delta table with saveAsTable or write.format("delta"), using mode("overwrite") or mode("append").

from pyspark.sql import functions as F

orders = spark.read.table("bronze_orders")
customers = spark.read.table("dim_customer")

result = (orders
    .filter(F.col("status") == "shipped")                       # filter
    .withColumn("net", F.col("amount") - F.col("discount"))     # derive
    .join(customers, "customer_id", "left")                     # join
    .groupBy("region")                                          # aggregate
    .agg(F.sum("net").alias("total_net")))

result.write.mode("overwrite").saveAsTable("gold_sales_by_region")
%%sql
SELECT c.region, SUM(o.amount - o.discount) AS total_net
FROM bronze_orders o LEFT JOIN dim_customer c ON c.customer_id = o.customer_id
WHERE o.status = 'shipped'
GROUP BY c.region

T-SQL in a warehouse

The warehouse offers CREATE TABLE AS SELECT, SELECT ... INTO, INSERT ... SELECT and MERGE. Sources can be warehouse tables, lakehouse tables read by three-part name, or files read with OPENROWSET. Cast explicitly in CTAS and SELECT INTO, because column types are inferred from the query result.

CREATE TABLE dbo.GoldSales AS
SELECT c.Region, SUM(o.Amount - o.Discount) AS TotalNet
FROM SilverLakehouse.dbo.Orders AS o
LEFT JOIN dbo.DimCustomer AS c ON c.CustomerID = o.CustomerID
WHERE o.Status = 'shipped'
GROUP BY c.Region;

KQL in an eventhouse

KQL is a pipeline: each operator takes the table from the one before, joined by |. Learn these:

  • where filters rows; project and project-away choose or drop columns; extend adds calculated columns.
  • summarize <aggregation> by <group> aggregates. Use bin() on a time column to bucket time.
  • join defaults to the innerunique flavor, which de-duplicates the left side before an inner join. Say kind=inner or kind=leftouter explicitly when you need all matching rows.
  • lookup enriches a fact table with values from a dimension table, a convenient left-outer style for star-schema work.
  • parse pulls fields out of a free-text string into columns; parse_json turns JSON text into a dynamic value; mv-expand turns each element of an array into its own row.
  • arg_max(col, *) inside summarize returns the whole row with the greatest value, which is the usual way to get the latest record per key.
Events
| where Level == "Error"
| extend Minute = bin(Timestamp, 1m)
| summarize Errors = count(), arg_max(Timestamp, Message) by Minute, Service
| lookup (ServiceOwners) on Service

Transform on arrival: update policy

An update policy runs automatically whenever new data lands in a source table. It executes a query over the new data and writes the result to a target table, so no orchestration is needed. A typical use is parsing a free-text column with parse into a clean typed table. Several policies can hang off one table, and each target can have its own schema and retention.

Choosing

Data in a lakehouse and complex logic: PySpark. Data in a warehouse and a SQL team: T-SQL. Event data and time-series questions, or shaping data as it is ingested: KQL, often via an update policy.

Common mistakes

  • Leaving KQL join on the default flavor and silently losing duplicate left rows.
  • Forgetting that Spark overwrite replaces the whole table, not just new rows.
  • Casting nothing in CTAS and getting unexpected inferred types.
  • Aggregating in KQL without bin() on a time column, producing one row per distinct timestamp.

For the exam

  • %%pyspark and %%sql switch the language of a notebook cell.
  • CTAS, SELECT INTO, INSERT ... SELECT and MERGE are the warehouse transformation statements.
  • KQL: summarize ... by, extend, project, parse, mv-expand, lookup, arg_max.
  • KQL join defaults to innerunique.
  • An update policy transforms data as it is ingested into a table.

Sources