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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
jjbates
Regular Visitor

SUMIF in Query Editor on Power BI Desktop?

I am trying to do a simple sumif in the Query Editor of the PowerBI desktop. I cannot edit the source data, so I need a calculated column as a new column that simply sums one column if it matches the values in another. I have done this with DAX before in Power Excel using this formula: CALCULATE(SUM([Received Qty]),FILTER('sheet 1',[Location]=EARLIER([Location])

 

and need something similar in the M language. Does this exist? Please help.

 

1 ACCEPTED SOLUTION
v-qiuyu-msft
Community Support
Community Support

Hi @jjbates,

 

You can open Query Editor, use Group By feature:

 

q3.PNG

 

The backend M formula is below:

 

= Table.Group(dbo_DimSalesTerritory, {"SalesTerritoryGroup"}, {{"Total", each List.Sum([SalesTerritoryKey]), type number}})

 

Best Regards,
Qiuyun Yu

Community Support Team _ Qiuyun Yu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
JcROXford
New Member

All the solutions I find point to doing a "Group By" in power query. However, there are many situations where every row is unique, but they often share a common identifier that needs a sum-if. For example, I can have a list of employees (All Unique). Each employee has a wage and a department. I want my end result to show the sum of wages related to the department as a percent. I don't want to group on department, because I will not be able to see the employee with their percentage of earnings as it relates to the department. A simple sum-if would easily do this. Does anyone have a working solution?  See table below. That's the output I'm looking for from Power Query.

 

EmployeeDepartmetn Wages  Department Wages % of Department
CharlesSales  50,000.00                150,000.0033%
KristiSales  40,000.00                150,000.0027%
MarySales  60,000.00                150,000.0040%
BobService  65,000.00                194,000.0034%
BillService  62,000.00                194,000.0032%
IsaacService  67,000.00                194,000.0035%
CharlesParts  87,000.00                269,000.0032%
WendyParts  45,000.00                269,000.0017%
SamParts  73,000.00                269,000.0027%
AaronParts  64,000.00                269,000.0024%
WayneFinance  84,000.00                180,000.0047%
JodiFinance  53,000.00                180,000.0029%
JamesFinance  43,000.00                180,000.0024%
v-qiuyu-msft
Community Support
Community Support

Hi @jjbates,

 

You can open Query Editor, use Group By feature:

 

q3.PNG

 

The backend M formula is below:

 

= Table.Group(dbo_DimSalesTerritory, {"SalesTerritoryGroup"}, {{"Total", each List.Sum([SalesTerritoryKey]), type number}})

 

Best Regards,
Qiuyun Yu

Community Support Team _ Qiuyun Yu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
MarcelBeug
Community Champion
Community Champion

For example the sum of all values where Group = "A":

= List.Sum(Table.SelectRows(PreviousStep, each [Group]="A")[Value])

 

Specializing in Power Query Formula Language (M)

Hello Marcel,

 

I am trying your formula but I cannot get it to work.


I am doing something a little more complicated:

 

= Table.Group(#"Removed Errors", {"order no."}, {{"Adj Gross Value", List.Sum(Table.SelectRows(PreviousStep, each Text.Start([ReferenceID], 3) = "ADJ") [Gross Value]), type number}}

 

As you can see I want to match using Text.Start but I cannot get it to work. This is my error:

 

Expression.Error: The name 'PreviousStep' wasn't recognized. Make sure it's spelled correctly.

 

If you can help thanks in advance.

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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