嗨,我目前正在将 Excel 公式转换为 Power 查询,但遇到了一些麻烦。这个想法很简单,但我无法让公式发挥作用,因为强力查询不支持递归。
这是我原来的Excel公式:
IF(AND[@[less than year]]<=EOMONTH([@[Month]],0),[@[id]]=C2,[@[MaxCap]]+P2,IF(AND([@[less than year]]<=EOMONTH([@[Month]],0),[@[id]]<>C2,[@[MaxCap]],0)
基本上它检查“小于年份”中的日期是否小于该月的月底,并且 ID 是否与上面一行相同。如果为 true,则将当前行的 MaxCap 与上一行的 Sum Value 相加 (p2)。如果为 false,则再次检查日期条件,如果 ID 与上面的 ID 不同,如果为 true,则返回 MaxCap,如果为 false,则返回 0。
这就是我在 Power Query 中的内容,我创建了一个名为 Sum 的新列,公式如下所示
if [less than year] <= Date.EndofMonth([Month])
and [id] = #"Added Index"{[Index]-1}[id]
then [MaxCap] + #"Added Index"{[Index]-1}[Sum]
else if [less than year] <= Date.EndofMonth([Month])
and [id] <> #"Added Index"{[Index]-1}[id]
then [MaxCap]
else 0
发生的情况是流入 [MaxCap] + "Added Index"{[Index]-1}[Sum] 的值触发了错误,因为它无法读取 [Sum] 列。公式的其他部分很好。
我现在不确定如何重写这个公式,感谢任何帮助。
样本数据
EMPID RCD ID FIRSTNAME EMP LASTNAME CALC GROUP Month TOTAL WORK HOURS MaxCap Anniversary BUSINESS TITLE Grade Grade Value Start Day less than year Sum
12345 0 12345-0 First Name Last Name C 2023-09-01 144.91 144.91 8-5-2023 Basketball Player B1 1 2022-7-11 2022-7-11 2028.76
12345 0 12345-0 First Name Last Name C 2023-10-01 84 84 8-5-2023 Basketball Player B1 1 2022-7-11 2022-7-11 2112.76
12345 0 12345-0 First Name Last Name C 2023-11-01 134.62 134.62 8-5-2023 Basketball Player B1 1 2022-7-11 2022-7-11 2247.38
12345 0 12345-0 First Name Last Name C 2023-12-01 144.25 144.25 8-5-2023 Basketball Player B1 1 2022-7-11 2022-7-11 2391.63
12345 0 12345-0 First Name Last Name C 2024-01-01 120.67 120.67 8-5-2023 Basketball Player B1 1 2022-7-11 2022-7-11 2512.3
12345 0 12345-0 First Name Last Name C 2024-02-01 62.83 62.83 8-5-2023 Basketball Player B1 1 2022-7-11 2022-7-11 2575.13
此数据将加载到 power query 中,其中 Sum 列将被删除并使用上面的公式创建。
结果会和我提供的数据表一样。
问题正如 horseyride 提到的“如果 Sum 是您正在创建的列的名称,那么您就无法让创建该列的公式引用其自身。”有没有另一种方法可以获得相同的结果而不遇到这个问题?
看来您想要的是每个 ID 的最大上限的运行总计。
我不知道您的日期考虑因素应该是什么,因为您所有的
less than year
日期都远远早于任何 month
日期。
此外,我认为您示例中的
SUM
列不可能从该示例中派生出来,但我认为这是不正确的。
要仅根据
ID
列向表中添加“运行总计”列,您
List.Generate
创建自定义聚合,它可以引用同一列中的前几行。代码看起来像这样:
#"Grouped Rows" = Table.Group(#"Previous Step", {"ID"}, {
{"Sum", (t)=> let
#"Running Total" = List.Generate(
()=>[rt=t[MaxCap]{0}, idx=0],
each [idx] < Table.RowCount(t),
each [rt = [rt]+t[MaxCap]{[idx]+1}, idx = [idx]+1],
each [rt]),
#"Add Sum Column" = Table.FromColumns(
Table.ToColumns(t)
& {#"Running Total"}, Table.ColumnNames(t) & {"Sum"})
in
#"Add Sum Column"
,type table [EMPID=nullable number, RCD=nullable number, ID=nullable text, FIRSTNAME=nullable text,
LASTNAME=nullable text, CALC GROUP=nullable text, Month=nullable date, TOTAL WORK HOURS=nullable number,
MaxCap=nullable number, Anniversary=nullable number, BUSINESS TITLE=nullable text,
Grade=nullable text, Grade Value=nullable number, Start Day=nullable date,
less than year=nullable date, Sum=nullable number] }}),
然后,您可以展开表格(除了已经存在的 ID 列)。
如果您可以提供您尝试使用日期过滤器执行的操作的示例,那么当然可以将其添加到自定义聚合中。
使用您发布的示例和此代码:
let
Source = Excel.CurrentWorkbook(){[Name="Table36"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{
{"EMPID", Int64.Type}, {"RCD", Int64.Type}, {"ID", type text}, {"FIRSTNAME", type text},
{"LASTNAME", type text}, {"CALC GROUP", type text}, {"Month", type date},
{"TOTAL WORK HOURS", type number}, {"MaxCap", type number}, {"Anniversary", Int64.Type},
{"BUSINESS TITLE", type text}, {"Grade", type text}, {"Grade Value", Int64.Type},
{"Start Day", type date}, {"less than year", type date}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {
{"Sum", (t)=> let
#"Running Total" = List.Generate(
()=>[rt=t[MaxCap]{0}, idx=0],
each [idx] < Table.RowCount(t),
each [rt = [rt]+t[MaxCap]{[idx]+1}, idx = [idx]+1],
each [rt]),
#"Add Sum Column" = Table.FromColumns(
Table.ToColumns(t)
& {#"Running Total"}, Table.ColumnNames(t) & {"Sum"})
in
#"Add Sum Column"
,type table [EMPID=nullable number, RCD=nullable number, ID=nullable text, FIRSTNAME=nullable text,
LASTNAME=nullable text, CALC GROUP=nullable text, Month=nullable date, TOTAL WORK HOURS=nullable number,
MaxCap=nullable number, Anniversary=nullable number, BUSINESS TITLE=nullable text,
Grade=nullable text, Grade Value=nullable number, Start Day=nullable date,
less than year=nullable date, Sum=nullable number] }}),
#"Expanded Sum" = Table.ExpandTableColumn(#"Grouped Rows", "Sum", List.RemoveItems(Table.ColumnNames(#"Changed Type"),{"ID"}) & {"Sum"})
in
#"Expanded Sum"