Search code examples
comparepowerbirowsdaxm

Dynamic row comparison in one table - DAX / M language / Power BI


Is this possible?

I've got a table, with a column containing the version of product (for example: fast car). Every version has a list of parts (for example: color). Every part has a number of values (for example: red).

VERSION PART PART_VALUE VERSION1 PART1 PART_VALUE1 VERSION1 PART2 PART_VALUE2 VERSION1 PART3 PART_VALUE3 VERSION2 PART2 PART_VALUE2 VERSION2 PART3 PART_VALUE3 VERSION2 PART4 PART_VALUE4 VERSION3 PART1 PART_VALUE1 VERSION3 PART2 PART_VALUE2 VERSION3 PART3 PART_VALUE4

Now I want to select - in Fragmentator or in some other checkbox - Version1 and Version2 and the results should be:

"ONLY DIFFENCES BETWEEN VERSION1 AND VERSION2"
VERSION1 PART1 PART_VALUE1 VERSION2 PART4 PART_VALUE4

or after selecting Version1 and Version3 the results should be:

"ONLY DIFFENCES BETWEEN VERSION1 AND VERSION3"
VERSION1 PART3 PART_VALUE3 VERSION3 PART3 PART_VALUE4


Solution

  • Here's an M query which should do what you want:

    (V1, V2) =>
    let
        Source = MyTable,
        First = Table.SelectRows(Source, each ([VERSION] = V1)),
        Second = Table.SelectRows(Source, each ([VERSION] = V2)),
        #"First Unique" = Table.NestedJoin(First,{"PART", "PART_VALUE"},Second,{"PART", "PART_VALUE"},"Second",JoinKind.LeftAnti),
        #"Second Unique" = Table.NestedJoin(Second,{"PART", "PART_VALUE"},First,{"PART", "PART_VALUE"},"First",JoinKind.LeftAnti),
        Combine = Table.Combine({#"First Unique", #"Second Unique"}),
        #"Removed Columns" = Table.RemoveColumns(Combine,{"Second", "First"})
    in
        #"Removed Columns"