Greg_Deckler's avatar
Greg_Deckler
Icon for Community Champion rankCommunity Champion
6 years ago

BITXOR

In my recent quest to create or catalog as many DAX equivalents for Excel functions, with DEC2BIN and BIN2DEC figured out and combining this with the work on XOR and BITOR we can implement a bitwise XOR function, BITXOR:

BITXOR = 
    VAR __Decimal1 = SELECTEDVALUE('Decimal1'[Decimal1],0)
    VAR __Decimal2 = SELECTEDVALUE('Decimal2'[Decimal2],0)
RETURN
    SWITCH(TRUE(),
        __Decimal1 = 0,__Decimal2,
        __Decimal2 = 0,__Decimal1,
            VAR __Table1 = 
                ADDCOLUMNS(
                    GENERATESERIES(0,LOG(__Decimal1,2),1),
                    "Bit",MOD(TRUNC(__Decimal1 / POWER(2,[Value])),2)
                )
            VAR __Table2 = 
                ADDCOLUMNS(
                    GENERATESERIES(0,LOG(__Decimal2,2),1),
                    "Bit",MOD(TRUNC(__Decimal2 / POWER(2,[Value])),2)
                )
            VAR __Table =
                ADDCOLUMNS(
                    ADDCOLUMNS(
                        GENERATESERIES(0,MAXX({ COUNTROWS(__Table1), COUNTROWS(__Table2) },[Value]) - 1),
                        "Xor",IF(
                                MAXX(FILTER(__Table1,[Value] = EARLIER([Value])),[Bit]) = 
                                    MAXX(FILTER(__Table2,[Value] = EARLIER([Value])),[Bit])
                                ,0,1
                            )
                    ),
                    "Decimal",POWER(2,[Value]) * [Xor]
                )
        RETURN
            SUMX(__Table,[Decimal])
    )

NOTE: Just like the original Excel BITXOR function, does not support negative numbers as inputs.

 

No RepliesBe the first to reply