Search notes:

Power Query M formula language standard library: Table.AddKey

Table.AddKey(TABLE as table, COLUMNS as list, ISPRIMARY as logical) as table

Primary keys

The following Power Query program creates a table with a primary key:
let
   tab = #table(
     {"id", "val 1", "val 2"}, {
     {  1 , "one"  , "foo"  },
     {  2 , "two"  , "bar"  },
     {  3 , "three", "baz"  }
   }),
   tab_pk = Table.AddKey(
     tab,
     {"id"},
     true
   )
in
   tab_pk

PKs are not enforced

Unfortunately, Power Query does not enforce (or check) primary keys. The last record should be rejected, imho, because the id 2 is already used. However, the program just runs fine:
let
   tab = #table(
     {"id", "val 1", "val 2"}, {
     {  1 , "one"  , "foo"  },
     {  2 , "two"  , "bar"  },
     {  3 , "three", "baz"  },
     {  2 , "uh oh", "!"    }
   }),
   tab_pk = Table.AddKey(
     tab,
     {"id"},
     true
   )
in
   tab_pk

See also

The Power Query standard library, especially the function Table.Keys

Index