table 中多列乘以同一列的递归表达式
Recursive expression for multiply multiple columns by same column in table
我有一个 table - 每行都有不同类别的百分比列和总价值列,我想通过将总价值乘以每个类别百分比来计算每个类别的价值。
我有一个 table,并且对于其他每一列(Market Cap.Large
、Market Cap.Medium
等 - 除了 Symbol
)我想将它相乘通过 Current Value
列。我可以手动完成并为每个添加一列(下面的 #"Added Custom"
行),但列数可能会增加并且标题可能会更改,所以我不想为每个输入自定义列.
是否有递归表达式为每个现有列创建一个新列,并将其乘以 Current Value
- 我猜它会涉及为列名创建一个参数,但我'我不太了解如何去做。
table
let
Source = #"Portfolio %",
#"Merged Queries" = Table.NestedJoin(Source,{"Symbol"},#"Current Output Aggregated",{"Symbol"},"Current Output Aggregated",JoinKind.Inner),
#"Expanded Current Output Aggregated" = Table.ExpandTableColumn(#"Merged Queries", "Current Output Aggregated", {"Current Value"}, {"Current Value"}),
#"Added Custom" = Table.AddColumn(#"Expanded Current Output Aggregated", "Market Cap.Giant Value", each [Market Cap.Giant]*[Current Value])
in
#"Added Custom"
未经测试,写在phone,但我认为你想要做的是:
创建要乘以 Current Value
列的 List
列(所有列除外:Symbol
、Current Value
)。
使用 List.Accumulate
遍历这个新创建的 List
,并在这样做时为列表中的每一列添加一列(通过调用 Table.AddColumn
每次循环迭代)。
所以,类似于下面的 M
代码(如有必要,将下面的代码复制粘贴到 Advanced Editor
):
let
Source = #"Portfolio %",
mergedQueries = Table.NestedJoin(Source,{"Symbol"},#"Current Output Aggregated",{"Symbol"},"Current Output Aggregated",JoinKind.Inner),
expandedNested = Table.ExpandTableColumn(mergedQueries, "Current Output Aggregated", {"Current Value"}, {"Current Value"}),
allHeaders = Table.ColumnNames(expandedNested),
headersToLoopOver = List.RemoveItems(allHeaders, {"Symbol", "Current Value"})
loopOverList = List.Accumulate(headersToLoopOver, expandedNested, (tableState, currentColumn) =>
Table.AddColumn(tableState, currentColumn & " Value", each Record.Field(_, currentColumn) * [Current Value], type number)
)
in
loopOverList
我不确定我的Record.Field
位是否正确(在循环中)而且我无法访问机器进行测试,但您应该很容易检查输出是否正确如你所愿。
我有一个 table - 每行都有不同类别的百分比列和总价值列,我想通过将总价值乘以每个类别百分比来计算每个类别的价值。
我有一个 table,并且对于其他每一列(Market Cap.Large
、Market Cap.Medium
等 - 除了 Symbol
)我想将它相乘通过 Current Value
列。我可以手动完成并为每个添加一列(下面的 #"Added Custom"
行),但列数可能会增加并且标题可能会更改,所以我不想为每个输入自定义列.
是否有递归表达式为每个现有列创建一个新列,并将其乘以 Current Value
- 我猜它会涉及为列名创建一个参数,但我'我不太了解如何去做。
table
let
Source = #"Portfolio %",
#"Merged Queries" = Table.NestedJoin(Source,{"Symbol"},#"Current Output Aggregated",{"Symbol"},"Current Output Aggregated",JoinKind.Inner),
#"Expanded Current Output Aggregated" = Table.ExpandTableColumn(#"Merged Queries", "Current Output Aggregated", {"Current Value"}, {"Current Value"}),
#"Added Custom" = Table.AddColumn(#"Expanded Current Output Aggregated", "Market Cap.Giant Value", each [Market Cap.Giant]*[Current Value])
in
#"Added Custom"
未经测试,写在phone,但我认为你想要做的是:
创建要乘以
Current Value
列的List
列(所有列除外:Symbol
、Current Value
)。使用
List.Accumulate
遍历这个新创建的List
,并在这样做时为列表中的每一列添加一列(通过调用Table.AddColumn
每次循环迭代)。
所以,类似于下面的 M
代码(如有必要,将下面的代码复制粘贴到 Advanced Editor
):
let
Source = #"Portfolio %",
mergedQueries = Table.NestedJoin(Source,{"Symbol"},#"Current Output Aggregated",{"Symbol"},"Current Output Aggregated",JoinKind.Inner),
expandedNested = Table.ExpandTableColumn(mergedQueries, "Current Output Aggregated", {"Current Value"}, {"Current Value"}),
allHeaders = Table.ColumnNames(expandedNested),
headersToLoopOver = List.RemoveItems(allHeaders, {"Symbol", "Current Value"})
loopOverList = List.Accumulate(headersToLoopOver, expandedNested, (tableState, currentColumn) =>
Table.AddColumn(tableState, currentColumn & " Value", each Record.Field(_, currentColumn) * [Current Value], type number)
)
in
loopOverList
我不确定我的Record.Field
位是否正确(在循环中)而且我无法访问机器进行测试,但您应该很容易检查输出是否正确如你所愿。