Background
The organization that I am working for has multiple site collections and within each Site Collection, there are multiple sites. Even some site collections have more than 150 subsites and growing.
There was a requirement about how we could check the usage, based on the number of documents uploaded and edited each month. One way of checking in is to use Site Collection Audit report but that is a very time-consuming process.
I chose to use SharePoint Search API to generate reports using Power BI. My requirement was to get usage. I used M-Query to get the desired results in a Data Table.
Below is the code,
- let
-
-
-
-
- urlRequestString = "<SiteCollectionURL>/_api/search/query?querytext='(IsDocument:True AND Path:<SiteCollectionURL>*)'&startrow=0&trimduplicates=false&rowlimit=500&selectproperties='Author,Title,Path,Created,Modified,ModifiedBy,SiteTitle,ContentType,SPWebUrl'",
-
-
-
-
- Source = Json.Document(Web.Contents(urlRequestString,
- [
- Headers = [#"Accept" = "application/json"]
- ])),
-
- PrimaryQueryResult = Source[PrimaryQueryResult],
- RelevantResults = PrimaryQueryResult[RelevantResults][TotalRows],
-
- Offset = List.Generate(() => 0, each _ <= Number.RoundTowardZero(RelevantResults / 100) * 100, each _ + 500),
-
- OffsetToTable = Table.FromList(Offset, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
- Terms = Table.RenameColumns(OffsetToTable, {
- {
- "Column1",
- "Term"
- }
- }),
- #"Removed Top Rows" = Table.Skip(Terms, 0),
- Termset = Table.TransformColumnTypes(#"Removed Top Rows", {
- {
- "Term",
- type text
- }
- }),
-
-
-
-
- SearchSuccessful = (Term) =>
- let Source = Json.Document(Web.Contents(urlRequestString,
- [
- Query = [#"startrow" = Term],
- Headers = [#"Accept" = "application/json"]
- ])),
- Rows = Source[PrimaryQueryResult][RelevantResults][Table][Rows],
- AllRows = List.Transform(Rows, each _[Cells]),
- RowsToTables = List.Transform(AllRows, each List.Transform(_, each Record.ToTable(_))),
- SkelToList = List.Transform(RowsToTables, each Table.FromList(_, Splitter.SplitByNothing(), null, null, ExtraValues.Error)),
- CleanRows = List.Transform(SkelToList, each List.Transform(_[Column1], each Table.PromoteHeaders(Table.RemoveLastN(Table.RemoveColumns(_, {
- "Name"
- }), 1)))),
- TransposeTable = Table.FromRows(List.Transform(CleanRows, each List.Transform(_, each Record.FieldValues(_ {
- 0
- }) {
- 0
- }))),
- ColumnRenames = List.Transform(CleanRows {
- 0
- }, each {
- "Column" & Text.From(List.PositionOf(CleanRows {
- 0
- }, _) + 1), Table.ColumnNames(_) {
- 0
- }
- }),
- RenamedTable = Table.RenameColumns(TransposeTable, ColumnRenames) in RenamedTable,
-
- Output = Table.AddColumn(Termset, "c", each SearchSuccessful([Term])),
-
-
- #"SPResultsTable" = Table.ExpandTableColumn(Output, "c", {
- "Author",
- "Title",
- "Path",
- "Created",
- "Modified",
- "ModifiedBy",
- "SiteTitle",
- "ContentType",
- "SPWebUrl"
- }, {
- "Author",
- "Title",
- "Path",
- "Created",
- "Modified",
- "ModifiedBy",
- "SiteTitle",
- "ContentType",
- "SPWebUrl"
- }),
- #"Changed Type of Created" = Table.TransformColumnTypes(#"SPResultsTable", {
- {
- "Created",
- type datetime
- },
- {
- "Modified",
- type datetime
- }
- }),
- AddColumnCreatedYearandMonth = Table.AddColumn(#"Changed Type of Created", "MonthandYear", each Date.ToText(DateTime.Date([Created]), "yyyy-MM-MMMM")) in AddColumnCreatedYearandMonth
In this article, we are going to learn how to get data through Rest Service, get data in Data table, how to use Graph to show data, and how to use slicers to filter data.
How to get the data through Rest Service
Once code is pasted in the advanced editor, click apply.
How to get the data in DataTable
Change the query name relevant to your data, once you get the data in your data table then you can create a report out of it.
You can also use M-query to transform your data.
How to get the data in Bar Graph
Follow the steps shown below to set up a graph for your data.
How to add Slicers for filters
Add slicers to your report to filter data, you can choose multiple slicers. In this example, I added two slicers, which are Created date and Site Title.
Entire Report with Slicers
Below is the entire report with slicers.
We can also create slicers for Modified Date or any other column, say for example - Content-type.
Note
We can publish the Power BI report to Power BI Online, but it will not refresh automatically, it needs to be refreshed manually.