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

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.

Reply
Anonymous
Not applicable

Sum if two fields match from two different columns

Hello, 

 

I would add column values from two different tables

 

Sum Total Sales column if Table1 Account field is equal to Table 2 Account field. When I run the below query, its provided me sum of all the accounts. 

 

Column = If('Table1'[T1Account] = RELATED(Table2[T2Account]), Sum('Table1'[Sales]))

 

Edit: Added Relationship

Table 1         Table 2

Account * : 1 Account

 

Thanks in advance

1 ACCEPTED SOLUTION

Hi @Anonymous ,

 

Based on the one to many relationship you have created. You can use the CROSSFILTER function to calculate the expect result.

Try to create calculated column or measure like this:

Column = CALCULATE(SUM(table1[Sales]),CROSSFILTER(table2[T2Account],table1[T1Account],OneWay))

test_sumif.PNGtest_sumif2.PNG

 

Best Regards,
Liang
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

6 REPLIES 6
amitchandak
Super User
Super User

@Anonymous , Try a  new column in Table 2 like

sumx(filter(Table1, table1[T1Account]=Table2[T2Account]),Table1[Sales])

Anonymous
Not applicable

After working through your queries. I have identified that I am looking for something else

 

If MainAccount is equal to SubAccount then Sum all Sales of all Sub Accounts and display MainAccount , SubAccount and Sum of all Sales

I am trying following query in new column but giving unexpected parementer error for Sales column

 

 

Sumx(FILTER('Account_Sales','Account_Sales'[MainAccount]= 'Account_Sales'[SubAccount],[Sales])

Hi @Anonymous ,

 

Based on the one to many relationship you have created. You can use the CROSSFILTER function to calculate the expect result.

Try to create calculated column or measure like this:

Column = CALCULATE(SUM(table1[Sales]),CROSSFILTER(table2[T2Account],table1[T1Account],OneWay))

test_sumif.PNGtest_sumif2.PNG

 

Best Regards,
Liang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

MarcoPessina
Resolver IV
Resolver IV

Hi @Anonymous ,

you can try this formula:

SumOfSales = CALCULATE(
    SUM(Table1[Sales]),Table1[AccountID]=RELATED(Table2[AccountID]),ALLEXCEPT(Table1,Table1[AccountID])
    )

Annotation 2020-05-08 171802.jpg 

Hope it helps.

Cheers,

Marco

Greg_Deckler
Super User
Super User

Sounds like the tables aren't related. Please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Hi Greg, 

 

Both columns have similar values but coming from two different tables. Sum is adding all the columns in the table instead where Table 1 Account is equal to Table 2 Account 

 

Thanks

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.