Sub topic 16
Anti-patterns to avoid and how to fix them
This chapter assumes you can open Model view in Power BI Desktop, read basic DAX, and know what primary and foreign keys are. It also assumes you have met the star schema, one-to-many relationships, and filter flow from earlier chapters. Here you will learn to spot the mistakes that most often undo those ideas, and how to repair each one.
The single wide table
The most common anti-pattern is loading one flat table that mixes order facts with product, customer, and date attributes. Text columns repeat on every row, so the model grows and scans slow down. Filters also behave oddly, because "distinct products" depends on which rows happen to exist.
The fix is to split the table. Keep the keys and numbers in a fact table at a declared grain. Move descriptive columns into dimensions. This query builds a product dimension from the flat table:
let
Source = SalesFlat,
ProductColumns = Table.SelectColumns(Source, {"ProductID", "ProductName", "Subcategory", "Category"}),
DimProduct = Table.Distinct(ProductColumns, {"ProductID"})
in
DimProductAfter that, remove the product text columns from the fact query so only ProductID remains as the key.
Unnecessary bidirectional filters
A bidirectional relationship lets filters flow from the fact table back to the dimension. People turn it on to make a slicer "react" to another slicer. The cost is ambiguous filter paths, slower queries, and results that change when you add a new table.
Set relationships to single direction, from dimension to fact. If a slicer must show only values that have sales, filter the visual with a measure such as Total Sales greater than zero instead of changing the relationship.
Other mistakes to look for
| Anti-pattern | Symptom | Fix |
|---|---|---|
| Relating on a text column that is not unique | Duplicate-value errors or wrong totals | Build a dimension with a unique key and relate on that |
| Using the fact table's own date column in visuals | Missing days, no month ordering | Build a date dimension and relate on `DateKey` |
| Duplicating the date table for each date column | Many near-identical tables | Keep one date table and use an inactive relationship |
| Many-to-many relationships between two facts | Totals that look inflated | Add a shared dimension between them |
| Snowflaked dimensions with long relationship chains | Slow filters, a cluttered model | Flatten the chain into one dimension |
Reusing one date table for several dates
When a fact table has both an order date and a ship date, do not clone the date table. Keep one active relationship for the main date and one inactive relationship for the other. Then activate the inactive one inside a measure:
Sales by Ship Date =
CALCULATE (
[Total Sales],
USERELATIONSHIP ( Sales[ShipDateKey], 'Date'[DateKey] )
)A quick review routine
- List each fact table and write its grain in one sentence.
- Confirm every relationship is one-to-many and single direction.
- Check that each dimension key is unique.
- Confirm one date table exists and is marked as the date table.
- Remove columns that no report or measure uses.
Exercise
A model has one flat table with 2 million rows, a bidirectional relationship from Sales to Customer, and two copies of the date table, one for order date and one for ship date. Name the three fixes you would make.
Show answer
Split the flat table into a fact table and dimensions, set the Sales to Customer relationship to single direction from dimension to fact, and replace the two date tables with one date table that has an active relationship for order date and an inactive one for ship date, used through USERELATIONSHIP.