Converting Decimal Numbers To Hexadecimal In Power Query M

This is a very short post! A lot of people have blogged about how to convert numbers between different bases in M (see for example Maxim Zelensky’s very elegant solution for converting from binary to decimal), but today I noticed there was a very easy way to convert a decimal number to hexadecimal using the Number.ToText() function: you just need to use “x” in the second parameter. For example:

Number.ToText(12, "x") //returns c
Number.ToText(123, "x") //returns 7b

I’m sure this will come in handy somewhere…

8 responses

  1. This is excellent, Chris!

    What’s your preferred approach for converting hexadecimal numbers into decimal? The shortest approach I’ve found so far was by Igor Cotruta:

    Expression.Evaluate ( “0x” & HexString )

    In your opinion, is Expression.Evaluate always safe to use?

  2. Pingback: Converting Decimal Numbers To Hexadecimal In Power Query M |

      • Hi Chris

        I’ve been meaning to reply to this. This is the one I that wrote up with for my work scenario, it’s a lot simpler than the one in your link. I can’t remember where I got the recursive algorithm from, it was something that I found on Google.

        1. Create a blank query, name it “number_tobinarytext”, and paste in this function below.

        number_parameter as number,
        optional text_from_parameter as nullable text


        text_from = if text_from_parameter = null then “” else text_from_parameter,
        number_tobinarytext_function = if number_parameter <= 1 then Text.From(number_parameter) & text_from else @number_tobinarytext( Number.RoundDown( number_parameter / 2 ), Text.From( Number.Mod( number_parameter, 2 ) ) & text_from )



  3. I am trying to adapt your code to work binary to hex , I have an example of where I have 110000000101100111011110 and want it converting to it’s hex code which is C059DE

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: