Top Banner
www.sqlbi.com
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

Oct 09, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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

www.sqlbi.com

Page 2: 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

Relationships in DAX

Alberto FerrariSenior consultant @[email protected]

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

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

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

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

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

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

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 9: 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

Blank rows

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

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

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

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 13: 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

Ambiguity

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

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

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

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

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

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

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 20: 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

Dependencies

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

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

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

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 24: 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

Weak relationships

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

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

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

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

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

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

Thank you!

Check our articles, whitepapers and courses on

www.sqlbi.com