MSBA-261 · Lesson 2 of 12 · 22 min

Relationships and cardinality

▶ Video coming soon A video version of this lesson is on the way.

With the tables loaded, Kwabena opened the model view of Power BI Desktop and found that Power BI had already drawn some lines between them. Autodetect had matched CampusCode in Registration to CampusCode in Campus, and ProgramCode to ProgramCode. It had also related the Outlet table to Registration through a column both happened to call LocationID, which made no sense at all. "Autodetect is a guess," Signe Tavernier-Mbeki told him. "Check every line it draws, and delete the ones you didn't design." This lesson covers what those lines mean, how filters travel along them, and how to choose cardinality and cross-filter direction on purpose.

What a relationship does

A relationship in a Power BI model connects a column in one table to a column in another. Its main job is to propagate filters. When a reader selects Lakeshore in a Campus slicer, the Campus table is filtered to one row. The relationship from Campus to Registration then filters Registration to the rows whose CampusCode is LKS, and every measure that adds up Registration sees only those rows. Without the relationship, the slicer would change nothing in an Registration visual.

Relationships are not joins that run when data is loaded. The tables stay separate in storage, and the engine uses the relationships at query time to decide which rows each calculation sees. That is why the same model can answer "tuition by campus" and "tuition by program" without storing a separate joined table for each question.

Cardinality

Each relationship has a cardinality, which describes how many rows on each side can match.

CardinalityMeaningTypical useLumenvale example
One-to-many (1:*)Each value is unique on the one side and can repeat on the many sideDimension to fact; by far the most commonCampus to Registration
Many-to-one (*:1)The same relationship seen from the other endFact to dimensionRegistration to Program
One-to-one (1:1)Values are unique on both sidesRare; often a sign that two tables should be mergedStudent to a student privacy-flag table
Many-to-many (*:*)Values can repeat on both sidesFacts and dimensions at different grains, or bridge situationsProgram Budget (by program and month) to a month-level table

In a well-designed star schema nearly every relationship is one-to-many, from the dimension's unique key to the fact table's foreign key. Power BI detects the cardinality from the data when you create the relationship, but you should confirm it. If Power BI proposes many-to-many between Program and Registration, it is telling you something important: the Program table contains duplicate program codes. That usually means a data problem upstream, such as a program that appears twice with different names, and the right fix is in Power Query or the source, not in the relationship settings.

A one-to-one relationship deserves a second look too. Two tables related one-to-one describe the same things, so in most cases they should be merged into one table. Keeping them separate adds a relationship for every query to cross and gives authors two places to look for one entity's attributes.

Many-to-many relationships

Power BI supports relationships with many-to-many cardinality, where neither column needs unique values. They are useful when two tables genuinely relate at a coarser grain than either table's key. The Program Budget table is a good example. It has one row per program per fiscal month, so it cannot relate one-to-many to the Date table on a daily key. One approach is to relate it to a month-level column. Kwabena instead added a MonthKey column to the Date table and related Program Budget to Date on MonthKey with many-to-many cardinality and a single filter direction from Date to Program Budget.

Many-to-many relationships should be used knowingly. Totals can behave in ways readers do not expect if a filter on a daily column reaches a monthly fact: selecting 15 October shows the whole of October's budget, because the budget simply has no daily detail. Kwabena documented this in the measure descriptions and hid the daily columns from budget visuals using a separate report page.

A different situation, sometimes also called many-to-many, arises when one fact relates to several dimension members, such as a course section co-taught by two instructors. That pattern is usually modeled with a bridge table: a table with one row per section-instructor pair, related one-to-many to both Instructor and Course Section, so that filters can travel from Instructor through the bridge to the sections and on to Registration.

Cross-filter direction

Each relationship also has a cross-filter direction, which controls which way filters travel.

Two panels. In the Single panel, an arrow runs from Campus to Registration and from Program to Registration, so filters on dimensions reach the fact table but not each other. In the Both panel, arrows run both ways between Course Section and Registration, so a filter on Registration can reach Course Section.
Single direction lets dimensions filter facts. Both directions also lets a filtered fact table filter a dimension, which is occasionally useful and often risky.

With single direction, the default for one-to-many relationships, filters flow from the one side to the many side: from Campus to Registration. A filter on Registration does not flow back to Campus. With both directions, sometimes called bidirectional filtering, filters flow either way, so filtering Registration can also filter Campus, and from Campus on to any other table related to it.

Bidirectional filtering looks convenient. If Kwabena set the Course Section relationship to both directions, a Program slicer would show only the course sections that have enrollments in that program, because the filter would flow from Program to Registration and then back up to Course Section. But Microsoft's guidance is to use single direction by default and to switch on both directions only for a specific need, because:

  • Filters can reach tables by more than one route, creating ambiguity, which Power BI resolves by refusing to make some relationships active.
  • Each extra direction adds work to every query, which can slow large models.
  • Results become harder to explain: a number changes because of a filter that traveled a path nobody remembers designing.

When only one measure needs the behavior, a better option is to keep the relationship single and use the CROSSFILTER function inside that measure to change the direction for that one calculation. Slicers that should show only values with data can often use a visual-level filter instead, such as filtering the slicer to rows where a measure is not blank.

Changing filter direction for one measure only. The model relationship stays single direction.
Sections with Registrations =
CALCULATE (
    DISTINCTCOUNT ( 'Course Section'[SectionCode] ),
    CROSSFILTER ( Registration[SectionKey], 'Course Section'[SectionKey], BOTH )
)

Bidirectional relationships do have a proper home. A bridge table in a co-teaching design usually needs filters to pass from Instructor through the bridge to Course Section, which requires both directions on the bridge-to-Course Section relationship. Row-level security can also apply across a bidirectional relationship when the security filter setting allows it, a topic for the lesson on row-level security.

Active and inactive relationships

Only one active relationship can exist between any two tables at a time, and more broadly Power BI must find only one active path for filters between any two tables. Additional relationships between the same tables are created as inactive, shown as dashed lines in the model view. An inactive relationship does nothing until a measure switches it on with the USERELATIONSHIP function.

This rule matters for the problem Maribeth raised at the whiteboard. The Registration fact table has two dates: the date the student registered and the term start date. Both relate to the same Date table. Kwabena made the term start date relationship active, because most enrollment reporting follows terms, and created an inactive relationship on the registration date. The next lesson builds the measures that use it, a design called a role-playing dimension.

Referential integrity and blank rows

What happens if a fact row's key has no match in the dimension? Suppose a nightly extract includes a registration for program code HLT-PN2, which has not been added to the Program table yet. Power BI does not drop the row. Instead, the engine adds a virtual blank row to the Program dimension, and unmatched fact rows are grouped under it. In a visual, they appear under a blank program name. Totals still add up, but a blank row in a dean's report is a sign of a data quality problem.

Kwabena built a small audit page that listed fact rows whose program was blank, and the registrar's office used it each Monday to add missing programs. For DirectQuery models, a relationship property called Assume referential integrity lets Power BI use faster inner joins when every fact key is guaranteed to have a match; it should only be set when that guarantee is real.

Creating and editing relationships

Relationships can be created by dragging a column onto another in the model view, or from the Manage relationships dialog, which lists every relationship and lets you set the tables, columns, cardinality, cross-filter direction and whether the relationship is active. The relationship editor previews matching values and warns when cardinality does not fit the data. Relationships can also be edited in the web modeling experience in the Power BI service and as text in TMDL view, which lessons later in the course use.

Kwabena turned off the Desktop option that autodetects new relationships after data loads, so that adding a table would never quietly add a line he had not designed. He then reviewed the model view table by table and wrote each intended relationship in his design document.

Worked problem

Diagnosing an unexpected many-to-many

When Kwabena relates Course Section to Registration on SectionCode, Power BI proposes many-to-many cardinality. What should he check and do?

Show solution
  1. Many-to-many means SectionCode is not unique in Course Section.
  2. In Power Query, group Course Section by SectionCode and count rows to find duplicates.
  3. He finds that cross-listed sections appear once per campus with the same code. The true key is SectionCode plus TermCode.
  4. Create a SectionKey column that combines the term and section (or use the student system's numeric section identifier), add the same key to Registration, and relate on it one-to-many.

Answer: Treat the proposal as a data warning: find the duplicate keys, build a genuinely unique key, and relate one-to-many on that key.

Three tables arranged in a triangle: Campus, Outlet and Outlet Sales. Campus relates to Outlet Sales directly and also to Outlet, which relates to Outlet Sales. One of the two routes is shown as a dashed inactive line.
Two routes from Campus to Outlet Sales create ambiguity. Power BI keeps only one route active; remove the redundant relationship or flatten Campus attributes into Outlet.

Ambiguous paths

The outlet part of the model produced a classic ambiguity. Outlet Sales related to Outlet, Outlet related to Campus, and Kwabena had also related Outlet Sales directly to Campus. That gave filters two routes from Campus to Outlet Sales, and Power BI made the second relationship inactive. Instead of living with a dashed line nobody would remember, he removed the direct relationship and kept the chain through Outlet, then later flattened the campus name onto the Outlet dimension so that the outlet part of the star had a single, short path.

Relationships in a model with several fact tables

Once a model has more than one fact table, relationship design becomes a question of paths. The Lumenvale model had Registration, Outlet Sales, Program Budget and Bookstore Stock, all sharing Date and Campus. Each fact table related to the dimensions it used, and no fact table related to another fact table. That rule is worth stating plainly: facts relate to dimensions, never directly to each other. If two fact tables were related, a filter on one fact would try to filter the other, totals would be calculated over a join of two event streams, and the model would quickly fill with inactive relationships as Power BI refused ambiguous paths.

Comparisons across facts happen through shared dimensions instead. A matrix with Campus on rows and measures from both Registration and Outlet Sales works because the Campus filter reaches each fact table along its own relationship. If a reader adds Program to the matrix rows, the Outlet Sales measure shows the same value on every program row, because Program has no relationship to Outlet Sales. That repeated value is correct, since outlet sales have nothing to do with programs, but it can confuse readers. Kwabena handled it with report design, keeping outlet visuals and program visuals on separate pages, and by documenting which dimensions each fact table used in the measure descriptions.

Worked problem

Spotting a dimension that does not reach a fact

A dean builds a table with Program on rows and the measures Registrations and Outlet Sales. Outlet Sales shows the same large number on every row. Is the model broken?

Show solution
  1. Check the relationships: Program relates to Registration but not to Outlet Sales.
  2. With no path from Program to Outlet Sales, the Program filter cannot reduce the outlet rows, so every row shows the unfiltered total.
  3. This is expected behavior, because outlet sales are not recorded by program.
  4. Explain it to the dean, move the outlet measure to a page sliced by campus and outlet, or write the outlet measure to return blank when Program is filtered.

Answer: The model is working as designed. Program does not filter Outlet Sales; separate the visuals or make the measure blank when Program is in scope.

Common mistakes

  • MistakeAccepting every relationship that autodetect creates.

    InsteadReview each line, delete accidental matches, and consider turning off autodetect after data load.

  • MistakeSetting cross-filter direction to both on many relationships to make slicers behave.

    InsteadUse single direction by default; use CROSSFILTER in a measure or a visual filter for one-off needs.

  • MistakeSwitching a relationship to many-to-many to make an error go away.

    InsteadTreat unexpected many-to-many as a sign of duplicate keys and fix the data.

  • MistakeIgnoring blank rows in visuals.

    InsteadInvestigate unmatched keys; they mean facts refer to dimension members that do not exist yet.

Summary and key terms

  • Relationships propagate filters between tables at query time; they do not join data at load.
  • Cardinality can be one-to-many, many-to-one, one-to-one or many-to-many; a star schema is mostly one-to-many.
  • Unexpected many-to-many or one-to-one cardinality usually points to duplicate keys or tables that should be merged.
  • Cross-filter direction is single by default; both directions add ambiguity and cost and should be used deliberately.
  • CROSSFILTER changes direction for one measure; USERELATIONSHIP activates an inactive relationship for one measure.
  • Only one active filter path may exist between two tables; extra relationships become inactive.
  • Fact keys with no dimension match are grouped under a virtual blank row.
Relationship
A link between columns in two tables that lets filters travel from one to the other.
Cardinality
Whether values are unique or repeat on each side of a relationship.
Cross-filter direction
Which way filters travel along a relationship: single or both.
Active relationship
The relationship Power BI uses by default; only one active path is allowed between two tables.
Inactive relationship
A relationship shown as a dashed line that measures can switch on with USERELATIONSHIP.
Bridge table
A table that resolves a many-to-many situation with one row per pair of related members.
Blank row
A virtual dimension row that groups fact rows whose keys have no match.

Practice problems

Work each problem on paper first, then reveal the answer to check.

  1. What does a relationship with single cross-filter direction from Campus to Registration allow?

    Show answer

    A filter on Campus filters Registration, but a filter on Registration does not filter Campus.

  2. Name a function that applies both-direction filtering for one measure only.

    Show answer

    CROSSFILTER, used as a filter argument of CALCULATE.

  3. A visual shows tuition under a blank program name. What is the likely cause?

    Show answer

    Registration rows have program codes with no matching row in the Program dimension.

Lesson quiz · 5 questions

Score 60% or more to complete this lesson.

Take the quiz