Excel Q:基于其他表转换列?
我的用例是导入CSV数据表。默认情况下,导入的表的所有列都具有“text”类型。然后根据单独的转换表转换每一列 CSVTable 转换表 我的目标是根据TransformationTable中的指令转换CSVTable。Excel Q:基于其他表转换列?,excel,powerbi,transformation,powerquery,Excel,Powerbi,Transformation,Powerquery,我的用例是导入CSV数据表。默认情况下,导入的表的所有列都具有“text”类型。然后根据单独的转换表转换每一列 CSVTable 转换表 我的目标是根据TransformationTable中的指令转换CSVTable。 注意:“名称”字段在CSV中已为文本格式,因此无需转换 可输出 目前,我已经在下面的代码行中硬编码了转换。这个片段 OutputTable = Table.TransformColumnTypes(CSVTable,{{"Key", Int64.Type}, {"Creatio
注意:“名称”字段在CSV中已为文本格式,因此无需转换 可输出 目前,我已经在下面的代码行中硬编码了转换。这个片段
OutputTable = Table.TransformColumnTypes(CSVTable,{{"Key", Int64.Type}, {"Creation Date", type datetime}}),
问题
我希望根据TransformationTable中的定义控制转换,而不是硬编码。我怎样才能做到这一点?
基于此设置:
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
Table1
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
ChangeTypes = Table.TransformColumnTypes(Source,TableTypes)
in
ChangeTypes
源代码:
表: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
表:表1
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
ChangeTypes = Table.TransformColumnTypes(Source,TableTypes)
in
ChangeTypes
输出:
如果确实需要非基本类型(如Int64.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)合并转换表(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
谢谢你的回答。您的解决方案确实帮助我缩小了如何合并两个表的差距。非常感谢!谢谢你的回答。它提供了一些关于如何桥接这两个表以及如何转换非原语的好见解。非常感谢!
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