let // aqui é informado a primeira parte da url do link http = "https://transwar.lannister.emitir.me/", // a segunda parte da url, é o "endpoint" da consulta que se quer fazer endpoint = "outcomes/search?created_from=2023-09-01", // Romaneios // e aqui se monta a url (o '&' concatena as partes) url = http & endpoint, //aqui o nº de registros por página RegistrosPorPagina = 40, // começo da função que acessa o link obterResultadoJson = (Url) => let opcoes = [Headers=[ #"x-api-key" = "75594207-b5d7-43bf-a7c2-571b3030308d", #"Content-Type" = "application/json" ]], dadosConteudo = Web.Contents(Url, opcoes), dadosJson = Json.Document(dadosConteudo) in dadosJson, // fim da função // começo da função que pega o total de registros obterTotalRegistros = () => let Url = url, Json = obterResultadoJson(Url), quantidade = Json[#"total"] in quantidade, // fim da função // começo da função que pega os registros de cada página obterRegistrosDaPagina = (pagina) => let page = "&page=" & Text.From(pagina), Url = url & page, Json = obterResultadoJson(Url), resultados = Json[#"data"] in resultados, // fim da função // a função List.Max pega o maior valor entre, o "total de registros" e o "nº de registros por página" totalRegistros = List.Max({ RegistrosPorPagina, obterTotalRegistros() }), // gera o nº de páginas arendondado pra mais numeroDePaginas = Number.RoundUp(totalRegistros / RegistrosPorPagina), // cria uma lista com os números das páginas que vão ser lidas indicesDasPaginas = { 1 .. numeroDePaginas }, // faz um looping lendo cada página da lista paginas = List.Transform(indicesDasPaginas, each obterRegistrosDaPagina(_)), // junta todas as páginas lidas paginasUnidas = List.Union(paginas), // transforma a lista em tabela tabela = Table.FromList(paginasUnidas, Splitter.SplitByNothing(), null, null, ExtraValues.Error), // começo da "Power Query" para definir e expandir as colunas da tabela // essa é a parte que deve ser mudada para cada tipo de consulta...(CTe, notas, romaneios, etc.) #"Column1 Expandido1" = Table.ExpandRecordColumn(tabela, "Column1", {"id", "number", "status", "type", "created_at", "created_user", "updated_user", "total_docs", "total", "owner", "vehicle", "scheduling", "scheduled", "docs", "driver", "ciot"}, {"data.id", "data.number", "data.status", "data.type", "data.created_at", "data.created_user", "data.updated_user", "data.total_docs", "data.total", "data.owner", "data.vehicle", "data.scheduling", "data.scheduled", "data.docs", "data.driver", "data.ciot"}), #"data.owner Expandido" = Table.ExpandRecordColumn(#"Column1 Expandido1", "data.owner", {"name", "id"}, {"data.owner.name", "data.owner.id"}), #"data.vehicle Expandido" = Table.ExpandRecordColumn(#"data.owner Expandido", "data.vehicle", {"traction"}, {"data.vehicle.traction"}), #"data.vehicle.traction Expandido" = Table.ExpandRecordColumn(#"data.vehicle Expandido", "data.vehicle.traction", {"id", "license_plate"}, {"data.vehicle.traction.id", "data.vehicle.traction.license_plate"}), #"data.scheduling Expandido" = Table.ExpandRecordColumn(#"data.vehicle.traction Expandido", "data.scheduling", {"route"}, {"data.scheduling.route"}), #"data.scheduling.route Expandido" = Table.ExpandRecordColumn(#"data.scheduling Expandido", "data.scheduling.route", {"start", "finish"}, {"data.scheduling.route.start", "data.scheduling.route.finish"}), #"data.scheduling.route.start Expandido" = Table.ExpandRecordColumn(#"data.scheduling.route Expandido", "data.scheduling.route.start", {"state", "city"}, {"data.scheduling.route.start.state", "data.scheduling.route.start.city"}), #"data.scheduling.route.start.state Expandido" = Table.ExpandRecordColumn(#"data.scheduling.route.start Expandido", "data.scheduling.route.start.state", {"id", "uf", "ibge"}, {"data.scheduling.route.start.state.id", "data.scheduling.route.start.state.uf", "data.scheduling.route.start.state.ibge"}), #"data.scheduling.route.start.city Expandido" = Table.ExpandRecordColumn(#"data.scheduling.route.start.state Expandido", "data.scheduling.route.start.city", {"id", "name", "ibge"}, {"data.scheduling.route.start.city.id", "data.scheduling.route.start.city.name", "data.scheduling.route.start.city.ibge"}), #"data.scheduling.route.finish Expandido" = Table.ExpandRecordColumn(#"data.scheduling.route.start.city Expandido", "data.scheduling.route.finish", {"state", "city"}, {"data.scheduling.route.finish.state", "data.scheduling.route.finish.city"}), #"data.scheduling.route.finish.state Expandido" = Table.ExpandRecordColumn(#"data.scheduling.route.finish Expandido", "data.scheduling.route.finish.state", {"id", "uf", "ibge"}, {"data.scheduling.route.finish.state.id", "data.scheduling.route.finish.state.uf", "data.scheduling.route.finish.state.ibge"}), #"data.scheduling.route.finish.city Expandido" = Table.ExpandRecordColumn(#"data.scheduling.route.finish.state Expandido", "data.scheduling.route.finish.city", {"id", "name", "ibge"}, {"data.scheduling.route.finish.city.id", "data.scheduling.route.finish.city.name", "data.scheduling.route.finish.city.ibge"}), #"data.scheduled Expandido" = Table.ExpandRecordColumn(#"data.scheduling.route.finish.city Expandido", "data.scheduled", {"start"}, {"data.scheduled.start"}), #"data.scheduled.start Expandido" = Table.ExpandRecordColumn(#"data.scheduled Expandido", "data.scheduled.start", {"date", "time"}, {"data.scheduled.start.date", "data.scheduled.start.time"}), #"data.docs Expandido" = Table.ExpandRecordColumn(#"data.scheduled.start Expandido", "data.docs", {"total", "done"}, {"data.docs.total", "data.docs.done"}), #"data.driver Expandido" = Table.ExpandRecordColumn(#"data.docs Expandido", "data.driver", {"name"}, {"data.driver.name"}), #"data.ciot Expandido" = Table.ExpandRecordColumn(#"data.driver Expandido", "data.ciot", {"id", "number"}, {"data.ciot.id", "data.ciot.number"}), #"Tipo Alterado" = Table.TransformColumnTypes(#"data.ciot Expandido",{{"data.id", Int64.Type}, {"data.number", Int64.Type}, {"data.status", Int64.Type}, {"data.type", Int64.Type}, {"data.created_at", type datetime}, {"data.created_user", type text}, {"data.updated_user", type text}, {"data.total_docs", Int64.Type}, {"data.total", Int64.Type}, {"data.owner.name", type text}, {"data.owner.id", Int64.Type}, {"data.vehicle.traction.id", Int64.Type}, {"data.vehicle.traction.license_plate", type text}, {"data.scheduling.route.start.state.id", Int64.Type}, {"data.scheduling.route.start.state.uf", type text}, {"data.scheduling.route.start.state.ibge", Int64.Type}, {"data.scheduling.route.start.city.id", Int64.Type}, {"data.scheduling.route.start.city.name", type text}, {"data.scheduling.route.start.city.ibge", Int64.Type}, {"data.scheduling.route.finish.state.id", Int64.Type}, {"data.scheduling.route.finish.state.uf", type text}, {"data.scheduling.route.finish.state.ibge", Int64.Type}, {"data.scheduling.route.finish.city.id", Int64.Type}, {"data.scheduling.route.finish.city.name", type text}, {"data.scheduling.route.finish.city.ibge", Int64.Type}, {"data.scheduled.start.date", type date}, {"data.scheduled.start.time", type time}, {"data.docs.total", Int64.Type}, {"data.docs.done", Int64.Type}, {"data.driver.name", type text}, {"data.ciot.id", type any}, {"data.ciot.number", type any}}) // final da "Power Query" in #"Tipo Alterado"