Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
Hello friends,
I have a table with three columns:
Employee, Project, Hours
showing hours spent by employees on different projects
I would like to add a column of TotalHours, summarizing total hours spent on all the projects for each employee.
I need this to calculate the percentage for each row.
Currently I am creating a new query grouped by empoyee with Sum of Hours and then Merge this query back into the detailed query.
It works fine but maybe there is a trick to do this without creating a separate query?
Thank you
Michael
Solved! Go to Solution.
You can embed your grouping in the main query:
let Source = HoursSpentTable, Grouped = Table.Group(Source, {"Employee"}, {{"TotalHours", each List.Sum([Hours]), type number}}), Merged = Table.NestedJoin(Source,{"Employee"},Grouped,{"Employee"},"NewColumn",JoinKind.LeftOuter), Expanded = Table.ExpandTableColumn(Merged, "NewColumn", {"TotalHours"}, {"TotalHours"}) in Expanded
Alternative:
let Source = HoursSpentTable, #"Added Custom" = Table.AddColumn(Source, "TotalHours", (This) => List.Sum(Table.SelectRows(Source,each [Employee] = This[Employee])[Hours])) in #"Added Custom"
The alternative looks nicer, but it might well be that the first solution is faster, which would only be relevant for a rather large dataset.
@Anonymous wrote:
Hello friends,
I have a table with three columns:
Employee, Project, Hours
showing hours spent by employees on different projects
I would like to add a column of TotalHours, summarizing total hours spent on all the projects for each employee.
I need this to calculate the percentage for each row.
Currently I am creating a new query grouped by empoyee with Sum of Hours and then Merge this query back into the detailed query.
It works fine but maybe there is a trick to do this without creating a separate query?
Thank you
Michael
@Anonymous
You may not have to calculate the precentage in advance. Try to use a Matrix visual as below.
You can embed your grouping in the main query:
let Source = HoursSpentTable, Grouped = Table.Group(Source, {"Employee"}, {{"TotalHours", each List.Sum([Hours]), type number}}), Merged = Table.NestedJoin(Source,{"Employee"},Grouped,{"Employee"},"NewColumn",JoinKind.LeftOuter), Expanded = Table.ExpandTableColumn(Merged, "NewColumn", {"TotalHours"}, {"TotalHours"}) in Expanded
Alternative:
let Source = HoursSpentTable, #"Added Custom" = Table.AddColumn(Source, "TotalHours", (This) => List.Sum(Table.SelectRows(Source,each [Employee] = This[Employee])[Hours])) in #"Added Custom"
The alternative looks nicer, but it might well be that the first solution is faster, which would only be relevant for a rather large dataset.
Hi @MarcelBeug
Would you care to elaborate how this embedding into main query works to a Power Query novice ?
I have an existing DataLoad stage and then referenced that stage as the source in a modelling query, where I basically want to perform the same exercise as the original problem in this post?
From testing I can't seem to figure out how to apply the "embed" the code into the existing query.
If you share the (relevant part of the) code from your efforts so far, I can probably point you to the required adjustments.
Awesome, thanks @MarcelBeug!
I thought you can only Merge/Join different Queries, not "steps/tables" inside one Query.
I only had a "feeling" it is possible....
Would you recommend some source with more advanced tricks like this for me to learn from?
Thanks a lot!
Michael
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
109 | |
99 | |
77 | |
66 | |
54 |
User | Count |
---|---|
144 | |
104 | |
102 | |
87 | |
64 |