Table.FromRecords

语法

Table.FromRecords(
    records as list,
    optional columns as any,
    optional missingField as nullable number
) as table

关于

将指定的记录列表转换为表。

  • records:要转换为表的记录列表。

  • columns:(可选) 表的列名列表或表的类型。

  • missingField:(可选) 指定如何处理行中缺少的字段。 使用下列值之一:

    • MissingField.Error:任何缺少的字段都会产生错误(默认值)。
    • MissingField.UseNull:任何缺失的字段都会作为null 值包含在内。

    MissingField.Ignore 此参数中使用会产生错误。

示例 1

使用记录字段名称作为列名称,通过记录创建表。

使用情况

Table.FromRecords({
    [CustomerID = 1, Name = "Bob", Phone = "123-4567"],
    [CustomerID = 2, Name = "Jim", Phone = "987-6543"],
    [CustomerID = 3, Name = "Paul", Phone = "543-7890"]
})

输出

#table(type table[CustomerID = any, Name = any, Phone = any],
{
    {1, "Bob", "123-4567"},
    {2, "Jim", "987-6543"},
    {3, "Paul", "543-7890"}
})

示例 2

使用键入的列通过记录创建表并选择数字列。

使用情况

Table.ColumnsOfType(
    Table.FromRecords(
        {[CustomerID = 1, Name = "Bob"]},
        type table[CustomerID = Number.Type, Name = Text.Type]
    ),
    {type number}
)

输出

{"CustomerID"}

示例 3

创建一个表,其中包含指定记录中客户的名字、中间名和姓氏。 如果缺少任何值,请将值替换为 null

使用情况

Table.FromRecords({
        [CustomerID = 1, FirstName = "Bob", MiddleInitial = "C", LastName = "Smith"],
        [CustomerID = 2, FirstName = "Sarah", LastName = "Jones"],
        [CustomerID = 3, FirstName = "Harry", MiddleInitial = "H"]
    },
    type table [FirstName = nullable text, MiddleInitial = nullable text, LastName = nullable text],
    MissingField.UseNull)

输出

#table(type table[FirstName = text, MiddleInitial = text, LastName = text],
{
    {"Bob", "C", "Smith"},
    {"Sarah", null, "Jones"},
    {"Harry", "H", null}
})

缺少字段