我在 power bi 中有这个公式来从外部 Api 获取数据:-
let
body=Text.Combine({"sAPIKey=",ApiKey}),
SourceAPILogin=Json.Document(
Web.Contents(
SmartAPI,
[
RelativePath="apilogin",
Headers=[Accept="application/json", #"Content-Type"="application/x-www-form-urlencoded",#"API-Key"=Token],
Content=Text.ToBinary(body)
]
)
),
tblGetUID = Table.FromList(SourceAPILogin, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
expGetUID = Table.ExpandRecordColumn(tblGetUID, "Column1", {"UID", "LanguageCode", "DatabaseVersion"}, {"Column1.UID", "Column1.LanguageCode", "Column1.DatabaseVersion"}),
GetUID = expGetUID{0}[Column1.UID],
Source=Json.Document(
Web.Contents(
SmartAPI,
[
RelativePath = "jobs", //RESTAPI endpoint
//Query = [#"sQueryKey" = QueryKey,#"FilterLocation"="Database"], // QueryParameters
Headers=[Accept="application/json", #"Content-Type"="application/json",#"API-Key"=Token, UID=GetUID, DeviceID=ApiKey] //Headers
]
)
),
#"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
//display all the columns from query
Result=Table.ExpandRecordColumn( #"Converted to Table","Column1", Table.ColumnNames(Table.FromRecords(List.Select(Table.Column(#"Converted to Table","Column1"), each _ <> "" and _ <> null))),Table.ColumnNames(Table.FromRecords(List.Select(Table.Column(#"Converted to Table","Column1"), each _ <> "" and _ <> null)))),
#"Changed Type" = Table.TransformColumnTypes(Result,{{"StartedDate", type datetime}})
in
#"Changed Type"
目前我只得到10,000个项目,所以我想使用偏移和限制来应用分页,所以我修改了代码如下:-
let
// Function to fetch data with pagination
Fetchjobs = (Offset as number, Limit as number) as table =>
let
body = Text.Combine({"sAPIKey=", ApiKey}),
SourceAPILogin = Json.Document(
Web.Contents(
SmartAPI,
[
RelativePath="apilogin",
Headers=[Accept="application/json", #"Content-Type"="application/x-www-form-urlencoded",#"API-Key"=Token],
Content=Text.ToBinary(body)
]
)
),
tblGetUID = Table.FromList(SourceAPILogin, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
expGetUID = Table.ExpandRecordColumn(tblGetUID, "Column1", {"UID", "LanguageCode", "DatabaseVersion"}, {"Column1.UID", "Column1.LanguageCode", "Column1.DatabaseVersion"}),
GetUID = expGetUID{0}[Column1.UID],
Source = Json.Document(
Web.Contents(
SmartAPI,
[
RelativePath = "jobs",
Query = [#"offset"=Text.From(Offset), #"limit"=Text.From(Limit)], // Adding Offset and Limit parameters
Headers=[Accept="application/json", #"Content-Type"="application/json",#"API-Key"=Token, UID=GetUID, DeviceID=ApiKey]
]
)
),
tblAll = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
tableHasRecords = Table.RowCount(tblAll) > 0,
Result = if tableHasRecords then
Table.ExpandRecordColumn(tblAll,"Column1", Table.ColumnNames(Table.FromRecords(List.Select(Table.Column(tblAll,"Column1"), each _ <> "" and _ <> null))),Table.ColumnNames(Table.FromRecords(List.Select(Table.Column(tblAll,"Column1"), each _ <> "" and _ <> null))))
else
#table(Table.ColumnNames(tblAll), {})
in
Result,
// Initial pagination parameters
Offset = 0,
Limit = 10000,
PageSize = 10000,
// Loop through pages to get all records
AllPages = List.Generate(
() => [Result = Fetchjobs(Offset, Limit), Offset = Offset + PageSize],
each Table.RowCount([Result]) > 0,
each [Result = Fetchjobs([Offset], Limit), Offset = [Offset] + PageSize],
each [Result]
),
// Combine all the pages into a single table
tbljobs = Table.Combine(AllPages),
// Change column types as needed
#"Changed Type" = Table.TransformColumnTypes(tbljobs,{{"StartedDate", type datetime}})
in
#"Changed Type"
但是应用偏移量和限制后我得到了这个错误:-
Expression.Error: The column 'StartedDate' of the table wasn't found.
Details:
StartedDate
这是我尝试连接到@的API https://developer.smarttid.dk/smartapi/jobs
此外,如果我删除列变换,如下所示:-
let
// Function to fetch data with pagination
Fetchjobs = (Offset as number, Limit as number) as table =>
let
body = Text.Combine({"sAPIKey=", ApiKey}),
SourceAPILogin = Json.Document(
Web.Contents(
SmartAPI,
[
RelativePath="apilogin",
Headers=[Accept="application/json", #"Content-Type"="application/x-www-form-urlencoded",#"API-Key"=Token],
Content=Text.ToBinary(body)
]
)
),
tblGetUID = Table.FromList(SourceAPILogin, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
expGetUID = Table.ExpandRecordColumn(tblGetUID, "Column1", {"UID", "LanguageCode", "DatabaseVersion"}, {"Column1.UID", "Column1.LanguageCode", "Column1.DatabaseVersion"}),
GetUID = expGetUID{0}[Column1.UID],
Source = Json.Document(
Web.Contents(
SmartAPI,
[
RelativePath = "jobs",
Query = [#"offset"=Text.From(Offset), #"limit"=Text.From(Limit)], // Adding Offset and Limit parameters
Headers=[Accept="application/json", #"Content-Type"="application/json",#"API-Key"=Token, UID=GetUID, DeviceID=ApiKey]
]
)
),
tblAll = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
tableHasRecords = Table.RowCount(tblAll) > 0,
Result = if tableHasRecords then
Table.ExpandRecordColumn(tblAll,"Column1", Table.ColumnNames(Table.FromRecords(List.Select(Table.Column(tblAll,"Column1"), each _ <> "" and _ <> null))),Table.ColumnNames(Table.FromRecords(List.Select(Table.Column(tblAll,"Column1"), each _ <> "" and _ <> null))))
else
#table(Table.ColumnNames(tblAll), {})
in
Result,
// Initial pagination parameters
Offset = 0,
Limit = 10000,
PageSize = 10000,
// Loop through pages to get all records
AllPages = List.Generate(
() => [Result = Fetchjobs(Offset, Limit), Offset = Offset + PageSize],
each Table.RowCount([Result]) > 0,
each [Result = Fetchjobs([Offset], Limit), Offset = [Offset] + PageSize],
each [Result]
),
// Combine all the pages into a single table
tbljobs = Table.Combine(AllPages)//,
// Change column types as needed
//#"Changed Type" = Table.TransformColumnTypes(tbljobs,{{"StartedDate", type datetime}})
tbljobs
in
tbljobs//#"Changed Type"
我不会从表中得到任何结果。
那么可能是什么问题?
谢谢
我的猜测是,服务器返回 JSON 结果,而不是明确声明 StartedDate 为 null,StartedDate 是从 JSON 对象中丢失/省略(其中为 null)。
因此,当 PowerQuery 将 JSON 转换为表并且在整个数据页中没有看到任何 StartedDate 属性时,生成的表没有 StartedDate 列!
尝试创建一个组合不同列名的表的函数,就像这样......
(Tables as list) as table =>
let
#"Column names per table" = List.Transform(Tables, Table.ColumnNames),
AllColumnNames = List.Distinct(List.Combine(#"Column names per table")),
#"Missing column names per table" = List.Transform(#"Column names per table", each List.RemoveItems(AllColumnNames, _)),
#"Tuples of table & missing column names" = List.Zip({Tables, #"Missing column names per table"}),
#"Tables with all column names" = List.Transform(
#"Tuples of table & missing column names",
each List.Accumulate(
_{1}, // Missing column names
_{0}, // Table
(tbl, cn) => Table.AddColumn(tbl, cn, each null)
)
),
Combined = Table.Combine(#"Tables with all column names")
in
Combined
如果我是对的,并且如果您将其放入名为
fTableCombineWithColumnsCheck
的单独查询中,那么您可以将 tbljobs = Table.Combine(AllPages)
替换为 tbljobs = fTableCombineWithColumnsCheck(AllPages)
,事情就会开始工作。