在PowerQuery / M中插入带有值列表的新列

问题描述 投票:0回答:1

如果我有以下来源:

#"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})

现在我的表将有三列。如何做到这一点?

excel powerbi powerquery m
1个回答
0
投票

我是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"

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.