问:根据其他 table 转换列?
Q: Transform columns based on other table?
我的用例是导入 CSV 数据 table。导入的 table 默认情况下所有列的类型为 'text'。然后根据单独的变换table.
对每一列进行变换
CSVTable
'Key' 'Name' 'Creation Date'
-------------------------------------------
'1' 'Bob' '21/Jan/20 12:00 AM'
转换表
'Field' 'Directive'
-------------------------------------
'Key' 'int64'
'Name' 'text'
'Creation Date' 'date'
我的 objective 是根据 TransformationTable.
中的指令转换 CSVTable
注意:'Name' 字段在 CSV 中已经是文本格式,因此无需转换。
输出表
目前我已经在下面的代码行中对转换进行了硬编码。这个片段
OutputTable = Table.TransformColumnTypes(CSVTable,{{"Key", Int64.Type}, {"Creation Date", type datetime}}),
问题
我想根据 TransformationTable 中的定义而不是硬编码来控制转换。我该如何实现?
基于此设置:
列类型table名称:TableTypes
值table名称:Table1
源代码:
Table: TableTypes
let
Source= Excel.CurrentWorkbook(){[Name="TableTypes"]}[Content],
ChangeColTypes = Table.TransformColumnTypes(Source,{{"Column header", type text}, {"Type", type text}}),
ToType = Table.TransformColumns(ChangeColTypes,{{"Type", Expression.Evaluate}}),
ToField = Table.AddColumn(ToType, "Custom", each Record.FieldValues(_)),
RemoveOtherCols = Table.SelectColumns(ToField,{"Custom"}),
ToList = RemoveOtherCols[Custom]
in
ToList
Table: Table1
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
ChangeTypes = Table.TransformColumnTypes(Source,TableTypes)
in
ChangeTypes
输出:
如果你真的需要非基本类型(比如Int64.Type)使用:
= Table.TransformColumns(types, {{"col Type", each Expression.Evaluate(_, [Currency.Type=Currency.Type, Int64.Type=Int64.Type, Percentage.Type=Percentage.Type]) }})
首先我会说我认为这是不可能的,因为那里有 Int64 类型。我相信您只能以编程方式更改为这些原始类型
type null, which classifies the null value
type logical, which classifies the values true and false
type number, which classifies number values
type time, which classifies time values
type date, which classifies date values
type datetime, which classifies datetime values
type datetimezone, which classifies datetimezone values
type duration, which classifies duration values
type text, which classifies text values
type binary, which classifies binary values
type type, which classifies type values.
type list, which classifies list values
type record, which classifies record values
type table, which classifies table values
type function, which classifies function values
type anynonnull, which classifies all values excluding null
就是说,假设您有 TransformationTable 和 CSVTable,此代码将 (a) 列出 CSVTable 中的列 (b) 合并转换 table (c) 展开合并 (d) 仅提取类型列 (e) 添加前缀 (f) 评估表达式结果以使结果可用 (g) 使用 List.Zip 创建一组成对的列和类型列表 (h) 基于 List.Zip 重命名
let Source = CSVTable,
//List of column names
Columns=Table.ColumnNames(Source),
// Merge in Transformation table to get types for each column
#"Merged Queries" = Table.NestedJoin(Table.FromList(Columns),{"Column1"},TransformationTable,{"Field"},"TransformationTable",JoinKind.LeftOuter),
#"Expanded TransformationTable" = Table.ExpandTableColumn(#"Merged Queries", "TransformationTable", {"Directive"}, {"Directive"}),
// Pull just the column with types in it
Directives=Table.Column(#"Expanded TransformationTable","Directive"),
// Add a prefix and evaluate the expression to make it usable
Directives2 = List.Transform(Directives, each Expression.Evaluate("type " & _)),
// Convert the column names and types to a paired list
TransformList = List.Zip({Columns, Directives2}),
// Convert the column names to the types
TypeTable= Table.TransformColumnTypes(Source, TransformList)
in TypeTable
我的用例是导入 CSV 数据 table。导入的 table 默认情况下所有列的类型为 'text'。然后根据单独的变换table.
对每一列进行变换CSVTable
'Key' 'Name' 'Creation Date'
-------------------------------------------
'1' 'Bob' '21/Jan/20 12:00 AM'
转换表
'Field' 'Directive'
-------------------------------------
'Key' 'int64'
'Name' 'text'
'Creation Date' 'date'
我的 objective 是根据 TransformationTable.
中的指令转换 CSVTable
注意:'Name' 字段在 CSV 中已经是文本格式,因此无需转换。
输出表
目前我已经在下面的代码行中对转换进行了硬编码。这个片段
OutputTable = Table.TransformColumnTypes(CSVTable,{{"Key", Int64.Type}, {"Creation Date", type datetime}}),
问题
我想根据 TransformationTable 中的定义而不是硬编码来控制转换。我该如何实现?
基于此设置:
列类型table名称:
TableTypes
值table名称:
Table1
源代码:
Table: TableTypes
let
Source= Excel.CurrentWorkbook(){[Name="TableTypes"]}[Content],
ChangeColTypes = Table.TransformColumnTypes(Source,{{"Column header", type text}, {"Type", type text}}),
ToType = Table.TransformColumns(ChangeColTypes,{{"Type", Expression.Evaluate}}),
ToField = Table.AddColumn(ToType, "Custom", each Record.FieldValues(_)),
RemoveOtherCols = Table.SelectColumns(ToField,{"Custom"}),
ToList = RemoveOtherCols[Custom]
in
ToList
Table: Table1
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
ChangeTypes = Table.TransformColumnTypes(Source,TableTypes)
in
ChangeTypes
输出:
如果你真的需要非基本类型(比如Int64.Type)使用:
= Table.TransformColumns(types, {{"col Type", each Expression.Evaluate(_, [Currency.Type=Currency.Type, Int64.Type=Int64.Type, Percentage.Type=Percentage.Type]) }})
首先我会说我认为这是不可能的,因为那里有 Int64 类型。我相信您只能以编程方式更改为这些原始类型
type null, which classifies the null value
type logical, which classifies the values true and false
type number, which classifies number values
type time, which classifies time values
type date, which classifies date values
type datetime, which classifies datetime values
type datetimezone, which classifies datetimezone values
type duration, which classifies duration values
type text, which classifies text values
type binary, which classifies binary values
type type, which classifies type values.
type list, which classifies list values
type record, which classifies record values
type table, which classifies table values
type function, which classifies function values
type anynonnull, which classifies all values excluding null
就是说,假设您有 TransformationTable 和 CSVTable,此代码将 (a) 列出 CSVTable 中的列 (b) 合并转换 table (c) 展开合并 (d) 仅提取类型列 (e) 添加前缀 (f) 评估表达式结果以使结果可用 (g) 使用 List.Zip 创建一组成对的列和类型列表 (h) 基于 List.Zip 重命名
let Source = CSVTable,
//List of column names
Columns=Table.ColumnNames(Source),
// Merge in Transformation table to get types for each column
#"Merged Queries" = Table.NestedJoin(Table.FromList(Columns),{"Column1"},TransformationTable,{"Field"},"TransformationTable",JoinKind.LeftOuter),
#"Expanded TransformationTable" = Table.ExpandTableColumn(#"Merged Queries", "TransformationTable", {"Directive"}, {"Directive"}),
// Pull just the column with types in it
Directives=Table.Column(#"Expanded TransformationTable","Directive"),
// Add a prefix and evaluate the expression to make it usable
Directives2 = List.Transform(Directives, each Expression.Evaluate("type " & _)),
// Convert the column names and types to a paired list
TransformList = List.Zip({Columns, Directives2}),
// Convert the column names to the types
TypeTable= Table.TransformColumnTypes(Source, TransformList)
in TypeTable