Sub topic 14
Validating Results and Watching Performance
This chapter assumes you can build a bridge table between Customers and Accounts, set its relationships, and write basic DAX with CALCULATE. Here you check that the numbers are right and keep the model fast.
Why validation matters
Many-to-many models rarely fail with an error. They return numbers that look plausible and are wrong. When an account belongs to several customers, a report that lists customers and sums their amounts counts that account's transactions once for each customer. The rows add up to more than the real total.
Validate every many-to-many report before you share it.
Check 1: Compare against the unfiltered total
Start with a base measure on the fact table:
Total Amount = SUM ( Transactions[Amount] )Put Total Amount in a card with no filters. This is the true total. Then put Amount via Bridge in a table by customer and compare the sum of the visible rows with the card. If the rows add up to more, accounts are shared and some amounts are counted more than once.
Check 2: Measure the overlap
A validation measure can report the difference directly:
Rows Check =
VAR GrandTotal = CALCULATE ( [Total Amount], ALL ( Customers ) )
VAR VisibleSum = SUMX ( VALUES ( Customers[CustomerID] ), [Amount via Bridge] )
RETURN VisibleSum - GrandTotalGrandTotal ignores the customer filter. VisibleSum adds the per-customer results. The result is the amount counted more than once. It is zero when each account belongs to only one customer.
Check 3: Confirm the bridge keys are unique
Duplicate rows in the bridge multiply results. Build the bridge with DISTINCT, as in the earlier chapter:
AccountBridge =
DISTINCT (
SELECTCOLUMNS (
AccountCustomer,
"AccountID", AccountCustomer[AccountID],
"CustomerID", AccountCustomer[CustomerID]
)
)Also confirm that every AccountID in Transactions exists in Accounts. A missing key adds a blank row to the dimension, and those transactions show up under a blank label.
Which measure to trust
Compare the two DAX approaches on the same visual. They should agree. If they do not, check the relationship direction first.
Amount for Selected Customers =
CALCULATE (
SUM ( Transactions[Amount] ),
TREATAS ( VALUES ( AccountBridge[AccountID] ), Transactions[AccountID] )
)If Amount via Bridge and Amount for Selected Customers differ for a customer, one of them is being filtered by something unexpected. Common causes are a relationship that is inactive, a direction set to single, or a slicer on the Accounts table that the other measure ignores.
Watching performance
Each choice has a cost.
| Choice | Performance effect |
|---|---|
| Bidirectional filter on a relationship | Filters travel further, so queries do more work. Can create ambiguous paths. |
| `CROSSFILTER` in a measure | Applies only where used. Cheaper than a model-wide bidirectional setting. |
| `TREATAS` over many values | Builds a filter from a list. Slow when the list is large. |
| Native many-to-many relationship | Evaluated at query time. Slower than a bridge on large tables. |
| Large bridge table | More rows to scan on each filter pass. |
To find slow measures, use Performance Analyzer in Power BI Desktop. Start recording, refresh the visual, and read the DAX query time. Copy the query into DAX Studio for a closer look.
Reducing cost
- Keep the bridge to the key columns only.
- Prefer
CROSSFILTERin a measure over a bidirectional relationship in the model. - Use integer keys instead of text keys.
- Filter early. Slicers on a dimension reduce the rows that reach the bridge.
Exercise
A table of customers shows Amount via Bridge for each customer, and the visible rows sum to 1,250,000. The unfiltered Total Amount is 1,000,000. What does this tell you, and what does Rows Check return?
Show answer
Accounts are shared by several customers, so some transactions are counted under more than one customer. Rows Check returns 250,000, which is the amount counted more than once. The per-customer values can still be correct, but the sum of the rows should not be presented as the overall total.
Exercise
Why is CROSSFILTER in a measure usually better for performance than setting a bidirectional relationship in the model?
Show answer
The bidirectional setting applies to every query that touches the relationship, and it can create ambiguous filter paths. CROSSFILTER changes the direction only inside the one calculation that needs it.