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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Mishutka
Frequent Visitor

SUMIFS DAX equivalent sum same item in different rows and columns

I am trying to calculate a sum based a value that could be in one of two columns:

The data is broadly

DateCustomerSales valueSalespersonSales Assist
01-JanAcme Subs100JohnJane
05-JanWidgets Inc75JohnLesley
07-JanAcme Subs125JaneJohn
07-JanWidgets Inc175LesleyJohn

 

What I want is a sum for each Salesperson or Sales Assistant

Ie total for John should be 100+75+125+150

for Jane should be 100 + 125

for Lesley should be 75 + 175

 

I have tried sum(table[Sales Value])+calculate(sum(table[Sales Value]),.. but then I cannot get the filter correct. If i say salesperson=sales assist I get nothing (obviously as they arent on the same rows. I have tried ALL, ALLSELECTED, VALUES and cant get anywhere.

 

Anyone know what DAX formula I need to get the summary results I have shown?

4 REPLIES 4
lbendlin
Super User
Super User

Here's a crude way of doing this using a table variable. I am sure there are more elegant solutions

 

 

Table 2 = 
var sr = selectcolumns('Table (4)',"Date",'Table (4)'[Date],"Customer",'Table (4)'[Customer],"Sales",'Table (4)'[Sales value],"Salesperson",'Table (4)'[Salesperson])
var sa = selectcolumns('Table (4)',"Date",'Table (4)'[Date],"Customer",'Table (4)'[Customer],"Sales",'Table (4)'[Sales value],"Salesperson",'Table (4)'[Sales Assist])
var combined = union(sr,sa)
return combined

 

 

Basically adding the original table back to itself but shifting the assist column to the salesperson column. 

Thanks, this will work, although it may be easier to combine the tables in Power Query - especially as thes are quite large data sets and I dont want slow run time performance. I found a really helpful article about variables and context https://www.sqlbi.com/articles/variables-in-dax/ which inspired me to try DAX filtering again. Thus I got to 

var Assister = MAX(Table1[Salesperson])
return
CALCULATE(SUM(Table1[Sales value]),all(Table1[Salesperson]),Table1[Sales Assist]=Assister)
 
But this gives a slightly odd result: 
Row LabelsSum of Sales valueAssisterSum
01/01/2020  
John100 
05/01/2020  
John75 
07/01/2020  
Jane125 
Lesley175 
Grand Total47575

 

This is strnage in that it has a grand total, but no line by line values so I am not quite sure what is going on

ah, I am getting there, I just need to somehow deal with the dates. If I change all the dates to 7/1 the table now reads

Row LabelsSum of Sales valueAssisterSum
07/01/2020  
Jane125100
John175300
Lesley17575
Grand Total47575

 

The Grand total is wrong becasue of my use of Max, which I will filter out with something like HASONEFILTER, but I need to think about how to deal with dates. I will post my solution here if I get it working

 

Ok, I am nearly there but still havent quite got it. I have updated the table and the measures.

I have a measure 

AssisterSumXX = var Assister = MAX(Table1[Salesperson])
var MinDate = min(Table1[Date])
var MaxDate = max(Table1[Date])
return
if(HASONEFILTER(Table1[Salesperson]),CALCULATE(SUM(Table1[Sales value])+0,all(Table1[Salesperson]),

Table1[Sales Assist]=Assister),SUM(Table1[Sales value]))

 

And in order to try to force a value for each row, I have another SalesX=[Sum of Sales value]+0 and finally Total Attributable Sales=[SalesX]+[AssisterSumXX]

 

Here are the data table and pivot table output.

Annotation 2020-06-22 162106.jpgAnnotation 2020-06-22 162124.jpg

As you can see, the Grand Totals are correct, but the row and row totals are wrong for 8/1 where John has no Sales, but a Sales Assist - he should show 107 under AssisterSumXX and the row total for John should also be 107. Even though this shows zero, the totals are correct.  Anyone got any ideas to close this thing down? I have tried various attempts at Values and adding zero but to no avail.  All help really gratefully received

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors