文本数字混排的求和(Power Query)

发布时间 2023-11-12 17:47:01作者: 熬肥妖

问题1:有明确的分隔符

let
    源 = Excel.CurrentWorkbook(){[Name="表1"]}[Content],
    行号 = Table.AddIndexColumn(源, "行号"),
    按分号拆分成行 = Table.ExpandListColumn(Table.TransformColumns(行号, {{"哪里来的数据?1", Splitter.SplitTextByDelimiter(";"), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "哪里来的数据?1"),
    汉字与数字分开 = Table.SplitColumn(按分号拆分成行, "哪里来的数据?1", Splitter.SplitTextByCharacterTransition((c) => not List.Contains({"0".."9"}, c), {"0".."9"}), {"哪里来的数据?1.1", "哪里来的数据?1.2"}),
    更改的类型 = Table.TransformColumnTypes(汉字与数字分开,{{"哪里来的数据?1.2", Int64.Type}}),
    按行号求和 = Table.Group(更改的类型, {"行号"}, {"求和", each List.Sum([#"哪里来的数据?1.2"])}),
    删除的其他列 = Table.SelectColumns(按行号求和,{"求和"})
in
    删除的其他列

问题2:没有明确的分隔符

let
    源 = Excel.CurrentWorkbook(){[Name="表2"]}[Content],
    已添加索引 = Table.AddIndexColumn(源, "行号"),
    按各各种拆分成列 = Table.SplitColumn(已添加索引, "哪里来的数据?2", Splitter.SplitTextByCharacterTransition({"0".."9"}, (c) => not List.Contains({"0".."9"}, c)), {"哪里来的数据?2.1", "哪里来的数据?2.2", "哪里来的数据?2.3", "哪里来的数据?2.4"}),
    逆透视转表结构 = Table.UnpivotOtherColumns(按各各种拆分成列, {"行号"}, "属性", "值"),
    汉字与数字分列 = Table.SplitColumn(逆透视转表结构, "值", Splitter.SplitTextByCharacterTransition((c) => not List.Contains({"0".."9"}, c), {"0".."9"}), {"值.1", "值.2"}),
    更改的类型 = Table.TransformColumnTypes(汉字与数字分列,{{"值.2", Int64.Type}}),
    按行号求和 = Table.Group(更改的类型, {"行号"}, {"求和", each List.Sum([值.2])}),
    删除的其他列 = Table.SelectColumns(按行号求和,{"求和"})
in
    删除的其他列