Forum Discussion

smpa01's avatar
smpa01
Community Champion
4 years ago
Solved

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 = 100010...
  • smpa01's avatar
    smpa01
    4 years ago

    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"