Search code examples
powerquerym

How might I filter out one half of a relationship set between values of two columns?


I'm trying to isolate one half of the relationship set between two columns in a table. For instance, if I have a table like this:

enter image description here

It show a relationship set between A and B (or B and A) twice. Once for each direction of the relationship...first A to B and then B to A.

So how might I be able to identify and filter out the second half (direction) of the relationship set?

enter image description here

To achieve a result like this?

enter image description here

Is there a "simple" way?


Solution

  • There may be a more efficient way, but this should work:

    1. Add a column which creates a sorted text value of the two columns, possibly separated by a character that won't appear in either column. You can do this by calling List.Sort on a list of the cell values, and then using List.Accumulate to join the strings. For example, the expression to use in Add Custom Column could be each List.Accumulate(List.Sort({[Column1], [Column2]}), "", (state, current) => state & ";" & current).
    2. Use Remove Duplicates on the new column to get rid of the duplicate relationships (i.e. use Table.Distinct).
    3. Remove the added column.