Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
smpa01
Super User
Super User

Integer/Decimal to Binary in PQ

Does PQ currently have any syntax that converts an integer /decimal value to Binary.

I looked into the documentation and could not find anything

e.g.

     52 = 110100,  

     137.5879541 = 10001001.10010110100001000010100011101111000011001001

 

https://www.rapidtables.com/convert/number/decimal-to-binary.html

 

Thank you in advance

@ImkeF 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
1 ACCEPTED SOLUTION

@mahoneypat  this is mine and only for integer but I even have a better solution than this, getting blogged out.

 

let
  Source = (p1 as number) =>
    let
      Loop = List.Generate(
        () => [i = p1, j = Number.IntegerDivide(i, 2), k = Number.Mod(i, 2), l = Text.From(k)], 
        each  [i] > 0, 
        each  [i = [j], j = Number.IntegerDivide(i, 2), k = Number.Mod(i, 2), l = Text.From(k) & [l]], 
        each  [l]
      )
    in
      Loop{List.Count(Loop) - 1},
   Source1 = {1..100},
    #"Converted to Table" = Table.FromList(Source1, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Added Custom" = Table.AddColumn(#"Converted to Table", "Custom", each fx([Column1]))    
in
  #"Added Custom"

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

View solution in original post

5 REPLIES 5
mahoneypat
Microsoft Employee
Microsoft Employee

Thought about this some more and came up with the function below to convert any integer to binary.  You can create a blank query, put this is the advanced editor.  On your original query, you can then Invoke a Custom Function on the add column tab.

 

(inputnumber)=>
let
    input = inputnumber,
    digits = Number.RoundUp(Number.Log(input, 2), 0),
    powerslist = List.Transform(List.Reverse({0..digits}), each Number.Power(2,_)),
    oneszeros = List.Accumulate(powerslist, [rem = input, num = {}], (state, current) => if state[rem] >= current then [rem = state[rem] - current, num = state[num] & {1}] else [rem = state[rem], num = state[num] & {0}]),
    listastext = List.Transform(oneszeros[num], each Number.ToText(_)),
    result = Text.Combine(listastext, "")
in
    result

 

@Ehren had a solution that was even better.

Converting Number column to binary and map the bin... - Microsoft Power BI Community

 

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


@mahoneypat  this is mine and only for integer but I even have a better solution than this, getting blogged out.

 

let
  Source = (p1 as number) =>
    let
      Loop = List.Generate(
        () => [i = p1, j = Number.IntegerDivide(i, 2), k = Number.Mod(i, 2), l = Text.From(k)], 
        each  [i] > 0, 
        each  [i = [j], j = Number.IntegerDivide(i, 2), k = Number.Mod(i, 2), l = Text.From(k) & [l]], 
        each  [l]
      )
    in
      Loop{List.Count(Loop) - 1},
   Source1 = {1..100},
    #"Converted to Table" = Table.FromList(Source1, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Added Custom" = Table.AddColumn(#"Converted to Table", "Custom", each fx([Column1]))    
in
  #"Added Custom"

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
mahoneypat
Microsoft Employee
Microsoft Employee

I am not aware of a native function to do that.  However, you could do it with a custom function, R or Python, or with a REST API call (slower).

 

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


BA_Pete
Super User
Super User

Hi @smpa01 ,

 

Read through this thread:

https://community.powerbi.com/t5/Desktop/Number-to-Binary/m-p/235261 

 

Here, I believe, is the final iteration of the solution function courtesy of, and much credit to, M-Wizard @MarcelBeug :

fnNBC = (input as anynonnull, base as number, optional outputlength as number) as any =>
    let
        //    input = 10,
        //    base = 2,
        //    outputlength = null,
        Base16 = "0123456789ABCDEF",
        Base32 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",
        Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
        Lookups = List.Zip({{16,32,64},{Base16,Base32,Base64}}),
        Lookup = Text.ToList(List.Last(List.Select(Lookups,each _{0} <= List.Max({16, base}))){1}),
        InputToList = Text.ToList(input),

        // This part will be executed if input is text:
            Reversed = List.Reverse(InputToList),
            BaseValues = List.Transform(Reversed, each List.PositionOf(Lookup,_)),
            Indexed = List.Zip({BaseValues, {0..Text.Length(input)-1}}),
            Powered = List.Transform(Indexed, each _{0}*Number.Power(base,_{1})),
            Decimal = List.Sum(Powered),
        // So far this part

        // This part will be executed if input is not text:
            Elements = 1+Number.RoundDown(Number.Log(List.Max({1,input}),base),0),
            Powers = List.Transform(List.Reverse({0..Elements - 1}), each Number.Power(base,_)),
            ResultString = List.Accumulate(Powers,
                                          [Remainder = input,String = ""], 
                                          (c,p) => [Remainder = c[Remainder] - p * Number.RoundDown(c[Remainder] / p,0),
                                                    String = c[String] & Lookup{Number.RoundDown(c[Remainder]/p,0)}])[String],    
            PaddedResultString = if outputlength = null then ResultString else Text.PadStart(ResultString,outputlength,Lookup{0}),
        // So far this part

        Result = if input is text then Decimal else PaddedResultString
    in
        Result

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




@BA_Pete  thanks for looking into it.  

 

@MarcelBeug is a legend in this forum and I did come across this post. But I did not want to utilize this as I can't imagine running this on a big table as the performance would take a big hit. Also, it does not convert the decimal value to  binary.

 

I have a new solution and blog out soon.

 

@mahoneypat  many thanks for the confirmation.

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Top Solution Authors