Relationships in DAX - Microsoft · Weak relationships o Both sides of a relationship can be the...
Transcript of Relationships in DAX - Microsoft · Weak relationships o Both sides of a relationship can be the...
![Page 3: Relationships in DAX - Microsoft · Weak relationships o Both sides of a relationship can be the many side •They are not many-to-many relationships •They are relationships at](https://reader033.fdocuments.in/reader033/viewer/2022052101/603afaf13a413100982b5c55/html5/thumbnails/3.jpg)
We write
BooksWe teach
CoursesWe provide
ConsultingWe are recognized
BI Experts
Remote
Consulting
Power BI/SSAS
Optimization
BI Architectural
Review
On-Site
Consulting
Custom Training
& Mentoring www.sqlbi .com
![Page 4: Relationships in DAX - Microsoft · Weak relationships o Both sides of a relationship can be the many side •They are not many-to-many relationships •They are relationships at](https://reader033.fdocuments.in/reader033/viewer/2022052101/603afaf13a413100982b5c55/html5/thumbnails/4.jpg)
Agenda
o Basic concepts about relationships
o Invalid relationships and the blank row
o Bidirectional filtering and ambiguity
o Circular dependencies
o Weak relationships and computing at the correct
granularity
o Take-away: do not use features that you do not totally
master
![Page 5: Relationships in DAX - Microsoft · Weak relationships o Both sides of a relationship can be the many side •They are not many-to-many relationships •They are relationships at](https://reader033.fdocuments.in/reader033/viewer/2022052101/603afaf13a413100982b5c55/html5/thumbnails/5.jpg)
Relationships 101
Related Source
Cross-Filter
Cardinality
![Page 6: Relationships in DAX - Microsoft · Weak relationships o Both sides of a relationship can be the many side •They are not many-to-many relationships •They are relationships at](https://reader033.fdocuments.in/reader033/viewer/2022052101/603afaf13a413100982b5c55/html5/thumbnails/6.jpg)
Cardinality
o One
• Column needs unique values
• Target of table expansion
• Source for filter context propagation
o Many
• Column may contain duplicates
• Source of table expansion
• Target for filter context propagation
![Page 7: Relationships in DAX - Microsoft · Weak relationships o Both sides of a relationship can be the many side •They are not many-to-many relationships •They are relationships at](https://reader033.fdocuments.in/reader033/viewer/2022052101/603afaf13a413100982b5c55/html5/thumbnails/7.jpg)
Cross-filter direction
o Single
• Filter context propagates from the one side to the many
side
• Default behavior
• Safe, fast, convenient
o Both
• Filter context propagates in both directions
• Need to be activated
• Unsafe, slow, extremely dangerous
![Page 8: Relationships in DAX - Microsoft · Weak relationships o Both sides of a relationship can be the many side •They are not many-to-many relationships •They are relationships at](https://reader033.fdocuments.in/reader033/viewer/2022052101/603afaf13a413100982b5c55/html5/thumbnails/8.jpg)
Types of relationships
o One-to-many
• The most common type of relationship
o One-to-one
• Quite uncommon
• Expansion goes both ways
• Cross-filter need to be both
o Weak relationships (many-to-many cross-filter)
• New and dangerous
• No expansion
• Need to decide the cross-filter direction
![Page 10: Relationships in DAX - Microsoft · Weak relationships o Both sides of a relationship can be the many side •They are not many-to-many relationships •They are relationships at](https://reader033.fdocuments.in/reader033/viewer/2022052101/603afaf13a413100982b5c55/html5/thumbnails/10.jpg)
The additional blank row
ProductId Product
1 Coffee
2 Pasta
3 Tomato
Amount ProductId
25.00 1
12.50 2
2.25 3
2.50 3
14.00 4
16.00 5
Relationship
BLANK BLANK
Tables with an incoming relationship might contain an additional blank row, created by DAX to guarantee referential integrity
![Page 11: Relationships in DAX - Microsoft · Weak relationships o Both sides of a relationship can be the many side •They are not many-to-many relationships •They are relationships at](https://reader033.fdocuments.in/reader033/viewer/2022052101/603afaf13a413100982b5c55/html5/thumbnails/11.jpg)
Counting values
o DISTINCT / VALUES
• DISTINCT does not return the blank row
• VALUES returns the blank row
o ALL / ALLNOBLANKROW
• ALLONOBLANKROW does not return the blank row
• ALL returns the blank row
o Need to use the right function, depending on the need
o Using the wrong one, simply results in wrong figures
![Page 12: Relationships in DAX - Microsoft · Weak relationships o Both sides of a relationship can be the many side •They are not many-to-many relationships •They are relationships at](https://reader033.fdocuments.in/reader033/viewer/2022052101/603afaf13a413100982b5c55/html5/thumbnails/12.jpg)
Iterating, might be harder than expected
Does this iteration take into account the blank row, or not?
What is the average sales of the blank row?
Avg Sales :=
AVERAGEX (Product,[Sales Amount]
)
![Page 14: Relationships in DAX - Microsoft · Weak relationships o Both sides of a relationship can be the many side •They are not many-to-many relationships •They are relationships at](https://reader033.fdocuments.in/reader033/viewer/2022052101/603afaf13a413100982b5c55/html5/thumbnails/14.jpg)
Ambiguity
• Multiple paths between any two tables
• DAX does not work on ambiguous models
• Therefore, one of the relationships is deactivated
![Page 15: Relationships in DAX - Microsoft · Weak relationships o Both sides of a relationship can be the many side •They are not many-to-many relationships •They are relationships at](https://reader033.fdocuments.in/reader033/viewer/2022052101/603afaf13a413100982b5c55/html5/thumbnails/15.jpg)
Ambiguity and bidirectional cross-filter
o Do you love bidirectional relationship? Better not
o Bidirectional cross-filter increases the chances of
ambiguity
o Filter propagation always goes both ways
o Therefore, the complexity increases on every relationship
![Page 16: Relationships in DAX - Microsoft · Weak relationships o Both sides of a relationship can be the many side •They are not many-to-many relationships •They are relationships at](https://reader033.fdocuments.in/reader033/viewer/2022052101/603afaf13a413100982b5c55/html5/thumbnails/16.jpg)
Is this an ambiguous model?
![Page 17: Relationships in DAX - Microsoft · Weak relationships o Both sides of a relationship can be the many side •They are not many-to-many relationships •They are relationships at](https://reader033.fdocuments.in/reader033/viewer/2022052101/603afaf13a413100982b5c55/html5/thumbnails/17.jpg)
Things are more complex in a larger model
Few questions:
• Does Date filter Sales?
• Does Customer filter
Purchases?
• Does Date filter
Purchases?
• Which subset of sales
is actually filtering
Purchases?
![Page 18: Relationships in DAX - Microsoft · Weak relationships o Both sides of a relationship can be the many side •They are not many-to-many relationships •They are relationships at](https://reader033.fdocuments.in/reader033/viewer/2022052101/603afaf13a413100982b5c55/html5/thumbnails/18.jpg)
What about a real model?
![Page 19: Relationships in DAX - Microsoft · Weak relationships o Both sides of a relationship can be the many side •They are not many-to-many relationships •They are relationships at](https://reader033.fdocuments.in/reader033/viewer/2022052101/603afaf13a413100982b5c55/html5/thumbnails/19.jpg)
Never work with an ambiguous model
o Ambiguity is not always evident
• The engine might consider as non-ambiguous a model
that is ambiguous
• The disambiguation rules have never been published
• Adding a table might completely change the relationships
setup
o Bidirectional cross-filter is the major culprit
• Don’t use it just “because it looks nicer to the user”
• Wrong values are quite never nice
![Page 21: Relationships in DAX - Microsoft · Weak relationships o Both sides of a relationship can be the many side •They are not many-to-many relationships •They are relationships at](https://reader033.fdocuments.in/reader033/viewer/2022052101/603afaf13a413100982b5c55/html5/thumbnails/21.jpg)
Calculated relationships
o Relationships can be created on calculated columns
• Performance-wise, there are no issues
• Model-wise: they are extremely powerful tools
• Good chances of creating circular dependencies
o Circular dependencies appear because of the blank row
![Page 22: Relationships in DAX - Microsoft · Weak relationships o Both sides of a relationship can be the many side •They are not many-to-many relationships •They are relationships at](https://reader033.fdocuments.in/reader033/viewer/2022052101/603afaf13a413100982b5c55/html5/thumbnails/22.jpg)
Circular dependencies
DAX Code
Blank row
![Page 23: Relationships in DAX - Microsoft · Weak relationships o Both sides of a relationship can be the many side •They are not many-to-many relationships •They are relationships at](https://reader033.fdocuments.in/reader033/viewer/2022052101/603afaf13a413100982b5c55/html5/thumbnails/23.jpg)
Main sources of circular dependencies
o Using ALL instead of ALLNOBLANKROW
o Using VALUES instead of DISTINCT
o Context transition happening
o Filters in CALCULATE
o Same considerations if you use calculated tables
![Page 25: Relationships in DAX - Microsoft · Weak relationships o Both sides of a relationship can be the many side •They are not many-to-many relationships •They are relationships at](https://reader033.fdocuments.in/reader033/viewer/2022052101/603afaf13a413100982b5c55/html5/thumbnails/25.jpg)
Continent and islands
VertiPaq Continent
Dataset Island
Dataset Island
![Page 26: Relationships in DAX - Microsoft · Weak relationships o Both sides of a relationship can be the many side •They are not many-to-many relationships •They are relationships at](https://reader033.fdocuments.in/reader033/viewer/2022052101/603afaf13a413100982b5c55/html5/thumbnails/26.jpg)
Weak relationships
o Both sides of a relationship can be the many side
• They are not many-to-many relationships
• They are relationships at different granularities
o Useful if the column is not a key in both tables
• Otherwise, a regular strong 1:M relationship works well
o Mandatory, for cross-island relationships
o Need to choose the cross-filter direction
• Bidirectional, A filters B, B filters A
o Table expansion does not happen, blank row is not enforced
![Page 27: Relationships in DAX - Microsoft · Weak relationships o Both sides of a relationship can be the many side •They are not many-to-many relationships •They are relationships at](https://reader033.fdocuments.in/reader033/viewer/2022052101/603afaf13a413100982b5c55/html5/thumbnails/27.jpg)
Weak: no expansion, no blank row
Slice by Product[Brand] Slice by Budget[Brand]
![Page 28: Relationships in DAX - Microsoft · Weak relationships o Both sides of a relationship can be the many side •They are not many-to-many relationships •They are relationships at](https://reader033.fdocuments.in/reader033/viewer/2022052101/603afaf13a413100982b5c55/html5/thumbnails/28.jpg)
Relationship cardinality
o Weak relationship transfer filters at their cardinality
• They do not use the table cardinality
• Browsing with other attributes leads to complex numbers
The value shown is the budget of any brand that contains at least one product of the selected category
In short: it does not make sense
![Page 29: Relationships in DAX - Microsoft · Weak relationships o Both sides of a relationship can be the many side •They are not many-to-many relationships •They are relationships at](https://reader033.fdocuments.in/reader033/viewer/2022052101/603afaf13a413100982b5c55/html5/thumbnails/29.jpg)
Conclusions
o Standard (strong) 1:M relationships are fast and safe
o Bidirectional cross-filter is dangerous
• Performance issues
• Ambiguity, might be very well hidden
o Weak relationships are even more dangerous
• No blank row
• No table expansion
• Filter moved at the key granularity
o All powerful features
o Don’t use all of them until you completely master them
![Page 30: Relationships in DAX - Microsoft · Weak relationships o Both sides of a relationship can be the many side •They are not many-to-many relationships •They are relationships at](https://reader033.fdocuments.in/reader033/viewer/2022052101/603afaf13a413100982b5c55/html5/thumbnails/30.jpg)
Thank you!
Check our articles, whitepapers and courses on
www.sqlbi.com