Excel和Power查询:使用最新日期合并

发布于 2025-01-28 18:49:41 字数 691 浏览 1 评论 0原文

我能够使用帐户编号作为我的钥匙来合并(内部加入)交易表与所有者信息表,以获取以下结果:

“在此处输入图像说明”

我需要链接到当前所有者的交易(s )帐户。但是,如您所见,对于9/16/2016的交易,它也链接到没有该帐户直到很久以后才拥有该帐户的所有者。同样,需要将11/27/2020交易链接到新所有者,所以我正在寻找类似的东西:

“在此处输入图像说明”

由于二级所有者没有改变,因此玛丽适用于两项交易。

对于其他帐户,在次要所有者更改时,主要所有者也可以保持相同。也有最多有4个二级所有者的帐户。

因此,简而言之,我需要交易日期才能与先前的最新所有者更改日期匹配。

我是电源查询的新手,因此我不知道使用PQ或简单地使用Excel函数/公式更好。还是我需要采取其他数据操纵/转换步骤?

I was able to merge(inner join) a transaction table with an owner info table using account number as my key to get the following results:

enter image description here

I need the transactions to be linked to the current owner(s) of the account. But as you can see, for the 9/16/2016 transaction, it is also linked to an owner who did not own the account until much later. Similarly, the 11/27/2020 transaction needs to be linked to the newer owner, so I am looking for something like this:

enter image description here

Since the secondary owner does not change, Mary applies to both transactions.

For other accounts, it is also possible for the primary owner to remain the same while the secondary owner has changed. There are also accounts in which there are up to 4 secondary owners.

So, in short, I need the transaction dates to match up with the previous, most recent Owner Change Date for both primary and secondary owner(s).

I am new to Power Query, so I do not know whether this is better done using PQ or simply Excel functions/formulas. Or maybe there are additional data manipulation/transformation steps I need to take before this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

来日方长 2025-02-04 18:49:41

假设您从2个表开始,看起来像这样:

所有者

“所有者table”

“交易表”

您可以使用函数来滤波所有者基于交易日期,将partiton应用于所有者类型,然后返回最新所有者,对于每个事务行:

let
    fnLatestOwners = (MyTable as table, MyDate as date) =>
    let
        #"Filtered Date" = Table.SelectRows(MyTable, each [Owner Change Date] <= MyDate),
        #"Partitioned Owners" = Table.Group(#"Filtered Date", {"Primary / Secondary Owner"}, {{"Partition", each Table.FirstN(Table.Sort(_,{{"Owner Change Date", Order.Descending}}),1), type table}}),
        #"Combined Partitions" = Table.Combine(#"Partitioned Owners"[Partition]),
        #"Removed Columns" = Table.RemoveColumns(#"Combined Partitions",{"Owner Change Date"})
    in
        #"Removed Columns",

    Source = Transactions,
    #"Merged Queries" = Table.NestedJoin(Source,{"Account"},Owners,{"Account"},"Owners",JoinKind.LeftOuter),
    #"Added Latest Owners" = Table.AddColumn(#"Merged Queries", "Latest Owners", each fnLatestOwners([Owners],[Trans Date]), type table),
    #"Expanded Latest Owners" = Table.ExpandTableColumn(#"Added Latest Owners", "Latest Owners", {"Primary / Secondary Owner", "Owner Name"}, {"Primary / Secondary Owner", "Owner Name"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Latest Owners",{"Owners"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Removed Columns",{{"Primary / Secondary Owner", type text}, {"Owner Name", type text}})
in
    #"Changed Type"

返回:

“请求输出”

Assuming you start with 2 tables, which look something like this:

Owners:

Owners table

Transactions:

Transactions table

You can use a function to filter owners based on transaction date, apply a partiton to owner type, and return the latest owner, for each transaction row:

let
    fnLatestOwners = (MyTable as table, MyDate as date) =>
    let
        #"Filtered Date" = Table.SelectRows(MyTable, each [Owner Change Date] <= MyDate),
        #"Partitioned Owners" = Table.Group(#"Filtered Date", {"Primary / Secondary Owner"}, {{"Partition", each Table.FirstN(Table.Sort(_,{{"Owner Change Date", Order.Descending}}),1), type table}}),
        #"Combined Partitions" = Table.Combine(#"Partitioned Owners"[Partition]),
        #"Removed Columns" = Table.RemoveColumns(#"Combined Partitions",{"Owner Change Date"})
    in
        #"Removed Columns",

    Source = Transactions,
    #"Merged Queries" = Table.NestedJoin(Source,{"Account"},Owners,{"Account"},"Owners",JoinKind.LeftOuter),
    #"Added Latest Owners" = Table.AddColumn(#"Merged Queries", "Latest Owners", each fnLatestOwners([Owners],[Trans Date]), type table),
    #"Expanded Latest Owners" = Table.ExpandTableColumn(#"Added Latest Owners", "Latest Owners", {"Primary / Secondary Owner", "Owner Name"}, {"Primary / Secondary Owner", "Owner Name"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Latest Owners",{"Owners"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Removed Columns",{{"Primary / Secondary Owner", type text}, {"Owner Name", type text}})
in
    #"Changed Type"

This returns:

requested output

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文