Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Finding the sum from two columns on the same table

Hello, I am new to power bi but I am having hard time finding the sum of two columns on the same table. Example: I want to find the total shipments going to each state. Thank You for your help.

Table1

IdFromToShipments
1New YorkTexas52
2CA New York15
3TexasCA32
4WIMN58
5MNTexas40
  • Hi, Anonymous 

    Try formula as below:
    calculated table:

    Table = 
    DISTINCT (
        UNION (
            SELECTCOLUMNS ( Table1, "State", Table1[From] ),
            SELECTCOLUMNS ( Table1, "State", Table1[To] )
        )
    )

    calculated column:

    Total Shipments = 
    CALCULATE (
        SUM ( Table1[Shipments] ),
        FILTER ( 'Table1', 'Table1'[From] = 'Table'[State] )
    )
        + CALCULATE (
            SUM ( Table1[Shipments] ),
            FILTER ( 'Table1', 'Table1'[To] = 'Table'[State] )
        )

    The result will show as below:

    Best Regards,
    Community Support Team _ Eason
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

7 Replies

  • Anonymous 

    in PQ, select both ID and shipments columns and unpivot other columns

    then you can get what you want

  • v-easonf-msft's avatar
    v-easonf-msft
    Icon for Community Support rankCommunity Support

    Hi, Anonymous 

    Try formula as below:
    calculated table:

    Table = 
    DISTINCT (
        UNION (
            SELECTCOLUMNS ( Table1, "State", Table1[From] ),
            SELECTCOLUMNS ( Table1, "State", Table1[To] )
        )
    )

    calculated column:

    Total Shipments = 
    CALCULATE (
        SUM ( Table1[Shipments] ),
        FILTER ( 'Table1', 'Table1'[From] = 'Table'[State] )
    )
        + CALCULATE (
            SUM ( Table1[Shipments] ),
            FILTER ( 'Table1', 'Table1'[To] = 'Table'[State] )
        )

    The result will show as below:

    Best Regards,
    Community Support Team _ Eason
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thank You So much!!!

  • Anonymous , to what brings from and to in same column and show the sum ?

     

    if yes you need to create a table 

    location = distinct(distinct(Table[from]),distinct(Table[to]))

     

    join with both to and from, one will be inactive assume to 

     

    then create a measure 

    measure = sum(Table[Shipments])  + CALCULATE(sum(Table[Shipments]), userelationship(table[to], location [from]))

     

    if needed refer

    https://radacad.com/userelationship-or-role-playing-dimension-dealing-with-inactive-relationships-in-power-bi

     

  • Anonymous's avatar
    Anonymous
    Not applicable

    thanks for the reply, when I try to create the Location table using that formula(location = distinct(distinct(Table[from]),distinct(Table[to]))), i am getting error

  • Anonymous 

    what's the expected result based on your sample data?

    • Anonymous's avatar
      Anonymous
      Not applicable

      it would be total shipment would be = sum of shipment( From + To)

      StateShipment
      New York 67
      CA47
      Texas124
      MN98
      WI58