Greg_Deckler's avatar
Greg_Deckler
Community Champion
6 years ago

BITOR (Bitwise OR)

In my recent quest to create or catalog as many DAX equivalents for Excel functions, with DEC2BIN and BIN2DEC figured out, we can implement a bitwise OR function, BITOR:

 

BITOR = 
    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),
                        "Or",IF(
                                OR(
                                    MAXX(FILTER(__Table1,[Value] = EARLIER([Value])),[Bit]),
                                    MAXX(FILTER(__Table2,[Value] = EARLIER([Value])),[Bit])
                                ),1,0
                            )
                    ),
                    "Decimal",POWER(2,[Value]) * [Or]
                )
        RETURN
            SUMX(__Table,[Decimal])
    )

 

NOTE: Does not support negative numbers, but so what, neither does Excel's BITOR function.

 

No RepliesBe the first to reply