Excel 将参数作为文本转换为查询名称?
Excel convert parameter as text to query name?
我有一个需要一些文本输入的函数,我想将其转换为查询名称。所以 source2 应该是查询 #"input text"。我该怎么做呢 ?我在网上到处搜索都找不到答案。
let
fnGetLeague = (leagueName as text) =>
let
Source = #"Matches Today",
Source2 = #"leagueName",
join = Table.NestedJoin(Source, "homeTeam.team_id",Source2, "team_id", "leagueData" )
in
join
in
fnGetLeague
我的主要目标是在我的“今日比赛”查询中加入来自特定查询(不同联赛)的一些数据。
在我的“今天的比赛”查询中,我有一个名为联赛的列,所以我想将其作为变量传递到我的函数中,将其分配给正确的查询,并在我的内部加入来自该特定查询的一些数据匹配今日查询。
最终会有很多联赛查询,所以动态指向正确的查询是我的主要目标。
我现在想避免使用 power pivot,因为它太慢了。
我将构建一个查询,将所有联赛的所有数据附加在一起,每组行都有一个 leagueName 列,其中填充了适当的值,例如“希腊 - 超级联赛 2”。
然后我将添加一个引用您的 leagueName 参数的过滤行步骤,以将输出限制为该联赛的行。
对问题的回答
要通过名称文本引用查询,需要使用相当隐蔽的内部变量 - #sections
。您可以阅读 documentation, but I personally found this Q&A about lists of queries 更有帮助。如问答中所述,使用 #sections
.
时请注意潜在的递归问题
特别是对于您的函数,如下所示更改 Source2
应该可行。但是,请注意该函数会出错
如果没有具有指定名称的查询,则退出。
let
fnGetLeague = (leagueName as text) =>
let
Source = #"Matches Today",
Source2 = List.First(Table.SelectRows(Record.ToTable(#sections[Section1]), each [Name] = leagueName)[Value]),
join = Table.NestedJoin(Source, "homeTeam.team_id",Source2, "team_id", "leagueData" )
in
join
in
fnGetLeague
注意: 此函数将 return 一个新查询,该查询执行从 #"Matches Today"
到指定查询的左外连接。这不会以任何方式改变 #"Matches Today"
。
如果您想将来自联赛特定查询的数据直接添加到 #"Matches Today"
(而不是“副本”),那么我会推荐以下方法之一。
备选方案
如下所示更改函数,然后通过#"Matches Today" 查询中的 table 选项调用该函数。这会将数据添加到 #"Matches Today" 查询,而不是创建新查询。
let
fnGetLeague = (leagueName as text, teamID as number) =>
let
Source = Record.ToTable(#sections[Section1]),
LeagueTable = List.First(Table.SelectRows(Source, each [Name] = leagueName)[Value]),
LeagueTeam = if Value.Is(LeagueTable, type table) then Table.SelectRows(LeagueTable, each [team_id] = teamID) else null
in
LeagueTeam
in
fnGetLeague
调用自定义函数
疑难解答
根据函数结果,您可能看不到“table-列”选项 (Expand/Aggregate)。如果是这样,您有时可以通过在 Table.AddColumn()
函数(调用自定义函数时创建)的末尾添加 , type table
来强制它,如下所示。
如果 expand/aggregate 按钮可见,但尝试使用它会导致
No columns were found
消息,那么有两种可能的选择:
- 手动添加适当的 expand/aggregate 步骤。
- 创建一个“空白”table 由函数 return 编辑而不是 null
手动步骤
扩展
添加手动扩展有几个主要简单的步骤:
添加自定义步骤。这可以通过右键单击您希望手动步骤遵循的步骤,然后单击“插入步骤”来完成选项。
自定义步骤将具有等式 = #"Previous Step"
(其中 #"Previous Step" 将是上一步的 table。如果您直接在后面添加自定义步骤源,等式将是 = Source
,等等)。
用 Table.ExpandTableColumn()
方程 (PQ Doc) 替换自定义步骤中的现有方程。例如,如果我想从现有的 table 列“Data”中扩展“count”和“Price”列(并假设上一步是“Grouped Data”),那么扩展方程看起来像
= Table.ExpandTableColumn(#"Grouped Data", "Data", {"count", "Price"})
如果我想将“count”重命名为“units”,则等式为
= Table.ExpandTableColumn(#"Grouped Data", "Data", {"count", "Price"}, {"units", "Price"})
聚合
对于聚合,有两个选项:
- 扩展相关列,然后添加分组步骤
- 使用
Table.AggregateTableColumn()
等式(PQ Doc)
例如,从“数据”列(上一步是“分组数据”)中查找最大“价格”将是:
= Table.AggregateTableColumn(#"Grouped Data", "Data", {{"Price", List.Max, "max of Price"}})
文档中有更多示例。
空白table
如果您感兴趣的列不会发生变化,但潜在的聚合或扩展可能会发生变化,则此方法可能是最佳选择。即,如果联盟 table 的格式和列名称在 table 之间基本一致,并且不应更改。
与其使用自定义函数 return null
,不如 return 一个“空白” table - 具有所需列的 table,但没有行。例如,如果“空白”table 应该有一个“价格”列和一个“名称”列,则空白 table 的代码将是:
#table(type table [#"price"=number, #"name"=text], {})
但是,由于用空 table 扩展一列会导致一行空值,因此等效于使用:
#table(type table [#"price"=number, #"name"=text], {{null, null}})
您还可以选择使用零或空字符串作为 table 的种子:
#table(type table [#"price"=number, #"name"=text], {{0, ""}})
下面,我有一个自定义函数的副本 return 在空白 table 中包含列 {price, name} 而不是 null LeagueTeam 代码行:
let
fnGetLeague = (leagueName as text, teamID as number) =>
let
Source = Record.ToTable(#sections[Section1]),
LeagueTable = List.First(Table.SelectRows(Source, each [Name] = leagueName)[Value]),
LeagueTeam = if Value.Is(LeagueTable, type table) then Table.SelectRows(LeagueTable, each [team_id] = teamID) else #table(type table [#"price"=number, #"name"=text], {})
in
LeagueTeam
in
fnGetLeague
我有一个需要一些文本输入的函数,我想将其转换为查询名称。所以 source2 应该是查询 #"input text"。我该怎么做呢 ?我在网上到处搜索都找不到答案。
let
fnGetLeague = (leagueName as text) =>
let
Source = #"Matches Today",
Source2 = #"leagueName",
join = Table.NestedJoin(Source, "homeTeam.team_id",Source2, "team_id", "leagueData" )
in
join
in
fnGetLeague
我的主要目标是在我的“今日比赛”查询中加入来自特定查询(不同联赛)的一些数据。
在我的“今天的比赛”查询中,我有一个名为联赛的列,所以我想将其作为变量传递到我的函数中,将其分配给正确的查询,并在我的内部加入来自该特定查询的一些数据匹配今日查询。
最终会有很多联赛查询,所以动态指向正确的查询是我的主要目标。
我现在想避免使用 power pivot,因为它太慢了。
我将构建一个查询,将所有联赛的所有数据附加在一起,每组行都有一个 leagueName 列,其中填充了适当的值,例如“希腊 - 超级联赛 2”。
然后我将添加一个引用您的 leagueName 参数的过滤行步骤,以将输出限制为该联赛的行。
对问题的回答
要通过名称文本引用查询,需要使用相当隐蔽的内部变量 - #sections
。您可以阅读 documentation, but I personally found this Q&A about lists of queries 更有帮助。如问答中所述,使用 #sections
.
特别是对于您的函数,如下所示更改 Source2
应该可行。但是,请注意该函数会出错
如果没有具有指定名称的查询,则退出。
let
fnGetLeague = (leagueName as text) =>
let
Source = #"Matches Today",
Source2 = List.First(Table.SelectRows(Record.ToTable(#sections[Section1]), each [Name] = leagueName)[Value]),
join = Table.NestedJoin(Source, "homeTeam.team_id",Source2, "team_id", "leagueData" )
in
join
in
fnGetLeague
注意: 此函数将 return 一个新查询,该查询执行从 #"Matches Today"
到指定查询的左外连接。这不会以任何方式改变 #"Matches Today"
。
如果您想将来自联赛特定查询的数据直接添加到 #"Matches Today"
(而不是“副本”),那么我会推荐以下方法之一。
备选方案
如下所示更改函数,然后通过#"Matches Today" 查询中的 table 选项调用该函数。这会将数据添加到 #"Matches Today" 查询,而不是创建新查询。
let
fnGetLeague = (leagueName as text, teamID as number) =>
let
Source = Record.ToTable(#sections[Section1]),
LeagueTable = List.First(Table.SelectRows(Source, each [Name] = leagueName)[Value]),
LeagueTeam = if Value.Is(LeagueTable, type table) then Table.SelectRows(LeagueTable, each [team_id] = teamID) else null
in
LeagueTeam
in
fnGetLeague
调用自定义函数
疑难解答
根据函数结果,您可能看不到“table-列”选项 (Expand/Aggregate)。如果是这样,您有时可以通过在 Table.AddColumn()
函数(调用自定义函数时创建)的末尾添加 , type table
来强制它,如下所示。
如果 expand/aggregate 按钮可见,但尝试使用它会导致
No columns were found
消息,那么有两种可能的选择:
- 手动添加适当的 expand/aggregate 步骤。
- 创建一个“空白”table 由函数 return 编辑而不是 null
手动步骤
扩展
添加手动扩展有几个主要简单的步骤:
添加自定义步骤。这可以通过右键单击您希望手动步骤遵循的步骤,然后单击“插入步骤”来完成选项。
自定义步骤将具有等式
= #"Previous Step"
(其中 #"Previous Step" 将是上一步的 table。如果您直接在后面添加自定义步骤源,等式将是= Source
,等等)。用
Table.ExpandTableColumn()
方程 (PQ Doc) 替换自定义步骤中的现有方程。例如,如果我想从现有的 table 列“Data”中扩展“count”和“Price”列(并假设上一步是“Grouped Data”),那么扩展方程看起来像= Table.ExpandTableColumn(#"Grouped Data", "Data", {"count", "Price"})
如果我想将“count”重命名为“units”,则等式为
= Table.ExpandTableColumn(#"Grouped Data", "Data", {"count", "Price"}, {"units", "Price"})
聚合
对于聚合,有两个选项:
- 扩展相关列,然后添加分组步骤
- 使用
Table.AggregateTableColumn()
等式(PQ Doc)
例如,从“数据”列(上一步是“分组数据”)中查找最大“价格”将是:
= Table.AggregateTableColumn(#"Grouped Data", "Data", {{"Price", List.Max, "max of Price"}})
文档中有更多示例。
空白table
如果您感兴趣的列不会发生变化,但潜在的聚合或扩展可能会发生变化,则此方法可能是最佳选择。即,如果联盟 table 的格式和列名称在 table 之间基本一致,并且不应更改。
与其使用自定义函数 return null
,不如 return 一个“空白” table - 具有所需列的 table,但没有行。例如,如果“空白”table 应该有一个“价格”列和一个“名称”列,则空白 table 的代码将是:
#table(type table [#"price"=number, #"name"=text], {})
但是,由于用空 table 扩展一列会导致一行空值,因此等效于使用:
#table(type table [#"price"=number, #"name"=text], {{null, null}})
您还可以选择使用零或空字符串作为 table 的种子:
#table(type table [#"price"=number, #"name"=text], {{0, ""}})
下面,我有一个自定义函数的副本 return 在空白 table 中包含列 {price, name} 而不是 null LeagueTeam 代码行:
let
fnGetLeague = (leagueName as text, teamID as number) =>
let
Source = Record.ToTable(#sections[Section1]),
LeagueTable = List.First(Table.SelectRows(Source, each [Name] = leagueName)[Value]),
LeagueTeam = if Value.Is(LeagueTable, type table) then Table.SelectRows(LeagueTable, each [team_id] = teamID) else #table(type table [#"price"=number, #"name"=text], {})
in
LeagueTeam
in
fnGetLeague