0

I have a column name Config which contains decimal number 1 to 14. I want to create a new column config2 and convert config column into binary (base2).

eg. config2 =(config binary (base2))

 *Config |Config2*
     1   |0001
     2   |0010
     3   |0011

Here is how my data looks like

enter image description here

Foxan Ng
  • 6,883
  • 4
  • 34
  • 41
Joyo Waseem
  • 680
  • 8
  • 25

2 Answers2

3

You can do it with recursion in Power Query / M.

Bin = (t as text, n as number) => 
    if n <= 1 
    then Text.From(n) & t
    else @Bin(Text.From(Number.Mod(n, 2)) & t, Number.RoundDown(n/2))

Note the @ before the Bin which enables the recursion to happen.

Results:

converted

My query for your reference:

let
    Bin = (t as text, n as number) => if n <= 1 then Text.From(n) & t else @Bin(Text.From(Number.Mod(n, 2)) & t, Number.RoundDown(n/2)),

    Source = {0..14},
    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Renamed Columns" = Table.RenameColumns(#"Converted to Table",{{"Column1", "Config"}}),
    #"Changed Type" = Table.TransformColumnTypes(#"Renamed Columns",{{"Config", Int64.Type}}),
    #"Added New Column" = Table.AddColumn(#"Changed Type", "Config2", each Bin("", [Config]))
in
    #"Added New Column"

If you need to pad leading zeros to Config2 then you'll need the following DAX:

Formatted Config2 = FORMAT(VALUE(Query1[Config2]), "0000")

Results:

padded

Foxan Ng
  • 6,883
  • 4
  • 34
  • 41
2

The function below is able to convert numeric values to strings, representing numbers with another base, and vice versa. You can use bases 2-16, 32 and 64.

Examples, if you name the function NumberBaseConversion:

= NumberBaseConversion(12, 2, 5) returns "01100"

= NumberBaseConversion("AB", 16) returns 171

(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(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
MarcelBeug
  • 2,872
  • 1
  • 8
  • 10