将十进制转换为二进制幂bi

时间:2017-10-23 05:19:04

标签: powerbi dax powerquery m powerbi-datasource

我有一个列名称Config,其中包含十进制数1到14.我想创建一个新列config2并将config列转换为二进制(base2)。

例如。  config2 =(config binary(base2))

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

以下是我的数据的样子

enter image description here

2 个答案:

答案 0 :(得分:2)

您可以使用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))

请注意@之前的Bin,以便进行递归。

结果:

converted

我的查询供你参考:

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"

如果您需要将前导零填充到Config2,那么您需要以下DAX:

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

结果:

padded

答案 1 :(得分:2)

下面的函数能够将数值转换为字符串,用另一个基数表示数字,反之亦然。您可以使用2-16,32和64的基础。

示例,如果您将函数命名为NumberBaseConversion:

= NumberBaseConversion(12,2,5)返回“01100”

= NumberBaseConversion(“AB”,16)返回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