Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Power BI Table Primary Key: Set and Use It Correctly

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. 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.
  2. 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.
  3. 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.
  4. Right-click the column name and select Mark as Primary Key. A key icon will appear next to the column name, confirming the setting.
  5. 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.
  6. 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.
  7. 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.
  8. 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.
  9. 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.

Frequently Asked Questions

Can I use a date column as a primary key?

Yes, but only if every date appears exactly once in the table. Most date tables have multiple rows per day (e.g., by hour or product), so they’re usually not suitable. Use a surrogate key like DateID instead.

Do I need a primary key for every table?

No, but it’s strongly recommended for fact and dimension tables. Tables used only for lookup or static values (like a list of regions) benefit most. Tables with no unique identifier can still be used, but relationships may be less reliable.

Can I set a primary key in Power BI Service (online)?

No. Primary keys can only be set in Power BI Desktop. After publishing, the setting is preserved, but you cannot change it in the service. Always finalize keys before publishing.

What happens if I don’t set a primary key?

Power BI can still create relationships using column names and data types, but it may guess incorrectly, leading to ambiguous or circular relationships. Performance may also suffer, and DAX measures could return inaccurate results due to improper context propagation.

Can I have multiple primary keys in one table?

No. Power BI allows only one primary key per table. If your data requires a composite key (e.g., OrderID + ProductID), create a new calculated column concatenating them (e.g., OrderID & "-" & ProductID) and mark that as the primary key.

Does setting a primary key affect data storage size?

Not significantly. Power BI compresses data efficiently regardless of primary key status. The benefit is in relationship accuracy and query speed, not file size.