Power BI Table Primary Key: Set and Use It Correctly
In this article
Setting a primary key in Power BI ensures data integrity and enables efficient relationships between tables, which is critical for accurate visuals and DAX calculations. A primary key uniquely identifies each row in a table, preventing duplicates and helping Power BI optimize query performance. You must first load your data into Power BI Desktop using Get Data, and ensure your source has a column with unique, non-null values—like an ID field—before proceeding. No special tools or licenses are required beyond Power BI Desktop.
What You'll Need
- Power BI Desktop (free download from powerbi.microsoft.com)
- A data source with a column containing unique, non-null values (e.g., OrderID, CustomerID, ProductCode)
- Access to the Power Query Editor and Model view in Power BI Desktop
Step-by-Step
- Open Power BI Desktop and load your data using Get Data. Choose your source (Excel, SQL Server, CSV, etc.) and load at least one table with a candidate key column.
- After data loads, go to the Data view by clicking the table icon on the left sidebar. You’ll see your table listed with columns.
- Click on the table name to select it. Then, in the Columns pane on the right, find the column you want to designate as the primary key. This must be a column with no duplicates and no blank values—common examples include InvoiceNumber, EmployeeID, or TransactionID.
- Right-click the column name and select Mark as Primary Key. A key icon will appear next to the column name, confirming the setting.
- Switch to Model view by clicking the table-and-lines icon on the left sidebar. Verify that the primary key column now shows a key symbol in the table’s column list.
- Create relationships with other tables by dragging the primary key column to a matching foreign key column in another table. Power BI will auto-detect the relationship type based on the primary key.
- After creating relationships, click on each relationship line to open the properties pane. Ensure the Cardinality is set to One-to-Many (with the primary key table on the “one” side) and Cross Filter Direction is set to Single (default) unless you need bidirectional filtering.
- Test the setup by creating a simple visual—a table or card showing a count of rows in the primary key table. If duplicates exist, Power BI will warn you during refresh or display inaccurate counts.
- Refresh the data (Home > Refresh) and check for errors. If any row in the primary key column becomes null or duplicated after refresh, Power BI will flag it in the Errors tab under the Applied Steps in Power Query.
Common Mistakes
- Marking a column as a primary key that contains duplicate values—this causes relationship failures and inaccurate aggregations.
- Using text columns with leading/trailing spaces or inconsistent casing (e.g., "CUST001" vs "cust001") as primary keys without cleaning them first in Power Query.
- Assuming a composite key (multiple columns together) can be set as a primary key—Power BI only allows one column to be marked as primary key per table.
- Marking a calculated column as a primary key, which is not allowed because calculated columns are not stored in the data model and can change with refresh.
- Forgetting to refresh data after fixing duplicates in the source, leading to persistent model errors.
Tips
- Always validate your candidate key column in Power Query before marking it as primary: use Remove Duplicates and Remove Blank Rows under the Home tab to clean it first.
- If your source doesn’t have a natural key, create one in Power Query using Index Column (Transform > Index Column > From 1) and mark that as primary key.
- Use the Data Profiling pane (View > Data Profiling) to check uniqueness, blank counts, and data types before setting a primary key.
- Primary keys improve performance in large models—use them even if relationships aren’t immediately needed, as they prepare your model for future expansion.
- When working with multiple tables from the same source (e.g., multiple Excel sheets), ensure the primary key column names match exactly in case and spelling across tables to avoid broken relationships.
- Use the Model view’s “Detect” button to let Power BI suggest relationships, but always review them manually—auto-detection can misassign keys if data quality is poor.
Troubleshooting
- If you see "The column 'X' contains duplicate values and cannot be used as a primary key," go back to Power Query and remove duplicates or fix the source data. Use Group By to identify which values are duplicated.
- If relationships disappear after a refresh, check the primary key column for null values. In Power Query, use Replace Values to convert blanks to a placeholder like "Unknown" or filter them out.
- If visuals show incorrect totals, verify the relationship direction. Go to Model view, click the relationship line, and ensure it points from the primary key table (one side) to the foreign key table (many side).
- If Power BI doesn’t let you mark a column as primary key, check if it’s a calculated column or if the data type is unsupported (e.g., binary or image). Convert it to text or integer if needed.
- If you accidentally mark the wrong column, right-click it again and select "Remove Primary Key," then try again with the correct column.
- For complex models with 10+ tables, use the Model view’s "Clean Up" button to auto-arrange tables and highlight orphaned relationships that may stem from missing primary keys.