Denormalizing, grouping and aggregating data

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

Source systems are usually normalized: data is split across many narrow tables so nothing is stored twice. Analytics works the other way. Reports join, filter and total data constantly, so you reshape it into fewer, wider tables (denormalization) and pre-compute totals (aggregation). This article covers both steps and how to write them in Fabric.

Denormalizing

Denormalizing means storing redundant data deliberately to make queries simpler and faster. In a star schema it applies mainly to dimension tables. A product dimension, for instance, holds the product plus its subcategory and category columns, so a report never needs a join chain. Dimensions are small compared with fact tables, so the extra storage almost always costs less than the speed and ease of use gained. The same logic applies when you flatten nested JSON or an order header with its lines into a single wide table in a lakehouse.

There is a deliberate exception, the snowflake dimension, which stays normalized across related tables (product, subcategory, category). Consider it when:

  • the dimension is extremely large and storage dominates,
  • facts at a higher grain (for example targets by subcategory) need a key to relate to the hierarchy,
  • you must track history at higher levels.

A semantic model hierarchy can only use columns from a single table, so a snowflake dimension should be exposed through a view that joins the pieces into one denormalized result.

CREATE VIEW dbo.DimProductFlat AS
SELECT p.ProductKey, p.ProductName, s.SubcategoryName, c.CategoryName
FROM dbo.DimProduct p
JOIN dbo.DimSubcategory s ON s.SubcategoryKey = p.SubcategoryKey
JOIN dbo.DimCategory c ON c.CategoryKey = s.CategoryKey;

In PySpark the same step is a join that keeps the columns you want:

flat = (products.join(subcats, "subcategory_id").join(cats, "category_id")
        .select("product_id", "product_name", "subcategory_name", "category_name"))
flat.write.mode("overwrite").saveAsTable("dim_product_flat")

Remember to keep surrogate keys stable if the table is a dimension that facts point to; a full reload with regenerated keys breaks the facts.

Grouping and aggregating

Aggregation reduces rows to one per group. Every language has the same three parts: the grouping columns, the aggregate functions, and an optional filter on the aggregated result. Aggregating can also raise the grain of a fact table, for example by dropping the order number and grouping by all dimension keys.

PySpark

from pyspark.sql import functions as F
daily = (sales.groupBy("store_id", F.to_date("ts").alias("day"))
         .agg(F.sum("amount").alias("revenue"),
              F.countDistinct("customer_id").alias("customers")))

T-SQL

GROUP BY plus aggregates, with HAVING to filter groups. The warehouse also accepts ROLLUP, CUBE and GROUPING SETS for subtotals in one pass.

SELECT Region, Category, SUM(Amount) AS Revenue
FROM dbo.FactSales
GROUP BY ROLLUP (Region, Category)
HAVING SUM(Amount) > 1000;

ROLLUP adds subtotals by region and a grand total; CUBE adds subtotals for every combination; GROUPING SETS lets you list exactly which groupings you want.

KQL

summarize ... by is the main aggregation operator. Useful functions: count(), countif(), sum(), avg(), dcount() (an estimate of distinct values, trading exactness for speed), make_list() and make_set() (collect values into an array), and arg_max() (the row with the greatest value). Group by bin(Timestamp, 1h) for time buckets.

Sales
| summarize Revenue = sum(Amount), Customers = dcount(CustomerId) by Region, bin(Timestamp, 1d)

If the input is empty, summarize without a by clause returns one empty row, while with a by clause it returns none.

Common mistakes

  • Denormalizing a fact table's descriptive attributes into every row when a dimension would do.
  • Selecting non-grouped columns without an aggregate (an error in T-SQL; in Spark you must aggregate them).
  • Treating KQL dcount() as exact.
  • Filtering aggregates in WHERE instead of HAVING in T-SQL.
  • Counting NULLs by accident: COUNT(column) skips them, COUNT(*) does not.

For the exam

  • Denormalize dimensions (flatten hierarchies); keep a snowflake only for the reasons above and expose it via a view.
  • Aggregating can lower the number of dimension keys and raise the fact grain.
  • T-SQL ROLLUP, CUBE and GROUPING SETS produce subtotals; HAVING filters groups.
  • PySpark: groupBy().agg(). KQL: summarize ... by.
  • KQL dcount() is approximate; arg_max() returns a whole row.

Sources