excel - 电源查询过滤每年的前 N ​​个值

标签 excel filter powerquery

我有每个国家和每年的数据集。
问题:我想用 PowerQuery 过滤每年的总金额 topN。
我只能使用此代码获得一年的结果,但我需要一个列表中包含 top10(N) 的所有年份数据。

let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Export Country", type text}, {"Gross Export", Int64.Type}, {"Share", type number}, {"Year", Int64.Type}, {"Imp/Exp", type text}}),
#"Sorted Rows" = Table.Sort(#"Changed Type",{{"Year", Order.Ascending}, {"Gross Export", Order.Descending}}),
#"Kept First Rows" = Table.FirstN(#"Sorted Rows",10) 
in #"Kept First Rows"
解决方法:我分别为每一年创建了列表,然后将它们合并,但这是一个漫长的过程。工作表“Export_Top10”中的预期结果
谢谢您的帮助。
Data File

最佳答案

这是另一种也涉及分组的方法。
但是,我没有对整个子表进行排序并添加索引列,而是对总导出列表进行排序,并仅选择总导出 >= 高于第十位的那些行。
请注意,此方法将返回 全部 事件中的行领带 .因此,如果两个国家/地区在给定年份的导出并列,您可能会返回 11 行而不是 10 行。

let
    Source = #"Table1 (3)",

//Group by year and extract top 10
    #"Grouped Rows" = Table.Group(Source, {"Year"}, {
        {"Top Ten", (t)=> Table.SelectRows(t, each [Gross Export]> List.Sort(t[Gross Export],Order.Descending){10})}}),

//remove year column since we will expand it in the correct order in next step
    #"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"Year"}),

//expand the top ten table
    #"Expanded Top Ten" = Table.ExpandTableColumn(#"Removed Columns", "Top Ten", 
        {"Export Country", "Gross Export", "Share", "Year", "Imp/Exp"}, {"Export Country", "Gross Export", "Share", "Year", "Imp/Exp"})
in
    #"Expanded Top Ten"
仅显示前 3 个 的示例结果
enter image description here

关于excel - 电源查询过滤每年的前 N ​​个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69754911/

相关文章:

api - Libav API : Filtering + Transconding

ios - 核心图像过滤器 iOS UISlider

indexing - Power Query 基于多列创建组编号

python - 使用 win32com 和 python 复制和粘贴隔离

excel - 如何在 Excel 中提取满足多行和多列条件的值?

.net - 通过.Net 丢失宏的 Excel 开发

filter - 具有数组值的 Algolia 分面过滤器

excel - M(Power Query 公式语言)中开放记录类型的目的和用法

postgresql - 使用 Npgsql 从 Power Query 访问 postgresql

excel - Countifs 匹配 OR 条件?