Forum Discussion

richard-powerbi's avatar
richard-powerbi
Post Patron
6 years ago
Solved

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 t...
  • dax's avatar
    6 years ago

    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.