【发布时间】:2021-12-28 20:18:41
【问题描述】:
我正在尝试在电源查询中按组计算百分位数(从列值,例如:按部门的小时数、按地区的销售额等)。相同的逻辑可用于其他自定义组聚合。经过大量搜索,我找到了两种可能的方法。
方法一:
this archived article 看起来有完美的答案。我找不到其他任何东西。
那里的解决方案是以下自定义函数:
//PercentileInclusive Function
(inputSeries as list, percentile as number) =>
let
SeriesCount = List.Count(inputSeries),
PercentileRank = percentile * (SeriesCount - 1) + 1, //percentile value between 0 and 1
PercentileRankRoundedUp = Number.RoundUp(PercentileRank),
PercentileRankRoundedDown = Number.RoundDown(PercentileRank),
Percentile1 = List.Max(List.MinN(inputSeries, PercentileRankRoundedDown)),
Percentile2 = List.Max(List.MinN(inputSeries, PercentileRankRoundedUp)),
PercentileInclusive = Percentile1 + (Percentile2 - Percentile1) * (PercentileRank - PercentileRankRoundedDown)
in
PercentileInclusive
结合表格中的一个步骤进行适当的分组并使用该功能:
=Table.Group(TableName, {"Grouping Column"}, {{"New Column name", each
PercentileInclusive(TableName[Column to calculate Percentile of], percentile # between 0 and 1)}})
[编辑以纠正 Ron R. 指出的错字并删除不必要的细节]
示例输入:
| Pen Type | Units Sold |
|---|---|
| Ball-Point | 6,109 |
| Ball-Point | 3,085 |
| Ball-Point | 1,970 |
| Ball-Point | 8,190 |
| Ball-Point | 6,006 |
| Ball-Point | 2,671 |
| Ball-Point | 6,875 |
| Roller | 778 |
| Roller | 9,329 |
| Roller | 7,781 |
| Roller | 4,182 |
| Roller | 2,016 |
| Roller | 5,785 |
| Roller | 1,411 |
按笔类型分组的 25%(含 25%)百分位数的所需输出:
| Pen Type | 0.25 Inclusive Percentile (Correct) |
|---|---|
| Ball-Point | 2,878 |
| Roller | 1,714 |
注意:上面没有显示小数,使用 Excel 的 PERCENTILE.INC 函数计算。
方法 1 效果很好。
方法二: 这是我尝试过的另一种 Power Query 解决方案。这是一个没有自定义功能的单一步骤。似乎它应该可以解决问题,但我想不出一种方法来使条件检查基于行。有些东西需要去我有 //Condition// 的地方告诉它哪些行属于当前行组,但无论我尝试什么它都不起作用。它要么中断,要么给出所有内容的百分位数,忽略分组。
=List.Percentile(Table.Column(Table.SelectRows(#"Previous Step Name", //Condition//), "Column to calculate percentile of"), percentile # 0 to 1)
任何想法如何使方法 2 起作用?
【问题讨论】:
-
如果您编辑您的问题以提供您的数据(或代表性数据)示例(作为可以复制/粘贴的文本)带有 那个 数据的所需输出的屏幕截图,以及用于获得这些结果的逻辑,这将有助于帮助您。
标签: powerquery aggregation custom-function