You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Power Query中按索引或位置引用列?代码动态修改求助

Power Query: 按位置动态引用列实现筛选

Hey there! Let's fix your column referencing issue and break down how to reference columns by index in Power Query.

Why Your Original Attempt Failed

When you tried replacing [part_desc] with Table.ColumnNames(Promoted){6}, you were using a column name string (like "part_desc") where Power Query expects a column value. Text.Contains needs the actual value from the column, not the name of the column itself—that's why it didn't work.

Correct Approach 1: Use Record.Field with Dynamic Column Name

First, grab the column name from the target position, then use Record.Field to fetch the value from the current row's record:

let
    // 获取第7列的列名(索引从0开始,6对应第7列)
    TargetColumn = Table.ColumnNames(#"Removed Columns3"){6},
    #"Filtered Part Desc" = Table.SelectRows(#"Removed Columns3", each 
        Text.Contains(Record.Field(_, TargetColumn), "ENG") 
        or Text.Contains(Record.Field(_, TargetColumn), "TRANS")
    )
in
    #"Filtered Part Desc"
  • Table.ColumnNames(#"Removed Columns3"){6} 会提取出目标位置的列名字符串
  • Record.Field(_, TargetColumn) 从当前行的记录中,取出对应列的值(_代表当前行的记录)

Correct Approach 2: Directly Reference by Index with Record.ToList

如果不需要用到列名,只想直接按位置取值,可以把行记录转成列表后直接通过索引获取:

let
    #"Filtered Part Desc" = Table.SelectRows(#"Removed Columns3", each 
        Text.Contains(Record.ToList(_){6}, "ENG") 
        or Text.Contains(Record.ToList(_){6}, "TRANS")
    )
in
    #"Filtered Part Desc"
  • Record.ToList(_) 把当前行的记录转换成值列表,顺序和表格的列顺序一致
  • {6} 从列表中取出索引为6的位置的数值

Power Query中按索引/位置引用列的常用方法

这里整理几种实用的按位置操作列的方式:

  • 获取列名: Table.ColumnNames(你的表格){索引} 返回对应位置的列名字符串(索引从0开始)
  • 获取整列数据: Table.Column(你的表格, 索引) 返回对应位置列的所有值组成的列表
  • 在行上下文(如筛选、新增列)中取单行对应位置的值:
    • 转列表后直接取索引:Record.ToList(_){索引}
    • 结合列名和Record.Field:Record.Field(_, Table.ColumnNames(你的表格){索引})

注意:Power Query的索引是从0开始计数的,第一列对应索引0,第二列对应1,以此类推,使用时要确认好目标列的位置哦!

内容的提问来源于stack exchange,提问作者Sam K

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:58:52