M中的开放loggingtypes(Power Query Formula Language)的用途和用法
我已经阅读了Power Query的M语言的语言规范,并且遇到了开放的loggingtypes,我的理解是开放types允许其他字段,但是我没有对这个意思的具体理解。
声明正常(封闭)logging的方法很简单
myRecord = [name = "MyName", Age = 30]
从语言规范(5.4:loggingtypes):
myRecordType1 = type [Name = text, Age = number] // Closed Record _type_ myRecordType2 = type [Name = text, Age = number, ...] // Open Record _type_
然而,
myRecord = [Name = "MyName", Age = 30, ...] // Not valid code
所以看来这个概念只是关于自定义loggingtypes ,而不是一般的logging,但是我不知道该怎么做。 我试过这个:
testFunc = (inputArg as myRecordType2) => 1 // Not valid code
…期待它可能使该函数只接受名称和年龄字段的logging,可选的其他字段,但没有。 认为它可能不适用于as
关键字,但即使这不起作用:
testTable = Table.AddColumn(Table.FromRecords({[A=1]}), "newcol", each [Name="MyName", Age=30], type myRecordType1) // Not valid code
有人可以说明一个使用(案例)吗? 我错过了语言规范的东西吗?
我的解释如下。 任何意见是赞赏(甚至其他的想法)。
types是值的分类。 有两种风格:基本types(数字,文本等)和自定义types,例如特定的表types或loggingtypes。
例如,表types是一组列名,types和任何键值。 表格types可以先指定,然后在创build表格时使用:
Tabeltype = type table[Key = number, Value = text], TabletypeWithKey = Type.AddTableKey(Tabeltype,{"Key"},true), TableWithKey = #table(TabletypeWithKey, {{1, "A"},{2, "B"}, {3, "C"}})
同样,您可以创buildloggingtypes。
但是,创buildlogging时不能直接使用loggingtypes。 您可以使用Value.ReplaceType将types“归于”某个值,例如loggingtypes为logging,只要loggingtypes是closures的并且没有可选字段。 下面的代码示例。
我希望有一种可能性来validation一个值是否与特定types相匹配,但只能用原始types(使用关键字“is”或“as”或“Type.Is”)来完成。
所以我创build了下面的代码来检查logging是否符合loggingtypes,根据我的解释:我不能给出任何保证它是完全的certificate。 目前它是一个查询,所以你可以看到发生了什么,但是你可以很容易地把它变成一个函数,并在目前被注释掉的代码的下半部分中使用例子。
// 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 */
马塞尔的回答非常好,但我可以添加更多的上下文。
确实,今天“M”中的开放loggingtypes没有太多的用处。
一个可能有用的地方是,如果我们有“粗糙”的表的概念,例如这个CSV在不同的行中有两个,三个和四个数据字段。
A,B,C 1,2 1,2,3 1,2,3,4
将此CSV加载到PQ编辑器/ Excel / PowerBI Desktop / powerbi.com中可能会有效,但它不适合表值。 在今天的“M”devise中,表格基本上是一个包含非可选字段的已closureslogging的列表(所以不能有比表格列更多或更less字段的表格行)。
一些其他数据源,如Azure Table或OData也可能使用不整齐的表。 现在,我们将返回一个包含一些固定列的表和一个logging列[Content]
或[Open Types]
。