Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Distinct Sum Then average in Measure

I need to create a measure to distinct Sum the value by ID and show average in the total. 

 

IDCodeValueQuantity 
1A10010
1B20012
1B20014
1B20016
2A50018
2A50020
2B80022
2B80024


Finally result:

 IDValue
 1300
 21300
Total 800




  • Hi Eric,

     

    Try the following:

     

    DistinctSum = 
    
    VAR DistinctTableSum = 
    	ADDCOLUMNS (
    		VALUES ( ExampleTable[ID] ),
    		"@SumColumn", CALCULATE (SUMX ( VALUES ( ExampleTable[Value] ), ExampleTable[Value] ) )
    		)
    
    VAR Result =
        AVERAGEX (
            DistinctTableSum,
            [@SumColumn]
        )
    
    RETURN Result

4 Replies

  • bcdobbs's avatar
    bcdobbs
    Community Champion

    Hi Eric,

     

    Try the following:

     

    DistinctSum = 
    
    VAR DistinctTableSum = 
    	ADDCOLUMNS (
    		VALUES ( ExampleTable[ID] ),
    		"@SumColumn", CALCULATE (SUMX ( VALUES ( ExampleTable[Value] ), ExampleTable[Value] ) )
    		)
    
    VAR Result =
        AVERAGEX (
            DistinctTableSum,
            [@SumColumn]
        )
    
    RETURN Result
    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks Ben, It do help a lot.  Finally I also figured it out. This is my formula for your reference. Please feel free to give me some advice 

      DistinctSum_average =
      VAR a_ =
      SUMMARIZE (
      Sheet1,
      Sheet1[ID],
      Sheet1[Code],
      "average", AVERAGE ( Sheet1[Value] )
      )
      RETURN
      IF (
      HASONEFILTER ( Sheet1[ID] ),
      SUMX ( a_, [average] ),
      DIVIDE ( SUMX ( a_, [average] ), DISTINCTCOUNT ( Sheet1[ID] ) )
      )