简体   繁体   English

如何使用Power Query按范围过滤列?

[英]How to filter a column by a range with Power Query?

I want to filter a Power Query column by a range of values. 我想按一系列值过滤Power Query列。 I see that I can manually select or deselect values from the column, but I need to do this automatically absed on a range of values already in the sheet. 我看到我可以手动从列中选择或取消选择值,但是我需要对工作表中已有的一系列值自动放弃。

Let's say the table I'm querying contains 100 unique names. 假设我要查询的表格包含100个唯一名称。 I only want to import rows that match a list of 20 names I have in my sheet. 我只想导入与工作表中包含的20个名称列表匹配的行。

How can I do this with Power Query? 如何使用Power Query执行此操作?

Edit: I can make an excel function to concatenate a list of names into the format needed for the query like this: (= Table.SelectRows(#"Changed Type", each ([Ticket Assignee] ="Name 1" or [Ticket Assignee] ="Name 2"))) . 编辑:我可以使一个excel函数将名称列表连接成查询所需的格式,如下所示: (= Table.SelectRows(#"Changed Type", each ([Ticket Assignee] ="Name 1" or [Ticket Assignee] ="Name 2"))) But still need a way to reference this from the query. 但是仍然需要一种从查询中引用它的方法。

Edit2: 编辑2:

My query: 我的查询:

This errors out with: 出现以下错误:

Expression.Error: We expected a FieldsSelector value. Expression.Error:我们需要一个FieldsSelector值。

let
    names_Source = Excel.CurrentWorkbook(){[Name="namesTable"]}[Content],
    names_Values = names_Source{1},


    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type datetime}, {"Ticket Assignee", type text}, {"# Solved", Int64.Type}}),
    #"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([Ticket Assignee] <> null)),
    #"Filtered Rows1" = Table.SelectRows(#"Changed Type", each Record.HasFields(names_Values, [Ticket Assignee]))
in
    #"Filtered Rows1"

Edit3 : Modified Query: Edit3 :修改后的查询:

Expression.Error: We cannot convert the value null to type Record. Expression.Error:我们无法将null值转换为Record类型。 Details: Value= Type=Type 详细信息:值=类型=类型

let
    names_Source = Excel.CurrentWorkbook(){[Name="namesTable"]}[Content],
    names_Values = Record.FromList(names_Source[Zendesk Name], names_Source[Zendesk Name]),


    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type datetime}, {"Ticket Assignee", type text}, {"# Solved", Int64.Type}}),
    #"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([Ticket Assignee] <> null)),
    #"Filtered Rows1" = Table.SelectRows(#"Filtered Rows", each Record.HasFields(names_Values, [Ticket Assignee]))
in
    #"Filtered Rows1"

Edit4 : My table has blank values that were erroring out as null. Edit4 :我的表具有空白值,这些值错误地显示为null。 I added a line to filter out the null values from my names source 我添加了一行以从名称源中过滤出空值

If the list of items you want to check against is in an Excel worksheet, then you can import the table using From Table and use List.Contains to see if your values are in that table. 如果要检查的项目列表在Excel工作表中,则可以使用List.Contains从表''导入表并使用List.Contains ,其中包含查看值是否在该表中。 If the worksheet table is in WorksheetQuery and the names are in the column Names, then your step would look like: 如果工作表表位于WorksheetQuery中,并且名称位于“名称”列中,那么您的步骤应类似于:

= Table.SelectRows(PreviousStep, each List.Contains(WorksheetQuery[Names], [Ticket Assignee]))

If that ends up being too slow, you can try to convert the column into a record and then using Record.HasFields instead. 如果最终结果太慢,则可以尝试将列转换为记录,然后使用Record.HasFields

Why don't to use join operations? 为什么不使用联接操作? It seems like it works at least not slower, but more clear, in my mind: 在我看来,它似乎至少不会变慢,但变得更加清晰:

let
    names_Source = Excel.CurrentWorkbook(){[Name="namesTable"]}[Content],
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Joind = Table.NestedJoin(Source,{"Ticket Assignee"},names_Source,{"Zendesk Name"},"NewColumn",JoinKind.RightOuter),
    Filtered = Table.RemoveColumns(Joind,{"NewColumn"})
in
    Filtered

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM