Excel Power Query 如何添加新列确定最近和第二个最近日期(当前与之前)

标签 excel powerquery

我有一个数据集,其中包含带有某些字段的报告日期。我想使用 excel power query 添加一列(例如状态),确定最近的日期(例如 2022 年 3 月 1 日)并分配“当前”,以及第二个最近的日期(例如 2022 年 2 月 1 日) )并分配“优先”。任何帮助将不胜感激。谢谢!

<表类=“s-表”> <标题> 报告日期 公司 数量 状态 <正文> 2022 年 1 月 1 日 苹果 7 2022 年 1 月 1 日 惠普 4 2022 年 2 月 1 日 苹果 8 先前 2022 年 2 月 1 日 惠普 9 先前 2022 年 3 月 1 日 苹果 10 当前 2022 年 3 月 1 日 惠普 10 当前

最佳答案

给你。

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ31DcyMDJSUNJRciwoyEkFMcwVlGJ1UOU8AkCkCUTCCJsmC0w5iCZLiIQxNk2GBpiSEF1gmVgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Report Date" = _t, Company = _t, Number = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Report Date", type date}, {"Company", type text}, {"Number", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Company"}, {{"All", each _, type table [Report Date=nullable date, Company=nullable text, Number=nullable number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each 
        let a = Table.Sort([All], {{"Report Date", Order.Descending}}),
        b = Table.AddIndexColumn(a, "index"),
        c = Table.AddColumn(b, "Status", each if [index] = 0 then "Current" else if [index] = 1 then "Prior" else null )
        in c
        ),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Company", "All"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"Report Date", "Company", "Number", "index", "Status"}, {"Report Date", "Company", "Number", "index", "Status"}),
    #"Removed Columns1" = Table.RemoveColumns(#"Expanded Custom",{"index"})
in
    #"Removed Columns1"

enter image description here

关于Excel Power Query 如何添加新列确定最近和第二个最近日期(当前与之前),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74211579/

相关文章:

excel - 需要用户输入来指示代码

excel - 在 Excel Power Query 中添加唯一索引

excel - Power Query 忽略缺失列

excel - Power Query 中的多个 API 查询

java - 如何为 JFileChooser 添加文件过滤器

vba - Excel VBA错误: The object invoked has disconnected from its clients

excel - 如何自动排序用户表单条目

mysql - 下载 MySQL 表作为电子表格

list - 强力查询列表转表

excel - "Lookup"Excel 中 PowerPivot 模型-FactTables 中的数据