Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Expression.Error: A primary key already exists on the given table type.

Hi,

The setup of my Power Query is this:
1) A single query to a SQL Server connection. The connection has a SQL statement that is query folded back to the SQL Server. This query is named BaseTable. The BaseTable is a high granular level table.
2) I have another eight queries that all reference the BaseTable query. Each of these eight queries aggreagate the BaseTable data differently. For discussion, we call each of the eight queries Ref1, Ref2, Ref3..... Ref8.
3) Once the referenced queries have all completed their different aggregations. I then use query Ref5 merge that with Ref1.
I then merge Ref5 (which now has Ref1 included) with Ref2.
I then merge Ref5 (which now has Ref1 and Ref2 included) with Ref3.
And so on until all reference queries have been merge together.


So that is my PQ setup.
To make PQ execute quicker, I would like place a key (Table.AddKey) in the right places. So I intend to do this by (and correct me if this isn't the best approach):
1) Not place a key on the BaseTable query.
2) Place a key (Primary Key) on the Ref5 query at the point of all Ref5 query's aggregations being completed, and before the next step of the merge with Ref1.
3) Place key (Foreign Key) as the last step of queries Ref1, Ref2, Ref3, Ref4, Ref6, Ref7, Ref8.

Are the keys (PK and FK) being added to the right tables at the right step/point?
When I attempt to add my first key, the Primary Key, to Ref5 (between the aggregation steps and merging with other queries) I get the below error. I haven't explicity created any keys at this point in the whole .pbix file. Any ideas on how a primary key already exists?
key.PNG


Am I getting this error due to all the Ref queries, the queries where I am looking to place a key in, are somehow implicity given a key due to being reference queries?

Strangely, I can set each key as a Foreign Key but I can't set a Primary Key on any table without receiving the above error message.

Can anyone shed any light on this?

Thanks.

1 REPLY 1
v-piga-msft
Resident Rockstar
Resident Rockstar

Hi @Anonymous ,

For your scenario, please refer to this similar thread which has been solved firstly.

In addition, for your error messge, I will suggest that you could duplicate BaseTable query and do the calculation instead of referencing the BaseTable query directly.

@ImkeF  any other ideas?

Best Regards,

Cherry

 

 

Community Support Team _ Cherry Gao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.