【问题标题】:Join two tables in PowerQuery by a date range按日期范围连接 PowerQuery 中的两个表
【发布时间】:2020-10-06 13:03:28
【问题描述】:

我在 PowerBI 中有这两个表,一个存储员工支付的款项,另一个存储这些员工类别。我需要加入这两个表才能获得付款所指的类别。问题是Associates可以更改类别,我需要知道用户在付款日期的类别。所以我需要通过AssociateNoPaymentDate 加入这两个表,这样我将在Associations 中为每个Payments 记录一条记录,其中PaymentDateStartEnd 之间。

Payments
ID      AssociateNo    PaymentDate           Value
1       1001           01/oct/2020    100,00
2       1001           02/oct/2020    120,00
3       1001           02/oct/2020    150,00
4       1002           04/oct/2020    160,00
5       1003           05/oct/2020    150,00
6       1004           10/nov/2020    120,00

Associations

AssociateNo   Start        End          Category
1001          10/01/2020   11/01/2020   A
1001          11/01/2020   11/01/2020   A
1001          10/01/2020   11/01/2020   A

PS1:我需要在 PowerQuery 中执行此操作,因为我将执行需要此数据的进一步步骤。
PS1:Associations 表在我可以控制的 mysql 数据库中,所以如果有帮助,我可以在其中创建一个视图。

【问题讨论】:

    标签: powerbi powerquery


    【解决方案1】:

    在关联表中,通过单击选择并使用转换...数据类型...十进制数将两个日期列转换为数字

    然后添加列...自定义列...使用公式

    = {[Start] .. [End]}
    

    单击新列标题顶部的箭头,然后从下拉列表中选择展开到新行

    通过单击选择并使用转换...数据类型...日期将两个原始日期列和新列转换回日期格式

    现在您有了开始日期和结束日期之间的所有可能日期。文件...关闭并加载...要保存的数据

    进入 Payments 表并使用 Left Outer 类型从该表和 Associations 表中执行 Home ... Merge Queries ... ,将 AssociateNo 链接在一起,并将 PaymentDate 链接到我们创建的新列。展开并提取类别

    关联表的示例代码

    let Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"AssociateNo", Int64.Type}, {"Start", Int64.Type}, {"End", Int64.Type}, {"Category", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each {[Start] .. [End]}),
    #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Custom",{{"Start", type date}, {"End", type date}, {"Custom", type date}})
    in #"Changed Type1"
    

    付款表的示例代码

    let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Merged Queries" = Table.NestedJoin(Source,{"AssociateNo", "PaymentDate"},Associations,{"AssociateNo", "Custom"},"Associations",JoinKind.LeftOuter),
    #"Changed Type" = Table.TransformColumnTypes(#"Merged Queries",{{"PaymentDate", type date}}),
    #"Expanded Associations" = Table.ExpandTableColumn(#"Changed Type", "Associations", {"Category"}, {"Category"})
    in  #"Expanded Associations"
    

    【讨论】:

      猜你喜欢
      • 2018-12-19
      • 1970-01-01
      • 2015-10-31
      • 2017-08-17
      • 2020-01-07
      • 2014-07-20
      • 1970-01-01
      • 2022-08-17
      相关资源
      最近更新 更多