【问题标题】:How to control variable filters automatically in Power query?如何在 Power 查询中自动控制变量过滤器?
【发布时间】:2016-12-04 05:34:26
【问题描述】:

有没有办法控制过滤器根据给定的值在查询中选择值

让我们说, 我有以下电源查询,数据如下所示

     country          type       value1     value2....
    ----------------------------------------------------------------------
     USA              a1         22         12
     Uk               a2         21         10

现在在电源查询中,我手动过滤这些值,使之像 country="USA", type="a1" 以获取数据透视表。

如何在电源查询中使用用户输入自动过滤值?

用户输入:

        country: [textbox] or workbook cell
        type   : [textbox] or workbook cell

感谢您的帮助,谢谢!

PS:我的工作查询如下所示,

let
   Source = Csv.Document(File.Contents("C:\Users\axlptl\Desktop\abc.csv"),[Delimiter=",", Columns=4, Encoding=1252, QuoteStyle=QuoteStyle.None]),
   UserInput = Excel.CurrentWorkbook(){[Name="Table1"]}[Content][country]{0},
   #"Promoted Headers" = Table.PromoteHeaders(Source),
   #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"country", type text}, {"type", type text}, {"value1", Int64.Type}, {"value2", Int64.Type}}),
   #"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([type] = UserInput))


in
   #"Filtered Rows"

【问题讨论】:

  • 您希望用户输入来自哪里?
  • @AlejandroLopez-Lago-MSFT :修改了问题

标签: excel powerquery vba


【解决方案1】:

尝试将22 放入工作簿单元格(可能在另一张工作表上)并从表格中进行选择。基本上,这样做:Power query & power pivot - empty table and clearing pivot

你会希望你的查询有这样一行:

UserInput = Excel.CurrentWorkbook(){[Name="Table1"]}[Content][country]{0},

【讨论】:

  • 感谢您的解决方案,它适用于我的本地数据集,但是当我在电源查询中使用 SSAS 多维数据集数据时,它显示空行。我在中使用了 (Cube.AttributeMemberId([Dashboard_all.Country]) = "[Dashboard_all].[Country].&[UserInput]" meta [DisplayName = "UserInput"]))
  • @Maddy 太好了,你搞定了。对于 SSAS 查询,请确保使用 UserInput 作为变量,而不是使用 "UserInput" 作为字符串。
  • 我尝试了两种方法,但都不起作用,表格显示为空...................... Cube .AttributeMemberId([Dashboard_all.Country]) = "[Dashboard_all].[Country].&[UserInput]" meta [DisplayName = UserInput]))
  • 代码"[Dashboard_all].[Country].&[UserInput]" 看起来不对。你为什么不问一个新的堆栈溢出问题,显示有效的 ssas 查询、你尝试了什么以及你得到了什么!
【解决方案2】:

您应该为包含 Country 和 Type 参数的单元格创建名称。 假设单元格 B1 是国家,B2 是类型。转到公式/名称管理器并为其分配相应的名称。

然后(假设您的数据表名为 Table1)您可以使用以下 PowerQuery:

let
    Country = Excel.CurrentWorkbook(){[Name="Country"]}[Content][Column1]{0}?,
    Type = Excel.CurrentWorkbook(){[Name="type"]}[Content][Column1]{0}?,
    Table = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Filter = Table.SelectRows(Table, each [country] = Country and [type] = Type)
in
    Filter

还要注意名称,它们是区分大小写的。

【讨论】:

    猜你喜欢
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    相关资源
    最近更新 更多