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

Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now

Reply
Claudine
Regular Visitor

Summing a Column Grouped by Value in Another Column

Hello, 

 

I'm trying to do a simple sumif (simple in Excel that is) but can't seem to make it work Power BI

 

If I have the sample table below (simplified for this sample but actual data has multiple columns and rows) where I want to add a measure, in column D, where I would sumif sales based on employee.

 

 ABCD
1EmpProdSalesSalesXEmp
2    
3JonesApple100

125

4SmithApple150250
5SmithKiwi100250
6LakeKiwi7575
7JonesDates25125

 

In Excel, my sumif formula (in cell D3) is simple = =SUMIF($A:$A,A3,$C:$C).

 

How do I recreate this sumif based on the value on a column (or aggregate using group by) in Power BI using a measure?

 

Thank you.

1 ACCEPTED SOLUTION

Thanks for the data.

Adding the dimension table to the allexcept clause should get the correct result.

Something like

CALCULATE(SUM(TableA[ONHANDQTY]), ALLEXCEPT(TableA, TableA[ITEMID], TableB))

View solution in original post

8 REPLIES 8
amitchandak
Super User
Super User

@Claudine ,

As a new column

SalesXEmp = sumx(filter(Table, [Emp]=earlier([Emp])),[Sales])Sales

As a new measure
SalesXEmp = calculate(sum(Table[Sales]),allexcept(Table, Table[Emp]))

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

Hello Amit ( @amitchandak ), hi HotChilli ( @HotChilli ),

 

Thanks for your responses.  The ALLEXCEPT was one of the solutions I previously tried but I couldn't get the appropriate result set.

 

After doing some trial and error, I was able to isolate the conditions in which the ALLEXCEPT would work and not work.  I have 2 tables joined via ITEMID.  When adding the measure, if I exclude the column from the secondary table, the measure works as it should.  However, when I include it, which is what I need, it's throwing off my result set and my records seems to grow exponentially, as though it performed a cartesian join.

 

BTW, if I exclude the measure, the joined tables looks good in my result set.  It's acting up when both the measure and the secondary table's column(s) are included.

 

Any ideas?

 

Thanks,

C

Can you provide 2 sample tables (doesn't have to be real data ) and what your desired outcome is please?

Hello @HotChilli ,

 

Can't seem to attach even a txt file...hopefully, the tables below suffices...

 

Table 1:

WAREHOUSEITEMIDONHANDQTYSUPPLYDATESUPPLYDAYS
HAYWDM10019300  
MINNM10019800  
PENHPM10019584  
DALLAM100196001/11/2021 0:00273
HAYWDM10022300  
MINNM100223000  
PENHPM10022589  
DALLAM10022230012/31/2020 0:00262
HAYWDM100304000  
MINNM10030300  
PENHPM100306032  
DALLAM100303045012/15/2020 0:00245
HAYWDM10032700  
MINNM10032400  
DALLAM1003225007/4/2026 0:002273
PENHPM100323396/4/2027 0:002608

 

Table 2:

ITEMIDITEMNAME
M10019MH3.5AUIN1/4-1/4AB
M10022MH3.5AUIN1/4-1/4BB
M10030MH3.5AUIN1/4-1/4CM
M10032MH3.5AUIN1/4-1/4DB

 

Basically, the tables are joined and I've included Item Name (from Table 2) and the last column, QtyTotal, is the measure I want to get.

WAREHOUSEITEMIDITEMNAMEONHANDQTYSUPPLYDATESUPPLYDAYSQTYTOTAL
HAYWDM10019MH3.5AUIN1/4-1/4AB300  2284
MINNM10019MH3.5AUIN1/4-1/4AB800  2284
PENHPM10019MH3.5AUIN1/4-1/4AB584  2284
DALLAM10019MH3.5AUIN1/4-1/4AB6001/11/2021 0:002732284
HAYWDM10022MH3.5AUIN1/4-1/4BB300  6189
MINNM10022MH3.5AUIN1/4-1/4BB3000  6189
PENHPM10022MH3.5AUIN1/4-1/4BB589  6189
DALLAM10022MH3.5AUIN1/4-1/4BB230012/31/2020 0:002626189
HAYWDM10030MH3.5AUIN1/4-1/4CM4000  40782
MINNM10030MH3.5AUIN1/4-1/4CM300  40782
PENHPM10030MH3.5AUIN1/4-1/4CM6032  40782
DALLAM10030MH3.5AUIN1/4-1/4CM3045012/15/2020 0:0024540782
HAYWDM10032MH3.5AUIN1/4-1/4DB700  3939
MINNM10032MH3.5AUIN1/4-1/4DB400  3939
DALLAM10032MH3.5AUIN1/4-1/4DB25007/4/2026 0:0022733939
PENHPM10032MH3.5AUIN1/4-1/4DB3396/4/2027 0:0026083939

 

Regards,

C

Thanks for the data.

Adding the dimension table to the allexcept clause should get the correct result.

Something like

CALCULATE(SUM(TableA[ONHANDQTY]), ALLEXCEPT(TableA, TableA[ITEMID], TableB))

Thank you very much @HotChilli !

Hello Amit,

 

Thank you for your reply. ALLEXCEPT was one of the solutions I tried earlier, but it continued to alter my result set.

 

I just did a trial and error and it seems that they have isolated the condition in which the ALLEXCEPT works and don't work. I have 2 tables joined together through ITEMID. If I exclude the column from the child table, THE ALLEXCEPT produces the expected results, however, when I include it, which is what I need, alters the result set and seems to have increased the number of records as if it were doing Cartesian union.

 

BTW, when I exclude the measure, the resulting set, with the 2 tables looks good.  So it seems to be happening when I have both the measure AND the column from the secondary table included.

 

Any ideas?

 

Thank you

C

HotChilli
Super User
Super User

Write a measure :

MSales = CALCULATE(SUM(TableD[Sales]), ALLEXCEPT(TableD, TableD[Emp]))

Helpful resources

Announcements
OCT PBI Update Carousel

Power BI Monthly Update - October 2024

Check out the October 2024 Power BI update to learn about new features.

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

October NL Carousel

Fabric Community Update - October 2024

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