Power Query - 删除包含小写字母的文本字符串

Power Query - Remove text strings that contain lower case letters

情况:我在 Power Query 中处理从 pdf 文件导入的数据,它有点乱。我有一列包含数字和文本字符串。一些文本字符串是大小写混合的,包含大写和小写字符,而另一些仅包含大写字符。

目标:我想删除所有数字和所有大小写混合的文本字符串。最终结果应该只显示完全大写的文本字符串。

例如,我希望我的最终结果包括 IRA、IRREVOCABLE TRUST、CHARITABLE TRUST 等内容,但将账户数量、总计、14 等内容替换为 null。

到目前为止我尝试过的:

  1. 以下删除了数字和小写字符,但它不太有效,因为它保留了包含在混合大小写字符串中的大写字符。

    Table.AddColumn(#"Added Custom2", "Account Type" each Text.Select([AccountType], {"A".."Z",""}), type text)

  2. 下面的代码去掉了大小写混合的文本字符串,但效果不佳,因为它没有删除数字。此外,它太具体了,需要我删除包含特定单词的字符串。我更愿意删除所有包含小写字符的字符串。

    Table.AddColumn(#"Added Custom2", "Account Type", each if [AccountType]= null or Text.Contains([AccountType],"Totals") or Text.Contains ([AccountType],"of") 或 Text.Contains([AccountType],"report") 然后 null else [AccountType])

您的见解将不胜感激。我是 PowerQuery 的新用户,所以请具体详细地回答你的问题。

第一个公式只查找所有不包含数字的大写字母

= if [Column1] = Text.Remove ([Column1],{"0".."9","a".."z"}) then [Column1] else null

第二个公式删除所有数字,然后查找所有不包含数字的大写字母

= if Text.Remove ([Column1],{"0".."9"}) = Text.Remove ([Column1],{"a".."z","0".."9"}) then Text.Remove ([Column1],{"0".."9"}) else null

let  Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if [Column1] = Text.Remove ([Column1],{"0".."9","a".."z"}) then [Column1] else null),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom2", each if Text.Remove ([Column1],{"0".."9"}) = Text.Remove ([Column1],{"a".."z","0".."9"}) then Text.Remove ([Column1],{"0".."9"}) else null)
in  #"Added Custom1"

~~~

如果您要从单词列表中解析单词......

这会保留 (a) 之前或之后没有数字,并且 (b) 全部大写的单词

let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each 
Text.Combine(
    List.RemoveNulls(
        List.Transform(Text.Split([Column1]," "), each 
        if  _ = Text.Remove (_,{"a".."z","0".."9"}) then _ else null
        ))," "))
in #"Added Custom"

这会删除所有数字,然后保留全部大写的单词

let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each 
Text.Combine(
    List.RemoveNulls(
        List.Transform(Text.Split(Text.Remove ([Column1],{"0".."9"})," "), each 
        if  _ = Text.Remove (_,{"a".."z"}) then _ else null
        ))," "))
in #"Added Custom"

由于您没有提供数据示例,因此很难确切知道您想要什么。

  • 如果单元格中有多个(space 分隔的)字符串,则可以使用:
   #"Added Custom" = Table.AddColumn(#"Changed Type", "allCaps", each 
        Text.Combine(
            List.Accumulate(Text.Split([Column1]," "),
                            {},
                            (state, current)=>  
                                if List.ContainsAny(
                                        Text.ToList(current),
                                            {"0".."9","a".."z",",",":","?","/","\"," "}) 
                                then state 
                                else state & {current}),", "))
  • 如果单元格中只有一个字符串,则可以使用
  #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each 
        if List.ContainsAny(
                Text.ToList([Column1]),
                {"0".."9","a".."z",",",":","?","/","\"," "}) 
        then null 
        else [Column1])

然后您可以通过在添加的列

中取消选择null来进行过滤

在每种情况下,#"Changed Type"是上一步。如果您的代码不是这种情况,请替换为上一步的实际名称