整理房号(Power Query)

发布时间 2023-04-09 18:38:31作者: 熬肥妖

问题:

let
    源 = Excel.CurrentWorkbook(){[Name="表1"]}[Content],
    替换租户 = Table.ReplaceValue(源,"租户","",Replacer.ReplaceText,{"房号"}),
    替换二次装修 = Table.ReplaceValue(替换租户,"(二次装修)","",Replacer.ReplaceText,{"房号"}),
    幢号和房号拆分 = Table.SplitColumn(替换二次装修, "房号", Splitter.SplitTextByPositions({0, 3}), {"房号.1", "房号.2"}),
    替换南1 = Table.ReplaceValue(幢号和房号拆分,"南1-"," ",Replacer.ReplaceText,{"房号.2"}),
    按空格拆分成行 = Table.ExpandListColumn(Table.TransformColumns(替换南1, {"房号.2", Splitter.SplitTextByDelimiter(" "), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}), "房号.2"),
    筛选掉空行 = Table.SelectRows(按空格拆分成行, each ([房号.2] <> "")),
    为房号分类 = Table.AddColumn(筛选掉空行, "自定义", each if Text.Length([房号.2]) < 5 then "已完成" else if Text.Length([房号.2]) < 10 then "待展开" else "待拆分"),
    添加索引 = Table.AddIndexColumn(为房号分类, "索引"),
    透视列 = Table.Pivot(添加索引, List.Distinct(添加索引[自定义]), "自定义", "房号.2"),
    拆分待展开列 = Table.SplitColumn(透视列, "待展开", Splitter.SplitTextByDelimiter("-"), {"待展开.1", "待展开.2"}),
    生成待展开列表 = Table.AddColumn(拆分待展开列, "自定义", each {Number.FromText([待展开.1])..Number.FromText([待展开.2])}),
    替换错误为空 = Table.ReplaceErrorValues(生成待展开列表, {"自定义", null}),
    展开列 = Table.TransformColumnTypes(Table.ExpandListColumn(替换错误为空, "自定义"),{{"自定义", type text}}),
    拆分待拆分列 = Table.ExpandListColumn(Table.TransformColumns(展开列, {"待拆分", Splitter.SplitTextByDelimiter("-"), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}), "待拆分"),
    合并房号列 = Table.CombineColumns(拆分待拆分列, {"待拆分", "已完成", "自定义"},Combiner.CombineTextByDelimiter(""),"已合并"),
    补齐四位 = Table.AddColumn(合并房号列, "自定义", each Number.ToText(Number.FromText([已合并]),"0000")),
    删除多余列 = Table.RemoveColumns(补齐四位,{"索引", "待展开.1", "待展开.2","已合并"}),
    幢号与房号合并 = Table.CombineColumns(删除多余列,{"房号.1", "自定义"},Combiner.CombineTextByDelimiter(""),"房号"),
    重排序 = Table.ReorderColumns(幢号与房号合并,{"房号", "出售时间"}),
    更改的类型 = Table.TransformColumnTypes(重排序,{{"房号", type text}, {"出售时间", type date}})
in
    更改的类型