Power Query M语言查询编辑器显示正常但表格视图无列计数值
问题:Power Query统计表格列数的查询,表格视图中丢失数值
我用M语言写了一个统计指定表格列数的查询,在查询编辑器里显示正常,但加载并关闭查询后,表格视图里看不到列计数值。
原代码
let // 统计表格列数的函数 CountColumnsFunction = (inputTable as table) as number => let ColumnCount = Table.ColumnCount(inputTable) in ColumnCount, // 通过section获取表格引用 TableReference = Record.Field(#sections[Section1], "REF Credit Limit"), ColumnCount = if TableReference <> null then CountColumnsFunction(TableReference) else null, // 构建统计结果记录 ProfileTable = [ DB_Name = "DB1", Table_Name = "REF Credit Limit", Column_Count = ColumnCount ], // 转换为指定类型的表格 TableProfiling = Table.FromRecords({ProfileTable}, type table [ DB_Name = text, Table_Name = text, Column_Count = Int64.Type ]) in TableProfiling
问题现象
- 查询编辑器中,能正常显示包含
DB_Name、Table_Name、Column_Count的表格,且Column_Count有正确数值 - 加载查询并关闭编辑器后,表格视图里
Column_Count列显示为空
原因分析
#sections[Section1]是Power Query编辑器的内部环境对象,仅在查询编辑会话中有效。当查询加载到表格视图或数据模型时,这个引用无法被解析,导致TableReference变为null,最终ColumnCount没有值,表格视图中自然看不到数据。
解决方案
最稳定的方法是直接引用目标查询,替换掉依赖#sections的代码:
修改后的完整代码
let // 统计表格列数的函数 CountColumnsFunction = (inputTable as table) as number => let ColumnCount = Table.ColumnCount(inputTable) in ColumnCount, // 直接引用目标查询(替换为你的实际查询名) TableReference = #"REF Credit Limit", ColumnCount = if TableReference <> null then CountColumnsFunction(TableReference) else null, // 构建统计结果记录 ProfileTable = [ DB_Name = "DB1", Table_Name = "REF Credit Limit", Column_Count = ColumnCount ], // 转换为指定类型的表格 TableProfiling = Table.FromRecords({ProfileTable}, type table [ DB_Name = text, Table_Name = text, Column_Count = Int64.Type ]) in TableProfiling
修改后,查询在编辑器和表格视图中都能正常显示列计数值。
内容的提问来源于stack exchange,提问作者adam Nadir
相关产品推荐
相关产品推荐

