在 PowerQuery/M 中插入包含值列表的新列

Insert new column with list of values in PowerQuery/M

如果我有以下来源:

#"My Source" = Table.FromRecords({
        [Name="Jared Smith", Age=24],
        [Name = "Tom Brady", Age=44],
        [Name="Hello Tom", Age = null],
        [Name = "asdf", Age = "abc"]
    }),

如何从值列表中添加新列,例如:

Table.AddColumn(#"My Source", "New Col", {'x', 'y', 'z', null})

现在我的 table 将有三列。这怎么可能?

我是PQ初学者,所以可能有更有效的方法,但这里有一个:

  • 为每个表添加一个索引列
  • 合并两个表,使用索引列作为键
  • 删除索引列

let
    Source1 = Table.FromRecords({
        [Name="Jared Smith", Age=24],
        [Name = "Tom Brady", Age=44],
        [Name="Hello Tom", Age = null],
        [Name = "asdf", Age = "abc"]
    }),
    #"Added Index" = Table.AddIndexColumn(Source1, "Index", 0, 1),
    Source2 = Table.FromRecords({
        [New="x"],
        [New = "y"],
        [New = "z"],
        [New = null]
    }),
    #"Added Index2" = Table.AddIndexColumn(Source2, "Index", 0, 1),
    Merge = Table.Join(#"Added Index", "Index",#"Added Index2", "Index"),
    #"Removed Columns" = Table.RemoveColumns(Merge,{"Index"})
in
    #"Removed Columns"

这是另一种方式。它与 Ron 使用的方法类似,通过添加索引开始,但随后不使用合并,而是使用索引作为对适当列表项的引用。

let
    Source1 = Table.FromRecords({
        [Name="Jared Smith", Age=24],
        [Name = "Tom Brady", Age=44],
        [Name="Hello Tom", Age = null],
        [Name = "asdf", Age = "abc"]
    }),
    #"Added Index" = Table.AddIndexColumn(Source1, "Index", 0, 1),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each {"x", "y", "z", null}{[Index]}),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"})
in
    #"Removed Columns"