Generate Account User List - API, Power Query, Power BI, Dataflow

WiseMan
WiseMan โœญโœญ
edited 04/25/24 in Show & Tell

Sharing the solution so you won't have to navigate the challenge I just conquered!

Need: Generate a list of the Smartsheet users WITHOUT having to download the report from the admin center.

Solution: Power Query

Assumptions: You're familiar with Power Query, Power BI, Dataflows, and how the PI works.

Solve:

  1. Create a new Smartsheet data source connection in Power Query, select the Smartsheet.query function.
  2. Create a new blank query, go into advanced editor, use the following script:

let

ย ย // Define the maximum number of records per page

ย ย pageSize = 100,


ย ย // Fetch the initial source to determine total count and total pages

ย ย initialSource = #"Smartsheet Query"("users", "page=1", []),

ย ย totalCount = initialSource[totalCount],

ย ย totalPages = initialSource[totalPages],


ย ย // Fields to check for null values and replace with appropriate values

ย ย fieldsToCheck = {"sheetCount", "name", "email", "firstName", "lastName", "admin", "licensedSheetCreator", "groupAdmin", "resourceViewer"},


ย ย // Function to apply null-replacement across specified fields in a record

ย ย transformRecord = (record) =>

ย ย ย ย let

ย ย ย ย ย ย existingFields = List.Intersect({Record.FieldNames(record), fieldsToCheck}),

ย ย ย ย ย ย updatedFields = List.Transform(existingFields, each

ย ย ย ย ย ย ย ย if _ = "sheetCount" then

ย ย ย ย ย ย ย ย ย ย {_, if Record.Field(record, _) = null then 0 else Record.Field(record, _)}

ย ย ย ย ย ย ย ย else

ย ย ย ย ย ย ย ย ย ย {_, if Record.Field(record, _) = null then null else Record.Field(record, _)}

ย ย ย ย ย ย )

ย ย ย ย in

ย ย ย ย ย ย Record.ReorderFields(Record.Combine({record, Record.FromList(List.Transform(List.Difference(fieldsToCheck, existingFields), each if _ = "sheetCount" then 0 else null), List.Difference(fieldsToCheck, existingFields))}), fieldsToCheck),


ย ย // Define a function to fetch data for a given page and apply the transformation

ย ย fetchPage = (pageNum as number) as table =>

ย ย ย ย let

ย ย ย ย ย ย delay = Function.InvokeAfter(()=> null, #duration(0,0,0,1)), // Add a delay of 1 second

ย ย ย ย ย ย source = #"Smartsheet Query"("users", "page=" & Text.From(pageNum), []),

ย ย ย ย ย ย data = source[data],

ย ย ย ย ย ย transformedData = List.Transform(data, each transformRecord(_))

ย ย ย ย in

ย ย ย ย ย ย Table.FromRecords(transformedData), // Convert the list of records into a table


ย ย // Fetch all pages and combine into a single table

ย ย allData = List.Accumulate({1..totalPages}, Table.FromRecords({}), (state, current) => Table.Combine({state, fetchPage(current)}))

in

ย ย allData