从文本中拆分小数作为批处理

Split decimal from text as batch

正在尝试使用此处提供的 prevo=ious 公式批量拆分小数,但结果是一个错误,指出 null 或“”或“x”(其中是数字)无法转换为类型列表.

公式:

=try Text.Remove([Column1],Text.ToList(Text.Remove([Column1],{"0".."9","."}))) otherwise null 在应用于单个列时有效,但是当尝试从这些列创建 table 时,我收到以下错误:

期望的输出:

M代码:

let
    Source = Excel.CurrentWorkbook(){[Name="Table19"]}[Content],
    #"Added Custom" = Table.AddColumn(Source, "Custom", each Table.FromColumns({
        
    (try Text.Remove([Column1],Text.ToList(Text.Remove([Column1],{"0".."9","."}))) otherwise null), 
    
    (try Text.Remove([Column2],Text.ToList(Text.Remove([Column2],{"0".."9","."}))) otherwise null)
    
    }))
in
    #"Added Custom"

我希望能够为 n 列生成一个 Table.FromColumns,然后我可以展开这些列。这只是一个示例,实际上,列数可能相差很大。

更新

为了更好地可视化我在 power query 中尝试做的事情,我想创建这个场景:

这样 table 可以扩展为:

可能是显而易见的事情,但感谢任何帮助。

编辑为可用于多列

let Source =Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
#"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1, Int64.Type),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Added Index", {"Index"}, "Attribute", "Value"),
#"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Value", Splitter.SplitTextByEachDelimiter({" "}, QuoteStyle.Csv, false), {"Value.1", "Value.2"}),
#"Removed Columns" = Table.RemoveColumns(#"Split Column by Delimiter",{"Value.2"}),
#"rename1" = Table.TransformColumns(#"Removed Columns",{{"Attribute", each _&"a", type text}}),
#"Pivoted Column" =  Table.RemoveColumns(Table.Pivot(#"rename1", List.Distinct(#"Lowercased Text"[Attribute]), "Attribute", "Value.1"),{"Index"}),
#"Removed Columns2" = Table.RemoveColumns(#"Split Column by Delimiter",{"Value.1"}),
rename = Table.TransformColumns(#"Removed Columns2",{{"Attribute", each _ & "b", type text}}),
#"Pivoted Column1" = Table.RemoveColumns(Table.Pivot(rename, List.Distinct(rename[Attribute]), "Attribute", "Value.2"),{"Index"}),
TFC = Table.FromColumns(Table.ToColumns(Source)&Table.ToColumns(#"Pivoted Column")&Table.ToColumns(#"Pivoted Column1"),Table.ColumnNames(Source)&Table.ColumnNames(#"Pivoted Column")&Table.ColumnNames(#"Pivoted Column1"))
in TFC

我只想复制两个原始列(添加列 > 复制列),然后在最左边的“”分隔符上拆分结果列。不需要M代码。

我会

  • 根据字符转换拆分列,包括字符列表中的小数。
  • 然后 Trim 删除所有 leading/following 空格的结果列

注意: 编辑代码以允许将任意数量的列一分为二。列名也可以是动态的

let
    Source = Excel.CurrentWorkbook(){[Name="Table21"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}}),

//Generate new table from all the columns
    //create List of columns
        colList = Table.ToColumns(#"Changed Type"),
        colNames = Table.ColumnNames(#"Changed Type"),

    //convert each column
        splitCols = List.Generate(
            ()=>[colPair=
                    List.Transform(colList{0},(li)=>
                        Splitter.SplitTextByCharacterTransition(
                            {"0".."9","."}, (c) => not List.Contains({"0".."9","."}, c))
                            (li)), 
                 cn = colNames{0},
                 idx=0],
            each [idx] < List.Count(colList),
            each [colPair=
                    List.Transform(colList{[idx]+1},(li)=>
                        Splitter.SplitTextByCharacterTransition(
                            {"0".."9","."}, (c) => not List.Contains({"0".."9","."}, c))
                            (li)), 
                   cn=colNames{[idx]+1},
                   idx=[idx]+1],
            each  List.Zip([colPair]) & {List.Transform({1..2}, (n)=> [cn] & "." & Text.From(n))}),
    newCols = List.Combine(List.Transform(splitCols, each List.RemoveLastN(_,1))),
    newColNames = List.Combine(List.Transform(splitCols, each List.Last(_))),
    newTable = Table.FromColumns(newCols,newColNames),

//trim the excess spaces
    trimOps = List.Transform(Table.ColumnNames(newTable), each {_, Text.Trim}),
    trimAll = Table.TransformColumns(newTable, trimOps)
in
    trimAll

三列示例

同样,如果您想保留结果中的原始列 table,您需要更改代码中的三行:

...
    newCols = Table.ToColumns(#"Changed Type") & List.Combine(List.Transform(splitCols, each List.RemoveLastN(_,1))),
    newColNames = Table.ColumnNames(#"Changed Type") & List.Combine(List.Transform(splitCols, each List.Last(_))),
    newTable = Table.FromColumns(newCols,newColNames),
...