Forum Discussion
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
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))Best Regards,
Liang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
6 Replies
- Greg_Deckler
Community Champion
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
- AnonymousNot 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
- MarcoPessina
Resolver IV
Hi Anonymous ,
you can try this formula:
SumOfSales = CALCULATE( SUM(Table1[Sales]),Table1[AccountID]=RELATED(Table2[AccountID]),ALLEXCEPT(Table1,Table1[AccountID]) )Hope it helps.
Cheers,
Marco
- amitchandak
Super User
Anonymous , Try a new column in Table 2 like
sumx(filter(Table1, table1[T1Account]=Table2[T2Account]),Table1[Sales])
- AnonymousNot 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])
- V-lianl-msft
Community Support
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))Best Regards,
Liang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.