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

Power Query实现Vlookup等效逻辑异常,结果不符预期求助

解决Power Query中模拟Vlookup时结果异常的问题

问题根源

你当前的代码中,Table.SelectRows的each上下文里,[Date]和[CostCenter]默认引用的是OLMPDirekta表的字段,导致条件变成判断OLMPDirekta的Date等于自身Date、CostCenter等于自身CostCenter,这会选中OLMPDirekta的所有行,最终取第一行的DiffNyOLMPDirekta值,所以结果全为2。

修复方案1:明确引用当前行值

修改自定义列代码,先将OLRR表当前行的Date和CostCenter保存为变量,再用这些变量匹配OLMPDirekta的对应行:

if [CostType] = "Absorption (DL)" then
    let
        currentDate = [Date],
        currentCostCenter = [CostCenter],
        matchingRow = Table.SelectRows(OLMPDirekta, each [Date] = currentDate and [CostCenter] = currentCostCenter),
        result = if Table.RowCount(matchingRow) > 0 then matchingRow{0}[DiffNyOLMPDirekta] else null
    in
        result
else
    null

修复方案2:字典式优化(适合大数据量)

如果两张表数据量较大,重复用Table.SelectRows会影响性能,可先将OLMPDirekta转换为字典结构再查找:

  1. 先处理OLMPDirekta表,添加匹配键并生成字典:
let
    Source = OLMPDirekta,
    AddMatchKey = Table.AddColumn(Source, "MatchKey", each Text.From([Date]) & "|" & Text.From([CostCenter])),
    CreateLookupDict = Record.FromList(AddMatchKey[DiffNyOLMPDirekta], AddMatchKey[MatchKey])
in
    CreateLookupDict

将这个查询命名为OLMPLookupDict。

  1. 在OLRR表的自定义列中使用字典查找:
if [CostType] = "Absorption (DL)" then
    let
        currentKey = Text.From([Date]) & "|" & Text.From([CostCenter])
    in
        try OLMPLookupDict[currentKey] otherwise null
else
    null

说明

两种方案都能实现按CostCenter和Date精准匹配取值,且无需合并表,符合你后续按不同CostType关联其他表的需求。

内容的提问来源于stack exchange,提问作者A L

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 23:47:27