在Power BI / Power Query

时间:2019-02-21 19:31:26

标签: powerbi powerquery m

正在寻找Max Zelensky's解决方案here的后续活动。假设原始示例的[Date]字段为空,我尝试再上一层,并添加一列以显示先前的[Date]值

我也看过hereherehere

  1. 每个最大值,我已经创建了表格表:

    • AddedCustom = Table.AddColumn(GroupedRows, "Custom", each Table.AddIndexColumn([tmp],"Occurrence", 1,1) type table)
  2. 创建了第二个索引:

    • SecondIndex= Table.AddColumn(AddedCustom, "Custom.2", each Table.AddIndexColumn([Custom],"Occurance.2", 0,1), type table)
  3. 我已经成功添加了引用当前[日期]行的列:

    • CurrentDate= Table.AddColumn(SecondIndex, "Date.2", each Table.AddColumn([Custom.2],"Date.2", each [Date]), type table)
  4. 但是当我尝试引用任一索引列(即使只是放入{0})时,新字段也会出错。我可以肯定地说我在表的列中引用表中的行的语法中缺少一些东西,但是我不确定如何到达那里-我尝试过的一些例子没有成功:

    • PriorDate= Table.AddColumn(SecondIndex, "PriorDate", each Table.AddColumn([Custom.2],"Prior Date", each {0}[Date]), type table) -只是看看我是否可以从第一行返回该值

    • PriorDate= Table.AddColumn(SecondIndex, "PriorDate", each Table.AddColumn([Custom.2],"Prior Date", each {[Occurance.2]}[Date]), type table)-不适用于[Occurance]或[Occurance.2]

    • PriorDate= Table.AddColumn(SecondIndex, "PriorDate", each Table.AddColumn([Custom.2],"Prior Date", each {[Occurance]-1}[Date]), type table)

    • PriorDate= Table.AddColumn(SecondIndex, "PriorDate", each Table.AddColumn([Custom.2],"Prior Date", each [Custom.2]{0}[Date]), type table)

    • PriorDate= Table.AddColumn(SecondIndex, "PriorDate", each Table.AddColumn([Custom.2],"Prior Date", each Table.SelectColums([Custom.2],[Date])), type table)

此外,还有谁能为我提供有关优化#Tables,{Lists},[Records]等的语法和机制的良好参考。我将不胜感激(我已经读了Ken Puls的书的第20章,时间,但尚未完全停滞)。预先感谢!

| Name | Date     | Occurance | Prior Date (Desired) |
|------|----------|-----------|----------------------|
| A    | 1/1/2019 | 1         | null/error           |
| A    | 3/1/2019 | 2         | 1/1/2019             |
| B    | 2/1/2019 | 1         | null/error           |
| A    | 4/1/2019 | 3         | 3/1/2019             |
| B    | 5/1/2019 | 2         | 2/1/2019             |

1 个答案:

答案 0 :(得分:0)

类似于我的answer here,您可以添加两个索引,而不是仅添加一个索引,一个从0开始,另一个从1开始,我们用它来计算上一行通过执行自我合并。

let
    Source = Table.FromRows({{"A",#date(2019,1,1)},{"A",#date(2019,1,3)},{"B",#date(2019,1,2)},{"A",#date(2019,1,4)},{"B",#date(2019,1,5)}}, {"Name", "Date"}),
    ChangeTypes = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Date", type date}}),
    GroupByName = Table.Group(ChangeTypes, {"Name"}, {{"tmp", each _, type table}}),
    AddIndices = Table.AddColumn(GroupByName, "Custom", each Table.AddIndexColumn(Table.AddIndexColumn([tmp],"Occurrence", 1,1),"Prev",0,1)),
    ExpandTables = Table.ExpandTableColumn(AddIndices, "Custom", {"Date", "Occurrence", "Prev"}, {"Date", "Occurrence", "Prev"}),
    SelfMerge = Table.NestedJoin(ExpandTables,{"Name", "Prev"},ExpandTables,{"Name", "Occurrence"},"Expanded Custom",JoinKind.LeftOuter),
    ExpandPriorDate = Table.ExpandTableColumn(SelfMerge, "Expanded Custom", {"Date"}, {"Prior Date"}),
    RemoveExtraColumns = Table.RemoveColumns(ExpandPriorDate,{"Prev", "tmp"})
in
    RemoveExtraColumns