Tabeltype = type table[Key = number, Value = text],
TabletypeWithKey = Type.AddTableKey(Tabeltype,{"Key"},true),
TableWithKey = #table(TabletypeWithKey, {{1, "A"},{2, "B"}, {3, "C"}})
但是,创建记录时不能直接使用记录类型。
您可以使用 Value.ReplaceType 将类型“赋予”一个值,例如记录类型到记录,前提是记录类型是封闭的并且没有可选字段。
下面代码中的示例。
因此,根据我的解释,我创建了以下代码来检查记录与记录类型的一致性:我不能保证它是完整的证明。
目前它是一个查询,因此您可以查看发生了什么,但您可以轻松地将其转换为函数并在当前注释掉的代码的下半部分使用示例。
// Mext 2 lines to be decommented to turn the code into a function
//let
// fnCheckConformity = (Record1 as record, RecordType as type) as logical =>
let
// Next 2 lines to be removed when turning this code into a function
Record1 = [x = 1, A = 3, B = 4],
RecordType = type [x = number, optional y = text,...],
RecordTypeFields = Type.RecordFields(RecordType),
ToTable = Record.ToTable(RecordTypeFields),
RecordTypeTable = Table.ExpandRecordColumn(ToTable, "Value", {"Optional", "Type"}, {"Optional", "Type"}),
RecordTable = Table.FromColumns({Record.FieldNames(Record1),Record.FieldValues(Record1)},{"Record FieldName", "Record FieldValue"}),
JoinedTable = Table.Join(RecordTypeTable, "Name", RecordTable, "Record FieldName", JoinKind.FullOuter),
ConformityCheck = Table.AddColumn(JoinedTable, "Conform",
each if [Optional] = null then Type.IsOpenRecord(RecordType) else
if [Optional] then true else
if [Record FieldValue] <> null then Value.Is([Record FieldValue], [Type]) else
false),
Result = List.AllTrue(ConformityCheck[Conform])
in
Result
// Add a comma after Result when turning the code above into a function
/* The code below can be used when turning the code above into a function.
// Examples:
OpenRecordType = type [x = number, optional y = text,...],
ClosedRecordType = type [x = number, y = text],
RecordA = [x = 1],
RecordB = [x = 1, A = 3, B = 4],
RecordC = [x = 1, y = "MarcelBeug"],
// RecordC is ascribed type ClosedRecordType:
RecordCTyped = Value.ReplaceType(RecordC, ClosedRecordType),
Conformity1 = fnCheckConformity(RecordA, OpenRecordType), // true
Conformity2 = fnCheckConformity(RecordA, ClosedRecordType), // false
Conformity3 = fnCheckConformity(RecordB, OpenRecordType), // true
Conformity4 = fnCheckConformity(RecordB, ClosedRecordType), // false
Conformity5 = fnCheckConformity(RecordC, OpenRecordType) // true
in
Conformity5 */