ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 628|回复: 8

[求助] Power Query(M语言),删除重复项,如何实现保留最后一行

[复制链接]

TA的精华主题

TA的得分主题

发表于 2022-11-19 16:02 | 显示全部楼层 |阅读模式
我发现power query删除重复项,默认是保留第一行的,而我希望保留最后一行。
我尝试先降序(日期列),再删除重复项,发现排序步骤对删除重复项是没有效果的。
以下代码,虽然操作了降序,但删除重复项仍是保留day1的。



  1. let
  2.     源 = Table.FromRecords({
  3.     [day = 1, Name = "Bob", Phone = "123-4567"],
  4.     [day = 1, Name = "Jim", Phone = "987-6543"],
  5.     [day = 1, Name = "Paul", Phone = "543-7890"],
  6.     [day = 1, Name = "Bob", Phone = "123-4567"],
  7.     [day = 1, Name = "Jim", Phone = "987-6543"],
  8.     [day = 2, Name = "Paul", Phone = "543-7890"],
  9.     [day = 2, Name = "Bob", Phone = "123-4567"],
  10.     [day = 2, Name = "Jim", Phone = "987-6543"],
  11.     [day = 2, Name = "Paul", Phone = "543-7890"],
  12.     [day = 2, Name = "Bob", Phone = "123-4567"],
  13.     [day = 2, Name = "Jim", Phone = "987-6543"],
  14.     [day = 3, Name = "Paul", Phone = "543-7890"],
  15.     [day = 3, Name = "Bob", Phone = "123-4567"],
  16.     [day = 5, Name = "Jim", Phone = "987-6543"],
  17.     [day = 6, Name = "Paul", Phone = "543-7890"],
  18.     [day = 6, Name = "Bob", Phone = "123-4567"],
  19.     [day = 6, Name = "Jim", Phone = "987-6543"],
  20.     [day = 6, Name = "Paul", Phone = "543-7890"]
  21.     }),
  22.     排序的行 = Table.Sort(源,{{"day", Order.Descending}}),
  23.     删除的副本 = Table.Distinct(排序的行, {"Name"})
  24. in
  25.     删除的副本
复制代码


所以,一般这种需求如何实现?
请大佬指教,感谢。

TA的精华主题

TA的得分主题

发表于 2022-11-19 17:01 | 显示全部楼层
逆序表,删除重复项,再逆序表

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2022-11-19 17:17 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2022-11-19 20:02 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册


加个buffer可以实现需求,什么原理不了解

  1. let
  2.     源 = Table.FromRecords({
  3.     [day = 1, Name = "Bob", Phone = "123-4567"],
  4.     [day = 1, Name = "Jim", Phone = "987-6543"],
  5.     [day = 1, Name = "Paul", Phone = "543-7890"],
  6.     [day = 1, Name = "Bob", Phone = "123-4567"],
  7.     [day = 1, Name = "Jim", Phone = "987-6543"],
  8.     [day = 2, Name = "Paul", Phone = "543-7890"],
  9.     [day = 2, Name = "Bob", Phone = "123-4567"],
  10.     [day = 2, Name = "Jim", Phone = "987-6543"],
  11.     [day = 2, Name = "Paul", Phone = "543-7890"],
  12.     [day = 2, Name = "Bob", Phone = "123-4567"],
  13.     [day = 2, Name = "Jim", Phone = "987-6543"],
  14.     [day = 3, Name = "Paul", Phone = "543-7890"],
  15.     [day = 3, Name = "Bob", Phone = "123-4567"],
  16.     [day = 5, Name = "Jim", Phone = "987-6543"],
  17.     [day = 6, Name = "Paul", Phone = "543-7890"],
  18.     [day = 6, Name = "Bob", Phone = "123-4567"],
  19.     [day = 6, Name = "Jim", Phone = "987-6543"],
  20.     [day = 6, Name = "Paul", Phone = "543-7890"]
  21.     }),
  22.     排序的行 = Table.Buffer(Table.Sort(源,{{"day", Order.Descending}})),
  23.     删除的副本 = Table.Distinct(排序的行, {"Name"})
  24. in
  25.     删除的副本
复制代码


TA的精华主题

TA的得分主题

发表于 2022-11-21 09:44 | 显示全部楼层
排序之后一般加个TABLE.BUFFER,原因我也不清楚。

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2022-11-21 10:34 | 显示全部楼层
fengbao008 发表于 2022-11-21 09:44
排序之后一般加个TABLE.BUFFER,原因我也不清楚。

PQ的排序是一种虚浮的状态,不是真实的排序,计算机内部仍然是原来的顺序,加一个TABLE.BUFFER,把排序结果装入计算机缓存,固化了排序结果,也能提高运算速度,个人理解。

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2022-11-22 10:34 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
suiyuanban 发表于 2022-11-21 10:34
PQ的排序是一种虚浮的状态,不是真实的排序,计算机内部仍然是原来的顺序,加一个TABLE.BUFFER,把排序结 ...

应该是这样的。

TA的精华主题

TA的得分主题

发表于 2022-11-23 17:22 | 显示全部楼层
是不是反转行的意思。我看结果一致,供参考!
let
    源 = Table.FromRecords({
    [day = 1, Name = "Bob", Phone = "123-4567"],
    [day = 1, Name = "Jim", Phone = "987-6543"],
    [day = 1, Name = "Paul", Phone = "543-7890"],
    [day = 1, Name = "Bob", Phone = "123-4567"],
    [day = 1, Name = "Jim", Phone = "987-6543"],
    [day = 2, Name = "Paul", Phone = "543-7890"],
    [day = 2, Name = "Bob", Phone = "123-4567"],
    [day = 2, Name = "Jim", Phone = "987-6543"],
    [day = 2, Name = "Paul", Phone = "543-7890"],
    [day = 2, Name = "Bob", Phone = "123-4567"],
    [day = 2, Name = "Jim", Phone = "987-6543"],
    [day = 3, Name = "Paul", Phone = "543-7890"],
    [day = 3, Name = "Bob", Phone = "123-4567"],
    [day = 5, Name = "Jim", Phone = "987-6543"],
    [day = 6, Name = "Paul", Phone = "543-7890"],
    [day = 6, Name = "Bob", Phone = "123-4567"],
    [day = 6, Name = "Jim", Phone = "987-6543"],
    [day = 6, Name = "Paul", Phone = "543-7890"]
    }),
    逆序的行 = Table.ReverseRows(源),
    删除的副本 = Table.Distinct(逆序的行, {"Name", "Phone"})
in
    删除的副本

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2022-11-23 17:24 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
是不是反转行的意思,我看结果一致,供参考!

let
    源 = Table.FromRecords({
    [day = 1, Name = "Bob", Phone = "123-4567"],
    [day = 1, Name = "Jim", Phone = "987-6543"],
    [day = 1, Name = "Paul", Phone = "543-7890"],
    [day = 1, Name = "Bob", Phone = "123-4567"],
    [day = 1, Name = "Jim", Phone = "987-6543"],
    [day = 2, Name = "Paul", Phone = "543-7890"],
    [day = 2, Name = "Bob", Phone = "123-4567"],
    [day = 2, Name = "Jim", Phone = "987-6543"],
    [day = 2, Name = "Paul", Phone = "543-7890"],
    [day = 2, Name = "Bob", Phone = "123-4567"],
    [day = 2, Name = "Jim", Phone = "987-6543"],
    [day = 3, Name = "Paul", Phone = "543-7890"],
    [day = 3, Name = "Bob", Phone = "123-4567"],
    [day = 5, Name = "Jim", Phone = "987-6543"],
    [day = 6, Name = "Paul", Phone = "543-7890"],
    [day = 6, Name = "Bob", Phone = "123-4567"],
    [day = 6, Name = "Jim", Phone = "987-6543"],
    [day = 6, Name = "Paul", Phone = "543-7890"]
    }),
    逆序的行 = Table.ReverseRows(源),
    删除的副本 = Table.Distinct(逆序的行, {"Name", "Phone"})
in
    删除的副本

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-16 15:21 , Processed in 0.047074 second(s), 17 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表