Forum Discussion

Kopec's avatar
Kopec
Helper I
1 year ago
Solved

Sum the Unique values to appropriate value

Can anybody help me with this:
Sum the unique values ​​in column "B" to the appropriate value in column "A".

 

Thank you,

Jan

 

 

  • Kopec - You can use the below to get your desired answer (just change the column names):

     

    VAR _table = SUMMARIZE( 'Table', 'Table'[Column1],  'Table'[Column2] )
    
    RETURN
    SUMX( _table, 'Table'[Column2])

     

     

     

    If this works for you please accept as the solution to help others with the same challenge. 

  • Anonymous's avatar
    Anonymous
    1 year ago

    Hi Kopec 

    The formula i offered can work in power pivot.

     

    Best Regards!

    Yolo Zhu

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

9 Replies

  • Hi Kopec ,

     

    You can achieve this in Power BI using DAX with a measure that sums the unique values of Column B for each corresponding value in Column A. The following DAX measure accomplishes this:

    Result =
    SUMX (
        VALUES ( 'Table'[Column B] ),
        'Table'[Column B]
    )
    

    This measure works by extracting the unique values in Column B for each group in Column A using VALUES('Table'[Column B]), then iterating over these unique values and summing them with SUMX(...). If you prefer to do this transformation in Power Query, you would first remove duplicates from Column B while keeping only unique values for each group in Column A. Then, you would group by Column A and sum the unique values in Column B. Finally, merge this result back to the original table to display the calculated sum in a new column.

     

    Best regards,

     

     

     

    • Kopec's avatar
      Kopec
      Helper I

      It's almost done, but I need: 

      SUMX (
          VALUES ( 'Table'[Column B] ),
          'Table'[Column A]
      )

      In Column "A" is string.... I have got a mistake. I suppose because of that STRING values in Column A.
      Can you help me?

      • mark_endicott's avatar
        mark_endicott
        Super User

        Kopec - You can use the below to get your desired answer (just change the column names):

         

        VAR _table = SUMMARIZE( 'Table', 'Table'[Column1],  'Table'[Column2] )
        
        RETURN
        SUMX( _table, 'Table'[Column2])

         

         

         

        If this works for you please accept as the solution to help others with the same challenge. 

  • Hi Mark,
    ist look great, but I need to get value for every row in the table. According to the picture (column "RESULT"): 

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi,

      Thanks for the solution mark_endicott  and DataNinja777  offered, and i want to offer some more information for user to refer to.

      hello Kopec , mark_endicott 's solution is right, you can use it as a calculated column .

      Column =
      VAR a =
          SUMMARIZE ( 'Table', [Column1], [Column2] )
      RETURN
          SUMX ( FILTER ( a, [Column1] = EARLIER ( 'Table'[Column1] ) ), [Column2] )
      

      Output

      Best Regards!

      Yolo Zhu

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

       

       

       

      • mark_endicott's avatar
        mark_endicott
        Super User

        Anonymous - Thank you, I always forget about calculated columns! Can I request we mark my solution please as this one is solved!

    • mark_endicott's avatar
      mark_endicott
      Super User

      Kopec - As far as I'm aware this will be nearly impossible in a measure due to the way Power BI groups identical rows in it's visualisations. Even if you add an index column to undo the grouping, there will still be an issue with the way we have to use SUMMARIZE to remove the row duplication to get the result you want.

      There may be a solution out there for you but I've tried a few, including adding a duplicate of the table, and none seem to work.

       

      I suggest you think about why it is necessary to present your data in this way, and if the solution I have provided works, or use Excel.