When grouping data have you ever wanted to keep a summary of the details? Maybe as a comma delimited list as a single column?
I've put together a sample project here that takes projects and their tasks and groups them by project and summarizes each project by summing project hours and by creating a comma delimited list of tasks.
Here's the source data. It happens to be from a SharePoint list with lookup columns for Project ID and Title. It could be from any other source such as a two table join of Projects and Tasks.
Here's the result I wanted:
If you have worked with Power BI much at all, getting the subtotals by project is probably not too much of a challenge. The comma delimited list of task name might be a bit harder.
Here's the steps, slightly generalized:
- In Power BI use Get Data to get the one table like my starting example, or two tables that you then join to get a result that looks like my example.
- Make sure you click Edit and not Load as the steps that follow occur in the Power Query Editor.
- For a SharePoint source remove all of the unneeded columns. Here's what I removed: "FileSystemObjectType", "ServerRedirectedEmbedUri", "ServerRedirectedEmbedUrl", "ContentTypeId", "ComplianceAssetId", "Project_IDId", "ID.1", "Modified", "Created", "AuthorId", "EditorId", "OData__UIVersionString", "Attachments", "GUID", "FirstUniqueAncestorSecurableObject", "RoleAssignments", "AttachmentFiles", "ContentType", "GetDlpPolicyTip", "FieldValuesAsHtml", "FieldValuesAsText", "FieldValuesForEdit", "File", "Folder", "LikedByInformation", "ParentList", "Properties", "Versions", "Author", "Editor"
- Rename any columns that did not come in the way you like.
- Here's my data at this point. As my SharePoint list used lookup columns, all of the looked up data is stored here as a "Record". (If your list does not include a "Record" column, you can skip steps 5 and 6.)
- To extract data from the Record column, click the double headed arrow, unselect all of the columns and select just the columns you want to keep. I selected Title and Project_Title. (Even if you renamed the "Title" column in your SharePoint list, it will still be imported here as "Title".)
- Rename the "Title" column to "Project_Title". (or any name of your choice!)
The result should now look like this. - If you also brought in a Task ID or Task date column, you may want to sort on it now.
- Click Group By in the ribbon.
- Group by the project columns, "Project_ID", and "Project_Title" in my example.
- Click Advanced so you can create two columns during the grouping.
- The first trick needed to get the task names out to a comma delimited list is to use the "All Rows" operation. I named this new column "TaskInfo", but any name will work as we will delete this column later.
- Click Add aggregation and add a Sum operation on the Hours column. I named this column "Total Hours".
- Click OK. Note that the TaskInfo is a column of Tables. Each table contains a list of the tasks.
- In the Add Column ribbon click Custom Column.
- Name the column "Tasks".
- Enter this formula: =Table.Column([TaskInfo], "Tasks_Title") (This is the second part of the trick...) This extracts the Task Title data from the table created by the group and then stores it as a new List column.
- Click OK. Note that the new column is a column of Lists.
- The "TaskInfo" column is no longer needed and can be deleted now.
- Now we need to extract the List data for each project into a comma delimited list.
- At the to top of the "Tasks" List column click the double headed arrow and click Extract Values...
- Select your delimiter from the drop down or create a custom delimiter. I selected Comma.
- Click OK.
- All done! Click Close & Apply and create your visuals!
Here's the M code (Advanced Editor) that was generated by this project. Note that I did not follow any best practices like naming each step! But I did add some comments.
No comments:
Post a Comment
Note to spammers...
Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.