# Primary and Foreign Keys in Power BI

Learn how primary and foreign keys link tables in a Power BI data model, how to set cardinality, and how to fix key errors in service and cost records.

Primary and foreign keys in Power BI are the columns that link tables through model relationships. A primary key holds one unique value per row, such as a vehicle ID in a vehicles table. A foreign key repeats that value in a related table, such as service records, letting filters flow across. 

- Power BI has no formal key constraint, but the one side of a relationship needs unique values.
- Match data types on both key columns before creating a relationship.
- Build a star schema with fact tables of costs and services linked to lookup tables.
- Use one column per side of a relationship and combine fields when a key spans several columns.
- Clean blank and mismatched foreign key values in Power Query to avoid a blank row in visuals.
 

## What primary and foreign keys mean in Power BI 

A primary key in Power BI is a column whose value identifies exactly one row in a table. In a fleet model, a vehicle ID or unit number in the vehicles table plays that role. Each vehicle appears once, with its make, model, year and assigned driver beside it. 

A foreign key is a column in another table that stores values pointing back to a primary key. Service records, fuel purchases and repair invoices each carry the vehicle ID of the unit involved. The same vehicle ID can appear on many rows, one for every oil change or tire purchase. That repeated column is what lets Power BI tie each cost to the right vehicle. 

Power BI does not enforce keys the way a relational database engine does. The terms describe the role a column plays inside a relationship rather than a setting you switch on. Power BI checks for unique values only on the one side when you define a relationship. Orphan or blank foreign keys load without warning, so clean data depends on your own preparation. 

## How keys form relationships in the data model 

A Power BI relationship joins a key column in one table to a matching column in another. Filters then travel along that link, so choosing a vehicle narrows its service costs. Without a relationship, a visual combining both tables shows the same total on every row. 

The relationships view in Power BI Desktop shows tables as boxes joined by lines. Newer versions of Power BI Desktop label the same screen as Model view. Each line marks a 1 on the primary key side and an asterisk on the foreign key side. An arrow on the line shows the direction in which filters flow. 

Automatic detection in Power BI can create relationships when loaded tables share column names. That feature is handy, yet it can link columns that share a name but hold different things. A column named ID in both a vehicles table and a drivers table is a typical trap. Review every detected line before you build reports on top of it. 

## Cardinality options and when each one fits 

Cardinality in a Power BI relationship describes how many rows on each side can share a key value. One-to-many is the standard shape between a lookup table and a table of transactions. One vehicle row matches many service, fuel or repair rows through the vehicle ID. 

One-to-one cardinality means each key appears once on both sides. That shape can link a vehicles table to a separate table of registration or insurance details. Merging those two tables in Power Query often gives a simpler model with fewer relationships to manage. 

Many-to-many cardinality allows duplicate key values on both sides of the relationship. Power BI supports it, but totals can look wrong when the duplication is unexpected. A bridge table holding unique keys often turns one many-to-many link into two one-to-many links. Reserve true many-to-many relationships for cases where duplicates are real, such as drivers shared across vehicles. 
| Cardinality | Key condition | Fleet example | 
| One-to-many | Unique on one side, repeated on the other | Vehicles to service records | 
| Many-to-one | Same as one-to-many, read from the other table | Fuel purchases to vehicles | 
| One-to-one | Unique on both sides | Vehicles to registration details | 
| Many-to-many | Repeated on both sides | Drivers to the vehicles they share |  

## Creating a relationship step by step 

Creating a Power BI relationship between primary and foreign keys starts with checking both columns. Both must share the same data type, such as whole number or text. A vehicle ID stored as text in one table but number in another can break the link. Leading zeros and stray spaces in text keys also prevent rows from matching. 

Relationships can be drawn by dragging the primary key onto the foreign key in the relationships view. The Manage relationships dialog offers the same result through drop-down lists of tables and columns. Either way, Power BI suggests a cardinality based on the values it finds. 

Cross filter direction sets which way filters pass across the relationship. Single direction sends filters from the primary key table to the foreign key table. Both directions let filters flow back, which can help with bridge tables but can also create ambiguous paths. Keep single direction unless a specific report needs the reverse flow. 

- Confirm the key column on the one side holds unique values
- Match data types on both key columns
- Drag the primary key onto the foreign key in the relationships view
- Check the suggested cardinality and correct it if needed
- Set cross filter direction, keeping single unless a report requires both
- Test with a visual that combines fields from both tables
 

## Arranging keys in a star schema 

A star schema places one or more fact tables in the center, surrounded by dimension tables. Fact tables hold events with amounts, such as service visits, fuel fills and parts costs. Dimension tables, also called lookup tables, describe things like vehicles, drivers, vendors and dates. 

Primary keys live in the dimension tables of a star schema, one unique value per row. Foreign keys sit in the fact tables, linking each event to its vehicle, vendor and date. Every relationship then runs one-to-many from a dimension to a fact table. Filters from any dimension reach every fact table connected to it. 

The star schema suits the Power BI engine, which filters efficiently from dimensions to facts. Separate service and fuel fact tables can share one vehicles dimension plus one date dimension. A single report page can then compare repair spend with fuel spend per vehicle without extra joins. 

## Handling composite and surrogate keys 

A composite key uses two or more columns together to identify a row uniquely. Power BI relationships connect a single column on each side, so composite keys need preparation first. Merging columns in Power Query, such as vendor ID with invoice number, creates one combined key. 

A combined key must be built the same way in both tables, with identical order and separator. Invoice headers and invoice line items both need the vendor-plus-invoice key in matching form. A mismatch of a single character leaves line items without a parent invoice. 

A surrogate key is an artificial ID, such as a sequential number, with no business meaning. Power Query can add one through an index column when source data lacks a clean identifier. Surrogate keys help when unit numbers get reused after a vehicle leaves the fleet. Fact tables then need the surrogate key too, added by merging on the original identifiers. 

## Fixing duplicate, blank and mismatched key values 

Duplicate values in a primary key column stop Power BI from creating a one-to-many relationship. The error message states that one of the columns must contain unique values. Use Remove Duplicates in Power Query only after confirming the repeats are true copies, not separate records. Power BI may propose many-to-many cardinality instead, which hides the underlying data problem. 

Foreign key values with no match in the dimension table produce a blank row in visuals. Service records with a missing or mistyped vehicle ID end up grouped under that blank entry. A table visual of costs by vehicle shows the size of the gap at a glance. 

Cleaning key columns at the source or in Power Query removes most matching problems before modeling. Trim stray spaces, keep leading zeros consistent and set one data type for every key column. Add new vehicles to the vehicles table before their first service records arrive. 

## Questions about keys in Power BI 

### Does Power BI need a primary key to create a relationship? 

No, Power BI does not require a declared primary key to create a relationship. The column on the one side of a one-to-many relationship must still hold unique values. Many-to-many relationships work without unique values, though they need more care in calculations. 

### Can a Power BI relationship use more than one column? 

No, each Power BI relationship links exactly one column in each table. A multi-column key needs to be merged into a single column first. Power Query handles that with the Merge Columns command on both tables. 

### What is an inactive relationship in Power BI? 

An inactive relationship is a defined link that does not filter data unless a measure calls it. Power BI allows only one active path between two tables, shown as a solid line. Inactive relationships appear dashed and are activated inside a measure with the USERELATIONSHIP function. A service table with both a request date and a completion date often needs one. 

### Why does my visual show the same total for every vehicle? 

The same total on every row means the tables in the visual have no working relationship. Power BI cannot pass a vehicle filter to the cost table without a link between keys. Check the relationships view for a missing line, a wrong column or a filter direction pointing away. 

### Should the date table have a primary key? 

Yes, a date table needs a date column with one unique row per day. Fact tables such as service records link to it through their own date column. Marking the table as a date table in Power BI enables time intelligence functions on that column. Service dates that carry a time part need converting to date only before they match.
