0

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

例如。config2 =(配置二进制(base2))

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

这是我的数据的样子

在此处输入图像描述

4

2 回答 2

3

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

注意使递归发生的@before 。Bin

结果:

已转换

我的查询供您参考:

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")

结果:

软垫

于 2017-10-23T08:53:33.240 回答
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
于 2017-10-23T21:27:07.977 回答