Excel Power Query: Using List.MatchAny on a column value

瘦欲@ 提交于 2019-12-13 23:32:10

问题


In Excel Power Query, I have a table. Column A has single numbers. I want to mark those records where the Column A value matches a list. A cutdown version of the problem is:

let
    TableA = Table.FromColumns({{1,2,4}}, {"A"}),
    ListB = {4,5,6 },
    DPart = Table.AddColumn(TableA, "IsInB", 
            List.MatchesAny(ListB, each _ = [A]))
in
    DPart

I get an error in the DPart line

Expression.Error: We cannot apply field access to the type Number.
Details:
  Value=4
  Key=A

Apparently, the code is trying to access the [A] column of elements of the list, instead of the [A] column of TableA.

What's the correct syntax to accomplish this?


回答1:


This works:

let
    TableA = Table.FromColumns({{1,2,4}}, {"A"}),
    ListB = {4,5,6 },
    DPart = Table.AddColumn(TableA, "IsInB", 
            (x) => List.MatchesAny(ListB, each _ = x[A]))
in
    DPart

But I would prefer:

let
    TableA = Table.FromColumns({{1,2,4}}, {"A"}),
    ListB = {4,5,6 },
    DPart = Table.AddColumn(TableA, "IsInB", 
            each List.Contains(ListB, _[A]))
in
    DPart


来源:https://stackoverflow.com/questions/45623765/excel-power-query-using-list-matchany-on-a-column-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!