【问题标题】:Add column of previous values from table of tables in Power BI / Power Query从 Power BI / Power Query 中的表中添加先前值的列
【发布时间】:2019-02-21 19:31:25
【问题描述】:

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

我也看过 hereherehere

  1. Per Max,我已经创建了表格:

    • 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. 我已成功添加引用当前 [Date] 行的列:

    • 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             |

【问题讨论】:

  • 这是一个很好的问题,但是如果您可以在起始数据表的示例表中进行编辑以及您期望的结果,将会得到很大的改进。
  • Alexis- 很抱歉将表格添加为 html,我将提高我的降价技能并更新帖子
  • 我为你修好了。
  • 谢谢。 (尝试)

标签: powerbi powerquery m


【解决方案1】:

类似于我的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

【讨论】:

  • 这绝对有效,我在表中添加列的原因是我试图避免左连接,这会使拉的行数加倍。我还有一些其他合并正在聚合“总出现”(过滤出现 1 的 3、2 的 3 等)、“第一次出现的日期等”,它们已经对 1M+ 行进行了约 200K 行样本查询。现在不是主要问题,但我担心当我将它应用到我的数据中的所有 40M 行时,这可能会成倍增加(和变慢)。
  • 我很确定自合并方法将比任何类型的索引移动或查找方法更有效,因为在大多数查询语言中合并通常是高度优化的操作。此外,我的建议不涉及创建任何额外的行。
  • 我一直在修改自合并,添加此列之前的基线加载时间约为 40 秒(210K 行)。尽管最终输出仍然只有 210K 行,但合并会使引用的行加倍并使加载时间加倍——它实际上必须查看数据两次才能合并它。我有另一个执行类似操作的合并,它使我的加载时间和数据中引用的行数增加了四倍(输出仍然只是原始的 210K 行)。我尚未针对我的完整数据集运行此解决方案,但我担心我将处理数小时的加载时间。
  • 我不能保证这会像你喜欢的那样执行(这可能是不可能的),但我非常有信心自合并将比索引查找有更好的性能.后者往往不会被查询引擎很好地优化。
  • 公平——我想知道这是否会更好地使用数据模型中的 DAX 计算列来处理。似乎 Power Query 中的合并会导致过多的性能问题以及其他快速加载。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多