同一列上一行的参考值

Reference value of previous row in same column

我有一个数据集,它通过列 Col1 标识单独的 table,其中 %T 行值表示这是一个新的 table(以及名称在 Col2 处提供),%R 表示这是 table 中的一条记录(并且值在 Col2 处提供)。

具有两个 table 的数据集将如下所示:

Col1 Col2
%T Table1
%R something
%R else
%R etc
%T Table2
%R other value
%R one more
%R etc

我希望能够将这些 table 分开。我的计划是计算一个新列,其中 table 名称将出现,然后我可以 Group By 此列并获得 All,这应该 return 2 个子集,然后我可以单独使用。

我卡在了这一步,我需要在 Col1 = "%R" 的所有行中传播 table 名称。理想情况下,我会使用以下公式添加名为 Added Custom Column 的步骤:

= Table.AddColumn(#"Added Index", "TableName", each if [Col1] = "%T" then [Col2] else #"Added Custom Column"[TableName]{[Index]-1})

但这不起作用,因为 #"Added Custom Column" 尚不存在,我知道这相当于生成某种循环引用。

在常规 Excel 中,我会在 Col1 之前插入一列并执行 =if(RC[1]="%T",RC[2],R[-1]C)。如何使用 Power Query 实现此目的?

根据我的评论,鉴于您的数据:

let
    Source = Excel.CurrentWorkbook(){[Name="Table7"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Col1", type text}, {"Col2", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if [Col1] = "%T" then [Col2] else null),
    #"Filled Down" = Table.FillDown(#"Added Custom",{"Custom"}),
    #"Grouped Rows" = Table.Group(#"Filled Down", {"Custom"}, {
        {"all", each _, type table [Col1=nullable text, Col2=nullable text, Custom=text]}
        })
in
    #"Grouped Rows"

将=>两个分组tables

如果您不希望第一行(名称为 table 的行)位于子组 table 中,只需更改聚合以将其删除:

{"all", each Table.RemoveFirstN(_,1), type table [Col1=nullable text, Col2=nullable text, Custom=text]}