Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
msgeorgie
Regular Visitor

Need some help using the KnowBe4 rest API and PowerBI

I am attempting to pull KnowBe4 data into PowerBI and unfortunatley the data is paginated and the only variables available are page and per_page.  There is no way to tell how many total records are in the dataset.  

I can pull each page back individually back using the following code but wanted to see if anyone coudl help me write some code which would bring back all of the records in one query.

 

The code below pulls back the first 500 records.  If i replace page = 1 with page = 2 it will bring back reocords 501-1000.  Please advise as to write code which will bring back all of the records efficiently.

 

let
Source = Json.Document(Web.Contents("https://us.api.knowbe4.com/v1/users?per_page=500&page=1", [Headers=[Authorization="TOKEN"]])),
Custom1 = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),

in
#"Custom1"

7 REPLIES 7
Samson217
New Member

This isn't a perfect solution, but I got this working. 

let
    //This will return up to 5000 users. Change the 2nd arguement in List.Numbers to query more or less users.
    Source = List.Numbers(1,10,1),
    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Changed Type" = Table.TransformColumnTypes(#"Converted to Table",{{"Column1", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each 
        Json.Document(
            Web.Contents(
                "https://us.api.knowbe4.com/v1",
                [
                    RelativePath= "users?page="& [Column1] & "per_page=500",
                    Headers=[Authorization="Bearer INSERT_YOUR_TOKEN_HERE"]
                ]
            )
        )
    ),
    #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom"),
    #"Expanded Custom1" = Table.ExpandRecordColumn(#"Expanded Custom", "Custom", {"id", "employee_number", "first_name", "last_name", "job_title", "email", "phish_prone_percentage", "phone_number", "extension", "mobile_phone_number", "location", "division", "manager_name", "manager_email", "provisioning_managed", "provisioning_guid", "groups", "current_risk_score", "aliases", "joined_on", "last_sign_in", "status", "organization", "department", "language", "comment", "employee_start_date", "archived_at", "custom_field_1", "custom_field_2", "custom_field_3", "custom_field_4", "custom_date_1", "custom_date_2"}, {"id", "employee_number", "first_name", "last_name", "job_title", "email", "phish_prone_percentage", "phone_number", "extension", "mobile_phone_number", "location", "division", "manager_name", "manager_email", "provisioning_managed", "provisioning_guid", "groups", "current_risk_score", "aliases", "joined_on", "last_sign_in", "status", "organization", "department", "language", "comment", "employee_start_date", "archived_at", "custom_field_1", "custom_field_2", "custom_field_3", "custom_field_4", "custom_date_1", "custom_date_2"}),
    #"Sorted Rows" = Table.Sort(#"Expanded Custom1",{{"Column1", Order.Descending}}),
    #"Filtered Rows" = Table.SelectRows(#"Sorted Rows", each [id] <> null and [id] <> ""),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Column1"})
in
    #"Removed Columns"

 

@Samson217 

I've been struggeling with the same and found that there is a syntax error in the code above.

After fixing that the paginating works as described.

 

You should add an '&' in this line:

RelativePath= "users?page="& [Column1] & "per_page=500"

which will result in this:

RelativePath= "users?page="& [Column1] & "&per_page=500"

 

That will fix the querie and result in max 10 pages of 500 results per page.

Because of the missing '&' it did default to 10 pages of 100 results per page.

 

Hope this helps you (and others in the future).

 

For ease, below is the complete fixed code:

let
    //This will return up to 5000 users. Change the 2nd arguement in List.Numbers to query more or less users.
    Source = List.Numbers(1,10,1),
    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Changed Type" = Table.TransformColumnTypes(#"Converted to Table",{{"Column1", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each 
        Json.Document(
            Web.Contents(
                "https://us.api.knowbe4.com/v1",
                [
                    RelativePath= "users?page="& [Column1] & "&per_page=500",
                    Headers=[Authorization="Bearer INSERT_YOUR_TOKEN_HERE"]
                ]
            )
        )
    ),
    #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom"),
    #"Expanded Custom1" = Table.ExpandRecordColumn(#"Expanded Custom", "Custom", {"id", "employee_number", "first_name", "last_name", "job_title", "email", "phish_prone_percentage", "phone_number", "extension", "mobile_phone_number", "location", "division", "manager_name", "manager_email", "provisioning_managed", "provisioning_guid", "groups", "current_risk_score", "aliases", "joined_on", "last_sign_in", "status", "organization", "department", "language", "comment", "employee_start_date", "archived_at", "custom_field_1", "custom_field_2", "custom_field_3", "custom_field_4", "custom_date_1", "custom_date_2"}, {"id", "employee_number", "first_name", "last_name", "job_title", "email", "phish_prone_percentage", "phone_number", "extension", "mobile_phone_number", "location", "division", "manager_name", "manager_email", "provisioning_managed", "provisioning_guid", "groups", "current_risk_score", "aliases", "joined_on", "last_sign_in", "status", "organization", "department", "language", "comment", "employee_start_date", "archived_at", "custom_field_1", "custom_field_2", "custom_field_3", "custom_field_4", "custom_date_1", "custom_date_2"}),
    #"Sorted Rows" = Table.Sort(#"Expanded Custom1",{{"Column1", Order.Descending}}),
    #"Filtered Rows" = Table.SelectRows(#"Sorted Rows", each [id] <> null and [id] <> ""),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Column1"})
in
    #"Removed Columns"

 

I used this code for my query and I was able to pull 1,000 rows where I had been only getting 500. I did an export from within the KnowBe4 app of the users and I have over 5,000 in the dataset. Does anyone know how to query the full dataset? 

mahoneypat
Microsoft Employee
Microsoft Employee

You can start with a table made from a list of numbers (for you page numbers), and then add a custom column in which you use your web call with the page number concatenated in as text.  Please see this video for an example.

Power BI - Tales From The Front - REST APIs - YouTube

 

@jpwgc 

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


jpwgc
New Member

Did you ever figure this out? Trying to do the same thing. 

jpwgc, did you ever figure this out? I have been asked to look at doing the same in terms of getting our Knowb4 data into PowerBI

Anonymous
Not applicable

@msgeorgie 
Not familar with KnowBe4, you may have a look at below articles to see if they helpful to your scenario:

Loop through Multiple Web Pages using Power Query

Iterating over multiple pages of web data using Power Query



Paul Zheng _ Community Support Team

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.