Power BI is turning 10! Tune in for a special live episode on July 24 with behind-the-scenes stories, product evolution highlights, and a sneak peek at what’s in store for the future.
Save the dateEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
Sample code to get data from Service Now
let
// Get parameters from the query editor
BaseUrl = #"Base URL", // Referencing the Base URL parameter
Token = #"Bearer Token", // Referencing the Bearer Token parameter
PageSize = #"Page Size", // Referencing the Page Size parameter
MaxPages = #"Max Pages", // Referencing the Max Pages parameter
// Function to get data from ServiceNow with pagination
GetData = (pageNum as number) =>
let
// Build the URL dynamically for each page
Offset = (pageNum - 1) * PageSize,
Url = BaseUrl & "?sysparm_limit=" & Text.From(PageSize) & "&sysparm_offset=" & Text.From(Offset),
// Set up the headers (Authorization using Bearer Token)
Headers = [#"Authorization" = "Bearer " & Token],
// Fetch the data from the API
Response = Json.Document(Web.Contents(Url, [Headers=Headers])),
// Access the 'result' from the response (adjust based on your API response structure)
Result = Response[result], // Assuming 'result' holds the list of records
// Convert the List into a Table
ResultTable = Table.FromList(Result, Splitter.SplitByNothing(), null, null, ExtraValues.Error)
in
ResultTable,
// Retrieve data from multiple pages
GetAllData = List.Transform({1..MaxPages}, each GetData(_)),
// Combine the results into a single table
CombinedData = Table.Combine(GetAllData)
in
CombinedData
This successfully retrieves the tables, but can't refesh online:
This dataset includes a dynamic data source.
I have tried a dozen iterations, they all end up with this error.
I think I am close, any help appreciated!
Solved! Go to Solution.
Hi @Yxalitis
Sure, here you go. I've also formatted the code with Power Query Formatter.
let
// Get parameters from the query editor
BaseUrl = #"Base URL", // Referencing the Base URL parameter
Token = #"Bearer Token", // Referencing the Bearer Token parameter
PageSize = #"Page Size", // Referencing the Page Size parameter
MaxPages = #"Max Pages", // Referencing the Max Pages parameter
// Function to get data from ServiceNow with pagination
GetData = (pageNum as number) =>
let
// Build the URL dynamically for each page
Offset = (pageNum - 1) * PageSize,
// Use the Base URL for Web.Contents (hopefully resolves refresh issue)
Url = BaseUrl,
// Set up the query parameters in a record with one field per parameter
Query = [sysparm_limit = Text.From(PageSize), sysparm_offset = Text.From(Offset)],
// Set up the headers (Authorization using Bearer Token)
Headers = [#"Authorization" = "Bearer " & Token],
// Fetch the data from the API
Response = Json.Document(Web.Contents(Url, [Headers = Headers, Query = Query])),
// Access the 'result' from the response (adjust based on your API response structure)
Result = Response[result], // Assuming 'result' holds the list of records
// Convert the List into a Table
ResultTable = Table.FromList(Result, Splitter.SplitByNothing(), null, null, ExtraValues.Error)
in
ResultTable,
// Retrieve data from multiple pages
GetAllData = List.Transform({1 .. MaxPages}, each GetData(_)),
// Combine the results into a single table
CombinedData = Table.Combine(GetAllData)
in
CombinedData
Here is a rough summary of the code:
Hope this helps!
Glad to hear it 🙂
Just FYI, if those additional filters are query parameters within the URL in this format:
?param1=value1¶m2=value2¶m3=value3...
then you have the option of adding them to the Query record.
Based on the values I think I saw in your earlier post, it would be this kind of structure:
Query =
[
sysparm_limit = Text.From(PageSize),
sysparm_offset = Text.From(Offset),
sysparm_query = "opened_atRELATIVEGT@month@ago@24",
sysparm_display_value = "True",
sysparm_fields = "number%2Csys_created_on%2Csys_created_by%2Csys_updated_on%2Cresolved_at"
],
Hi @Yxalitis
The first thing that jumps out at me is that the query parameters should be moved to the options record, alongside Headers (similar to described here). This ensures that the first argument of Web.Contents doesn't change.
Here is how I would try writing the steps from Url to Response:
Url = BaseUrl,
Query =
[
sysparm_limit = Text.From(PageSize),
sysparm_offset = Text.From(Offset)
],
// Set up the headers (Authorization using Bearer Token)
Headers =
[#"Authorization" = "Bearer " & Token],
// Fetch the data from the API
Response =
Json.Document(
Web.Contents(
Url,
[Headers = Headers, Query = Query]
)
),
You could also look at providing RelativePath as another field in the record passed to Web.Contents (see the post linked above for an example), though this may not be required in this case.
Does refresh in the Power BI Service work using this type of setup?
Hi, thanks very much for your help!
OK, I updated my query to use your code, and it WORKS!!
Hi @Yxalitis
Sure, here you go. I've also formatted the code with Power Query Formatter.
let
// Get parameters from the query editor
BaseUrl = #"Base URL", // Referencing the Base URL parameter
Token = #"Bearer Token", // Referencing the Bearer Token parameter
PageSize = #"Page Size", // Referencing the Page Size parameter
MaxPages = #"Max Pages", // Referencing the Max Pages parameter
// Function to get data from ServiceNow with pagination
GetData = (pageNum as number) =>
let
// Build the URL dynamically for each page
Offset = (pageNum - 1) * PageSize,
// Use the Base URL for Web.Contents (hopefully resolves refresh issue)
Url = BaseUrl,
// Set up the query parameters in a record with one field per parameter
Query = [sysparm_limit = Text.From(PageSize), sysparm_offset = Text.From(Offset)],
// Set up the headers (Authorization using Bearer Token)
Headers = [#"Authorization" = "Bearer " & Token],
// Fetch the data from the API
Response = Json.Document(Web.Contents(Url, [Headers = Headers, Query = Query])),
// Access the 'result' from the response (adjust based on your API response structure)
Result = Response[result], // Assuming 'result' holds the list of records
// Convert the List into a Table
ResultTable = Table.FromList(Result, Splitter.SplitByNothing(), null, null, ExtraValues.Error)
in
ResultTable,
// Retrieve data from multiple pages
GetAllData = List.Transform({1 .. MaxPages}, each GetData(_)),
// Combine the results into a single table
CombinedData = Table.Combine(GetAllData)
in
CombinedData
Here is a rough summary of the code:
Hope this helps!
This works amazing, but dumps the entire table.
I added the filters to teh end of the baseURL, and that worked!
Thanks so much!
Glad to hear it 🙂
Just FYI, if those additional filters are query parameters within the URL in this format:
?param1=value1¶m2=value2¶m3=value3...
then you have the option of adding them to the Query record.
Based on the values I think I saw in your earlier post, it would be this kind of structure:
Query =
[
sysparm_limit = Text.From(PageSize),
sysparm_offset = Text.From(Offset),
sysparm_query = "opened_atRELATIVEGT@month@ago@24",
sysparm_display_value = "True",
sysparm_fields = "number%2Csys_created_on%2Csys_created_by%2Csys_updated_on%2Cresolved_at"
],
I have to say your solution is the neatest I've seen, just so clean and logical!
Check out the July 2025 Power BI update to learn about new features.