Power Query M - return 最近的列值

Power Query M - return most recent column value

O365

我正在使用 PQ 将数据集 ETL 到 Excel。

ID Status Date CurrentStatus
1 Active 1/1/2022 Terminated
1 Terminated 1/10/2022 Terminated

在 CurrentStatus 下,查找行的 ID 并查找具有该 ID 的最新日期的记录,然后 return 该记录的状态。

当前设置:

公式附加到输出 table。

当前公式=XLOOKUP([@ID]&MAXIFS([Date],[ID],[@ID]),[ID]&[Date],[Status],"")

问题:

计算需要永远

目标:

将解决方案构建到 PQ M 代码而不是公式中。

感谢任何指导,谢谢。

在 powerquery 中,您可以按 ID 分组,然后在其中按日期排序,然后取最近的结果。然后展开status

使用 table/range 中的数据将数据加载到 powerquery 中,右键单击 ID 列并按

分组

取默认密码

= Table.Group(#"Changed Type", {"ID"}, {{"Count", each Table.RowCount(_), Int64.Type}})

并将末尾替换为类似这样的内容:

= Table.Group(#"Changed Type", {"ID"}, {{"data", each _, type table },     {"MaxStatus", each Table.FirstN(Table.Sort(_,{{"Date", Order.Descending}}),1)[Status]{0}}})

然后使用新列顶部的箭头 [x] 展开状态、日期和当前状态列

完整代码:

let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Status", type text}, {"Date", type date}, {"CurrentStatus", type text}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"data", each _, type table },     {"MaxStatus", each Table.FirstN(Table.Sort(_,{{"Date", Order.Descending}}),1)[Status]{0}}}),
#"Expanded data" = Table.ExpandTableColumn(#"Grouped Rows", "data", {"Status", "Date", "CurrentStatus"}, {"Status", "Date", "CurrentStatus"})
in #"Expanded data"