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

BASE

In my recent quest to create or catalog as many DAX equivalents for Excel functions, we can make a slight modification to DEC2HEX which itself is a slight modification to DEC2BIN in order to get BASE. Convert decimal numbers to base 2 - 36 just like the Excel BASE function.

 

BASE = 
    VAR __DecimalNumber = MAX('Table'[Decimal])
    VAR __Radix = SELECTEDVALUE(Radix[Radix],10)
    VAR __Dec2Base = 
        { 
            (10, "A"), 
            (11, "B"), 
            (12, "C"), 
            (13, "D"), 
            (14, "E"), 
            (15, "F"), 
            (16, "G"), 
            (17, "H"), 
            (18, "I"), 
            (19, "J"), 
            (20, "K"), 
            (21, "L"), 
            (22, "M"), 
            (23, "N"), 
            (24, "O"), 
            (25, "P"), 
            (26, "Q"), 
            (27, "R"),
            (28, "S"),
            (29, "T"),
            (30, "U"),
            (31, "V"),
            (32, "W"),
            (33, "X"),
            (34, "Y"),
            (35, "Z")
        }    
RETURN
    IF(
        __DecimalNumber = 0,
        "0",
            VAR __Table = 
                ADDCOLUMNS(
                    ADDCOLUMNS(
                        GENERATESERIES(0,LOG(__DecimalNumber,__Radix),1),
                        "BaseInDecimal",MOD(TRUNC(__DecimalNumber / POWER(__Radix,[Value])),__Radix)
                    ),
                    "Base",IF([BaseInDecimal] < 10,[BaseInDecimal],MAXX(FILTER(__Dec2Base,[Value1] = [BaseInDecimal]),[Value2]))
                )
        RETURN
            CONCATENATEX(__Table,[Base],"",[Value],DESC)
    )

 

NOTE: Does it support negative numbers? No, no it does not. But neither does the Excel BASE function so meh...

 

No RepliesBe the first to reply