从 Power Query 中的单元格值中提取数据类型?
Posted
技术标签:
【中文标题】从 Power Query 中的单元格值中提取数据类型?【英文标题】:Extract Datatype from Cell Values in PowerQuery? 【发布时间】:2021-12-13 22:22:58 【问题描述】:如何将每个单元格的数据类型保存到新列中?我希望“自定义”列显示“值”列中每个项目的数据类型。我试过使用Value.Type([values])
,但输出只显示这个“类型”值。如果我点击“类型”,它会创建一个新的导航查询,我可以看到数据类型保存在其中,但我似乎无法提取它们。
【问题讨论】:
有趣的问题。 This 是一个可能的解决方案,但我不喜欢它。 谢谢!该解决方案有效。在拆分列后尝试使用此方法时,我现在面临一个新问题:***.com/questions/70342312/… 【参考方案1】:原始类型与归属类型
@AlexisOlson:
This 是一个可能的解决方案,但我不喜欢它。
你是对的,那个答案是不正确的。它使用Table.Schema
,which tests for ascribed types That's usually the right type, but if data isn't transformed correctly, it can be wrong
您需要使用Value.Type()
来获取真实/实际的primitive type
查询
let
Sample =
123, 0.45, true,
"text",
Currency.From(12.34),
Int64.From(91),
DateTime.LocalNow(), DateTimeZone.FixedLocalNow(), #date(2021,1,3),
#duration(0,0,1,3), #binary(2,3,4),
#table(type table[Foo=text], "bar"),
[ a = "b" ],
each "x",
0..3
,
records = List.Transform(
Sample,
(item) => [
Original = item,
Name = TypeToText( Value.Type( item ))
]
),
Final = Table.FromRecords( records, type table[Original = any, Name = text])
in
Final
Type.ToText.pq
这是_serialize_typename()
的简化版本,其中包含DataConnectors/UnitTesting.query.pq 的真正序列化
let
Type.ToText = (x, optional funtype as logical) =>
let
isFunctionType = (x as type) => try if Type.FunctionReturn(x) is type then true else false otherwise false,
isTableType = (x as type) => try if Type.TableSchema(x) is table then true else false otherwise false,
isRecordType = (x as type) => try if Type.ClosedRecord(x) is type then true else false otherwise false,
isListType = (x as type) => try if Type.ListItem(x) is type then true else false otherwise false
in
if funtype = null and isTableType(x) then "Table"
else if funtype = null and isListType(x) then "list"
else if funtype = null and isFunctionType(x) then "Function"
else if funtype = null and isRecordType(x) then "Record"
else if x = type any then "any"
else let base = Type.NonNullable(x) in
(if Type.IsNullable(x) then "nullable " else "") &
(if base = type anynonnull then "anynonnull" else
if base = type binary then "binary" else
if base = type date then "date" else
if base = type datetime then "datetime" else
if base = type datetimezone then "datetimezone" else
if base = type duration then "duration" else
if base = type logical then "logical" else
if base = type none then "none" else
if base = type null then "null" else
if base = type number then "number" else
if base = type text then "text" else
if base = type time then "time" else
if base = type type then "type" else
/* Abstract types: */
if base = type function then "function" else
if base = type table then "table" else
if base = type record then "record" else
if base = type list then "list"
else "any /*Actually unknown type*/")
in
Type.ToText
【讨论】:
以上是关于从 Power Query 中的单元格值中提取数据类型?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Power Query 从 SharePoint 库中的(许多)文档中获取数据
如何使用 Power Query 从 Toggl API 中提取数据?
按单元格值提取 excel 数据:python PANDAS