Power Query 空列表未被识别为空
Power Query empty list is not recognized as empty
我目前正在开发 PowerBi 连接器。我从一家公司的 REST-web 服务中获取数据。
现在是关于分页的。
问题是,如果 pageSize 是 100 并且数据库中有 101 条记录(第一次调用我得到 100,第二次我得到 1 条记录),我无法停止调用,因为 List.Count 给我不为零空列表。
示例数据:
{"records":[{"firstname":"...","lastname":"..",}]}
代码:
json = Json.Document(Web.Contents(url, [
Content = Text.ToBinary(body)])),
records = Table.FromRecords({json}),
recordsExpaned = Table.ExpandTableColumn(records, "records", {"firstname", "lastname"}),
recordsTable = Table.ToList(recordsExpaned),
result =
if(List.Count(recordsTable) < 1) then
Data.Contacts(json) meta [NextPage = null]
else
SData.Contacts(json) meta [NextPage = page + 1]
我希望 List.Count(recordsTable) 是 0 或 null,如果记录是
{"records":[]}
但事实并非如此。
{"records":[]}
{"records":[{"firstname":"...","lastname":"..",}]}
给出相同的计数值。
这让我发疯。我如何检查列表是否真的是空的
{"records":[]}
如果我这样检查
if(List.Count(acd) < 2) then
然后它停在空列表上,但也停在只有一个参数的列表上(没错)。这对我来说意味着空列表不是真的空?!
编辑:
感谢@MarcelBeug,这是有效的
json = Json.Document(Web.Contents(url, [
Content = Text.ToBinary(body)])),
data = Table.FromRecords({json}),
recordsExpaned = Table.ExpandTableColumn(data, "records", {"firstname", "lastname"}),
recordsTable = Table.ToList(recordsExpaned),
result =
if(List.IsEmpty(json[records]) = true) then
Data.Contacts(json) meta [NextPage = null]
else
Data.Contacts(json) meta [NextPage = page + 1]
下面这行改变了游戏规则
if(List.IsEmpty(json[records]) = true) then
IsEmpty-Function 似乎正在寻找 json 中的元素 "records",尽管我从未声明过 "records"。貌似函数是解析元素来搜索的,不过我不是Power Query M的专家
您首先需要将字符串解析为 JSON 值,从而产生一条记录。
然后您可以检查该记录的 "records" 字段是否包含空列表。
示例(returns 正确):
let
Source = "{""records"":[]}",
#"Parsed JSON" = Json.Document(Source),
Custom1 = List.IsEmpty(#"Parsed JSON"[records])
in
Custom1
我目前正在开发 PowerBi 连接器。我从一家公司的 REST-web 服务中获取数据。 现在是关于分页的。 问题是,如果 pageSize 是 100 并且数据库中有 101 条记录(第一次调用我得到 100,第二次我得到 1 条记录),我无法停止调用,因为 List.Count 给我不为零空列表。
示例数据:
{"records":[{"firstname":"...","lastname":"..",}]}
代码:
json = Json.Document(Web.Contents(url, [
Content = Text.ToBinary(body)])),
records = Table.FromRecords({json}),
recordsExpaned = Table.ExpandTableColumn(records, "records", {"firstname", "lastname"}),
recordsTable = Table.ToList(recordsExpaned),
result =
if(List.Count(recordsTable) < 1) then
Data.Contacts(json) meta [NextPage = null]
else
SData.Contacts(json) meta [NextPage = page + 1]
我希望 List.Count(recordsTable) 是 0 或 null,如果记录是
{"records":[]}
但事实并非如此。
{"records":[]}
{"records":[{"firstname":"...","lastname":"..",}]}
给出相同的计数值。
这让我发疯。我如何检查列表是否真的是空的
{"records":[]}
如果我这样检查
if(List.Count(acd) < 2) then
然后它停在空列表上,但也停在只有一个参数的列表上(没错)。这对我来说意味着空列表不是真的空?!
编辑: 感谢@MarcelBeug,这是有效的
json = Json.Document(Web.Contents(url, [
Content = Text.ToBinary(body)])),
data = Table.FromRecords({json}),
recordsExpaned = Table.ExpandTableColumn(data, "records", {"firstname", "lastname"}),
recordsTable = Table.ToList(recordsExpaned),
result =
if(List.IsEmpty(json[records]) = true) then
Data.Contacts(json) meta [NextPage = null]
else
Data.Contacts(json) meta [NextPage = page + 1]
下面这行改变了游戏规则
if(List.IsEmpty(json[records]) = true) then
IsEmpty-Function 似乎正在寻找 json 中的元素 "records",尽管我从未声明过 "records"。貌似函数是解析元素来搜索的,不过我不是Power Query M的专家
您首先需要将字符串解析为 JSON 值,从而产生一条记录。 然后您可以检查该记录的 "records" 字段是否包含空列表。
示例(returns 正确):
let
Source = "{""records"":[]}",
#"Parsed JSON" = Json.Document(Source),
Custom1 = List.IsEmpty(#"Parsed JSON"[records])
in
Custom1