Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

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

    • Anonymous's avatar
      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

  • 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

  • Anonymous , Try a  new column in Table 2 like

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

    • Anonymous's avatar
      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])

      • V-lianl-msft's avatar
        V-lianl-msft
        Icon for Community Support rankCommunity 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.