Forum Discussion
How to remove and add keys? Table.ReplaceKeys how does it work?
- 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 ZhiIf this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
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.
- richard-powerbi6 years agoPost Patron
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?
- richard-powerbi6 years agoPost Patron
Anyone knows?
- Smauro6 years agoSolution SageHi 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.