【问题标题】:Using Excel to GROUP BY and find date WHERE MAX使用 Excel 进行 GROUP BY 并查找日期 WHERE MAX
【发布时间】:2021-06-01 22:50:21
【问题描述】:

我的问题是我有一个结构如下的数据表:

+---------------+------------+---------+
| recipe number |    date    | quality |
+---------------+------------+---------+
|           154 | 01/01/2020 |       2 |
|           154 | 01/03/2020 |       3 |
|           154 | 01/05/2020 |       1 |
|           154 | 01/07/2020 |       2 |
|           222 | 01/01/2020 |       3 |
|           222 | 01/03/2020 |       2 |
|           222 | 01/05/2020 |       2 |
|           222 | 01/07/2020 |       1 |
|           888 | 01/01/2020 |       1 |
|           888 | 01/03/2020 |       3 |
|           888 | 01/05/2020 |       2 |
|           888 | 01/07/2020 |       3 |
|           666 | 01/01/2020 |       2 |
|           666 | 01/03/2020 |       3 |
|           666 | 01/05/2020 |       3 |
|           666 | 01/07/2020 |       3 |
|           777 | 01/01/2020 |       1 |
|           777 | 01/03/2020 |       2 |
|           777 | 01/05/2020 |       3 |
|           777 | 01/07/2020 |       1 |
|           123 | 01/09/2020 |       3 |
|           254 | 01/01/2020 |       2 |
|           254 | 01/03/2020 |       3 |
|           745 | 01/01/2020 |       1 |
|           745 | 01/03/2020 |       3 |
|           745 | 01/05/2020 |       2 |
|           745 | 01/07/2020 |       3 |
|           578 | 01/11/2020 |       3 |
|           578 | 01/01/2021 |       3 |
|           578 | 01/03/2021 |       1 |
|           578 | 01/05/2021 |       3 |
|           678 | 01/07/2021 |       2 |
|           999 | 01/09/2021 |       1 |
|           999 | 01/11/2021 |       1 |
+---------------+------------+---------+

我想要的最终答案是,我需要一个包含每个食谱编号的表格,以及该食谱的质量是否会随着时间的推移而下降的简单是/否。

有些食谱只有一个条目,而有些食谱只会增加质量 - 这些需要回答“否”

EG:

recipe decreased?
154 yes
666 no

不幸的是,我只限于使用 Excel,尽管我知道在其他环境中这样做可能更容易。

我尝试了一个 max(index+match) 来查看是否可以为每个配方返回最高质量(以及最低质量)。但我一直在试图让 Excel 返回一系列质量,这些质量取决于要查看的食谱。

我也尝试过 PowerQuery,但该实用程序的问题似乎太复杂了。

我做了更多的思考和一些可以解决它的伪代码是:

For each recipe number:
  1) Find me the max quality and the date where it happened
  2) Find me any quality lower than this number where the date it happened is 
     after step 1
  3) If the date of step 1 is earlier (less than) the date of step 2, output 
     "yes", otherwise "no"

将其转换为 Excel 2016 有点困难

【问题讨论】:

  • over time 到底是什么意思?开始VS结束?结束时间质量低于最大时间质量?在最大时间质量之后发生最小时间质量的任何下降?对于154,它从2 开始,到2 结束,中间向上和向下。什么版本的Excel?使用 Office 365,您可能可以使用公式来做到这一点。如果没有,使用 Power Query 会相当简单。
  • 好吧,每个食谱在不同的日期进行评估,如果一个食谱在某个日期是 2 质量,那么在随后的日期是 3,这将是“是”。我还认为 powerquery 可能会处理它,但我对 powerquery 没有经验,我正在努力寻找比找到每个配方的最大质量更复杂的地方 - 特别是我找不到每个配方的最大日期
  • 那么我不明白 666。它是 01/01 的 2 和后续日期的 3,但您将其显示为 154 号是 01/01 的 2 和 01/03 的 3 和您将其显示为是。您需要更一致的规则。
  • 对不起,在最初的问题中,666 是“否”,因为它从 2 变为 3,并且从未退缩。与我原来的问题相比,我仍在努力解决这个问题,并且试图通过这些假装数据进行思考,让自己感到困惑。问题是寻找质量下降的食谱。忽略我评论中的愚蠢错误。
  • 1 是最高质量还是最低质量?什么版本的 Excel?

标签: excel date max


【解决方案1】:

编辑: 感谢@TomSharpe 指出了一些逻辑错误 => 不正确的结果,我不得不修改 M 代码并删除公式法。我相信下面的代码可以满足您的要求

这可以在 Power Query 中完成

  1. 按配方分组
  2. 确定最高质量的第一个日期
  3. 查看第一个日期之后的日期是否质量较低
  4. 如果是,则“是”,否则“否”

检查 cmets 并逐步通过 Applied Steps 窗口以更好地理解算法。

M 码

let

//read in the data
//change table name in next line to actual table name
    Source = Excel.CurrentWorkbook(){[Name="Table18"]}[Content],

//set data types
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"recipe number", Int64.Type}, {"date", type date}, {"quality", Int64.Type}}),

//group by recipe number
    #"Grouped Rows" = Table.Group(#"Changed Type", {"recipe number"}, {

//(t) is each subtable returned
        {"Quality Decrease", (t)=>
            let 

            //List.Max(quality) => highest quality rating
            //Filter the subtable to only show qualities of that value
            //then, with List.Min(....[date]) return the earliest date

               firstBest= List.Min(Table.SelectRows(t, each [quality] = List.Max(t[quality]))[date]),

            //Filter the subtable to only show dates > firstBest date and with quality worse than the highest quality rating

               decrQual = Table.SelectRows(t, each [date] > firstBest and [quality] < List.Max(t[quality]))
            in 

            //check if resultant table is empty
               if Table.IsEmpty(decrQual) then "no" else "yes", Text.Type 
               }
         })
in
    #"Grouped Rows"

【讨论】:

  • 很好,我需要更好地理解M代码。有趣的是,在配方 999 中,尽管两种质量相同,但它却出现了减少 - 我可以看到这是一个特殊情况,其中最小值和最大值相同,因此它比较日期和最佳质量 (1) 的第一个日期是
  • @TomSharpe 感谢您注意到这一点。这是一个相当简单的修复,我将编辑我的答案。
  • 非常感谢罗恩,这很有意义。
猜你喜欢
  • 1970-01-01
  • 2013-08-15
  • 2020-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-05
相关资源
最近更新 更多