Search code examples
powerbidax

Power BI/DAX - compare single column value across rows


(I'm specifically seeking a DAX solution) Given the below sample table, I want to get a count of how many IDs have differing levels for type=A and type=B (ignore any other types). In this case, the count would be 1 since ID 123 has differing levels for types A and B.

ID Type Level
123 A 2
123 B 1
123 C 2
987 A 3
987 B 3

Solution

  • I filtered the main table for types A and B to create a summary table by ID, by calculating the maximum level for types A and B separately for each ID. This is necessary because your example doesn't specify multiple levels per type per ID, but if that's a possibility, with MAX you get a distinct level to compare.

       IDsWithDifferingLevels = 
        VAR FilteredTable = 
            FILTER(
                'YourTableName',
                'YourTableName'[Type] = "A" || 'YourTableName'[Type] = "B"
            )
        
        VAR SummaryTable = 
            SUMMARIZE(
                FilteredTable,
                'YourTableName'[ID],
                "LevelA", CALCULATE(MAX('YourTableName'[Level]), 'YourTableName'[Type] = "A"),
                "LevelB", CALCULATE(MAX('YourTableName'[Level]), 'YourTableName'[Type] = "B")
            )
        
        VAR DifferingLevelsCount = 
            CALCULATE(
                COUNTROWS(
                    FILTER(
                        SummaryTable,
                        [LevelA] <> [LevelB]
                    )
                )
            )
        
        RETURN
            DifferingLevelsCount
    

    enter image description here