使用 Excel 进行 GROUP BY 并查找日期 WHERE MAX
Using Excel to GROUP BY and find date WHERE MAX
我的问题是我有一个 table 结构如下的数据:
+---------------+------------+---------+
| 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 |
+---------------+------------+---------+
我想要的最终答案是,我需要每个食谱编号的 table 和一个简单的 yes/no 该食谱的质量是否会随着时间的推移而下降。
有一些食谱只有一个条目,而另一些食谱只增加了质量 - 这些需要回答“否”
EG:
食谱
减少了?
154
是
666
没有
不幸的是,我仅限于此 Excel,但我知道在其他环境中这样做可能更容易。
我已经尝试了 max(index+match) 以查看我是否可以 return 每个食谱的最高质量(以及最小的最低质量)。但我一直在试图让 Excel 到 return 一系列质量,这取决于要看的食谱。
我也尝试过 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 年有点困难
编辑: 感谢@TomSharpe 指出了一些逻辑错误 => 不正确的结果,我有修改 M 代码并删除公式方法。我相信下面的代码可以满足您的要求
这可以在 Power Query 中完成
- 按食谱分组
- 确定质量最高的第一次约会
- 查看第一个日期之后的日期是否质量较低
- 如果是,则“是”,否则“否”
检查注释并逐步执行应用步骤 window 以更好地理解算法。
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"
我的问题是我有一个 table 结构如下的数据:
+---------------+------------+---------+
| 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 |
+---------------+------------+---------+
我想要的最终答案是,我需要每个食谱编号的 table 和一个简单的 yes/no 该食谱的质量是否会随着时间的推移而下降。
有一些食谱只有一个条目,而另一些食谱只增加了质量 - 这些需要回答“否”
EG:
食谱 | 减少了? |
---|---|
154 | 是 |
666 | 没有 |
不幸的是,我仅限于此 Excel,但我知道在其他环境中这样做可能更容易。
我已经尝试了 max(index+match) 以查看我是否可以 return 每个食谱的最高质量(以及最小的最低质量)。但我一直在试图让 Excel 到 return 一系列质量,这取决于要看的食谱。
我也尝试过 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 年有点困难
编辑: 感谢@TomSharpe 指出了一些逻辑错误 => 不正确的结果,我有修改 M 代码并删除公式方法。我相信下面的代码可以满足您的要求
这可以在 Power Query 中完成
- 按食谱分组
- 确定质量最高的第一次约会
- 查看第一个日期之后的日期是否质量较低
- 如果是,则“是”,否则“否”
检查注释并逐步执行应用步骤 window 以更好地理解算法。
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"