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

To celebrate FabCon Vienna, we are offering 50% off select exams. Ends October 3rd. Request your discount now.

Reply
Teapotlid
Frequent Visitor

Grouping 2 columns separately for a calculation

I have data similar to the table below and I'm trying to get the Department % per Shop.

E.g. Thompson Electronics totals 300 & Thompson totals 450 so I want to get 67% (300/450) to use in another query calculation.

I'll then link the resulting % column to the other query using the combination of the columns.

 

DateShopDepartmentSales
1/1/2025ThompsonElectronics100
3/2/2025ThompsonElectronics200
2/3/2025ThompsonHomewears150
4/4/2025T&RHomewears300
4/4/2025T&RHomewears200
3/3/2025T&RElectronics120
1/2/2025T&RClothing250

 

In Query I can group by either Shop or Department but get messed up trying to get both to do the %.

I was hoping to get to something like this.

 Dept SalesShop SalesDept as % Shop Sales
Thompson Electronics30045067%
Thompson Homewears15045033%
T&R Homewears50087057%
T&R Electronics12087014%
T&R Clothing25087029%

It seems so simple but it's doing my head in!

Any help greatfully appreciated

1 ACCEPTED SOLUTION
slorin
Super User
Super User

Hi, @Teapotlid 
Another possibility

 

let
Source = Your_Source,
#"Group Shop+Dept" = Table.Group(Source, {"Shop", "Department"},
{{"Dept Sales", each List.Sum([Sales]), type number}}),
#"Group Shop" = Table.Group(#"Group Shop+Dept", {"Shop"},
{{"Shop Sales", each List.Sum([Dept Sales]), type number},
{"Data", each _, type table [Shop=text, Department=text, Dept Sales=number]}}),
Expand = Table.ExpandTableColumn(#"Group Shop", "Data", {"Department", "Dept Sales"}, {"Department", "Dept Sales"}),
#"Dept%Shop" = Table.AddColumn(Expand, "Dept as % Shop Sales", each [Dept Sales] / [Shop Sales], Percentage.Type)
in
#"Dept%Shop"

Stéphane

View solution in original post

9 REPLIES 9
v-venuppu
Community Support
Community Support

Hi @Teapotlid ,

I hope the information provided is helpful.I wanted to check whether you were able to resolve the issue with the provided solutions.Please let us know if you need any further assistance.

Thank you.

v-venuppu
Community Support
Community Support

Hi @Teapotlid ,

May I ask if you have resolved this issue? Please let us know if you have any further issues, we are happy to help.

Thank you.

v-venuppu
Community Support
Community Support

Hi @Teapotlid ,

Thank you for reaching out to Microsoft Fabric Community.

Thank you @Omid_Motamedise @slorin  for the prompt response.

I wanted to check if you had the opportunity to review the information provided and resolve the issue..?Please let us know if you need any further assistance.We are happy to help.

Thank you.

slorin
Super User
Super User

Hi, @Teapotlid 
Another possibility

 

let
Source = Your_Source,
#"Group Shop+Dept" = Table.Group(Source, {"Shop", "Department"},
{{"Dept Sales", each List.Sum([Sales]), type number}}),
#"Group Shop" = Table.Group(#"Group Shop+Dept", {"Shop"},
{{"Shop Sales", each List.Sum([Dept Sales]), type number},
{"Data", each _, type table [Shop=text, Department=text, Dept Sales=number]}}),
Expand = Table.ExpandTableColumn(#"Group Shop", "Data", {"Department", "Dept Sales"}, {"Department", "Dept Sales"}),
#"Dept%Shop" = Table.AddColumn(Expand, "Dept as % Shop Sales", each [Dept Sales] / [Shop Sales], Percentage.Type)
in
#"Dept%Shop"

Stéphane

Thanks Stéphane, this works well.

(sorry for the tardy response - life got in the way)

Thanks @Omid_Motamedise  & @slorin .

Can the "source" be an earlier row in the query? My data comes from another query which I referenced to build this query and I have some earlier steps where I remove unnecessary columns.

@Teapotlid yes it can.

Just change the source keyword to the name of last step in your query 

If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h
Omid_Motamedise
Super User
Super User

Please finds the attached file including two solutions for your case.

first is using two grouping command and merging as follow

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type datetime}, {"Shop", type text}, {"Department", type text}, {"Sales", Int64.Type}}),
    Grouped1 = Table.Group(#"Changed Type", {"Shop"}, {{"Shop Sales", each List.Sum([Sales]), type nullable number}}),
    Grouped2 = Table.Group(#"Changed Type", {"Shop","Department"}, {{"Dept Sales	", each List.Sum([Sales]), type nullable number}}),
    #"Merged Queries" = Table.NestedJoin(Grouped2, {"Shop"}, Grouped1, {"Shop"}, "Grouped2", JoinKind.LeftOuter),
    #"Expanded Grouped2" = Table.ExpandTableColumn(#"Merged Queries", "Grouped2", {"Shop Sales"}, {"Shop Sales"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Grouped2", "% Shop Sales", each [#"Dept Sales#(tab)"]/[Shop Sales])
in
    #"Added Custom"

 

 

Another is using grouping and adding a column as follow

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type datetime}, {"Shop", type text}, {"Department", type text}, {"Sales", Int64.Type}}),
    Grouped2 = Table.Group(#"Changed Type", {"Shop","Department"}, {{"Dept Sales	", each List.Sum([Sales])}}),
    #"Added Custom" = Table.AddColumn(Grouped2, "Shop Sales", each List.Sum(Table.SelectRows(#"Changed Type", (x)=>x[Shop]=[Shop])[Sales]))
in
    #"Added Custom"

 

If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h

I'll review these ones over the next few days, (sorry for tardy response to your efforts)

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Kudoed Authors