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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
richard-powerbi
Post Patron
Post Patron

How to remove and add keys? Table.ReplaceKeys how does it work?

I have a SQL table loaded in PBI. I want to create my own primary keys and qualify them as keys in PBI.

The documentation on Table.ReplaceKeys is really bad. Can anyone explain how it works? Or is there another way of doing this?

1 ACCEPTED SOLUTION
dax
Community Support
Community Support

Hi @richard-powerbi , 

It seems to change key, you could try below M code and refer to power-query-m-primer-part13-tables-table-think-ii  and this post  for details.

let

   
  
  Source = #table(
    {"CompanyID", "Name", "Location"}, 
      {
        {1, "ABC Company", "Chicago"}, 
        {2, "ABC Company", "Charlotte"}, 
        {3, "Some Other Company", "Cincinnati"}
      }
    ),
  KeysTagged = Table.AddKey(Source, {"CompanyID"}, true),
  aa=Table.ReplaceKeys(
        KeysTagged,
        {
            [Columns = {"Location"},
            Primary = true]
        }
    )
in
 Table.Keys(aa)

Best Regards,
Zoe Zhi

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
dax
Community Support
Community Support

Hi @richard-powerbi , 

It seems to change key, you could try below M code and refer to power-query-m-primer-part13-tables-table-think-ii  and this post  for details.

let

   
  
  Source = #table(
    {"CompanyID", "Name", "Location"}, 
      {
        {1, "ABC Company", "Chicago"}, 
        {2, "ABC Company", "Charlotte"}, 
        {3, "Some Other Company", "Cincinnati"}
      }
    ),
  KeysTagged = Table.AddKey(Source, {"CompanyID"}, true),
  aa=Table.ReplaceKeys(
        KeysTagged,
        {
            [Columns = {"Location"},
            Primary = true]
        }
    )
in
 Table.Keys(aa)

Best Regards,
Zoe Zhi

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Another question, is below code correct if I want two columns to act as one composite key? Both columns do not have unique values, only the combination of them is unique.

 

= Table.ReplaceKeys(Source, {[Columns = List.Combine({{"Column1"}, {"Column2"}), Primary = true]})

 

Later on I will merge on these two columns and I would like this utilize this for performance. Does above code work for this?

@dax

Anyone knows?

Hi @richard-powerbi,

You could simplify it as

... {[Columns = {"Column1", "Column2"}, Primary = true]})

and as long as it's unique you should see some performance improvements.



Feel free to connect with me:
LinkedIn

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors