Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
JMHenriques
Frequent Visitor

SUM and/or count depending on value

Hi all,

 

This is driving me nuts... Take the following data as an example:

 

ContractProductAmount
1001Product 11
1001Product 11
1001Product 10
1001Product 10
5555Product 1231
5555Product 1232
5555Product 1230
5555Product 1230
5555Product 1235
2020Product 1230
2020Product 1230

 

If for the same contract and Product I only have 0 and 1 value total should be 1 -> The case of contract 1001

 

If the contact and product have different numbers it should sum them -> in the case of contract 5555 it should be 8

 

If contract and product only have 0 it should count 1 -> In the case of contract 2020 it should return 1

 

I know I must get there with a measure but Im still search this beautiful forum for some wisdom 🙂 

1 ACCEPTED SOLUTION
jgeddes
Super User
Super User

Something like this might work for you...

Measure = 
var _sum =
SUMX('Table', 'Table'[Amount])
var _zeroRows = 
COUNTROWS(FILTER('Table', 'Table'[Amount] = 0))
var _oneRows =
COUNTROWS(FILTER('Table', 'Table'[Amount] = 1))
var _rowCount =
COUNTROWS('Table')
Return
IF(
    _zeroRows + _oneRows = _rowCount,
    1,
    _sum
)

Using your example table the above measure returns

jgeddes_0-1716510485679.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





View solution in original post

2 REPLIES 2
jgeddes
Super User
Super User

Something like this might work for you...

Measure = 
var _sum =
SUMX('Table', 'Table'[Amount])
var _zeroRows = 
COUNTROWS(FILTER('Table', 'Table'[Amount] = 0))
var _oneRows =
COUNTROWS(FILTER('Table', 'Table'[Amount] = 1))
var _rowCount =
COUNTROWS('Table')
Return
IF(
    _zeroRows + _oneRows = _rowCount,
    1,
    _sum
)

Using your example table the above measure returns

jgeddes_0-1716510485679.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





Many thanks @jgeddes, it worked perfectly, well almost since I forgot a condition...   

in the case of the contract having a number >1 even with zeros, than it needs to be that number. 

 

In the case below, the number should be 3. 

 

ContractProductAmount
1001Product 13
1001Product 13
1001Product 10
1001Product 10

 

Added a new variable with a MAX function and it worked.

 

NumberNE =
var _sum =
SUMX('Contracts per CaPM', 'Contracts per CaPM'[Product - Amount of contracted NEs per contract - Products])
var _zeroRows =
COUNTROWS(FILTER('Contracts per CaPM', 'Contracts per CaPM'[Product - Amount of contracted NEs per contract - Products] = 0))
var _oneRows =
COUNTROWS(FILTER('Contracts per CaPM', 'Contracts per CaPM'[Product - Amount of contracted NEs per contract - Products] = 1))
var _plusRows =
CALCULATE(MAX( 'Contracts per CaPM'[Product - Amount of contracted NEs per contract - Products]))
var _rowCount =
COUNTROWS('Contracts per CaPM')
Return
IF(
    _zeroRows + _oneRows = _rowCount,
    1,
    IF(
        _rowCount > 1,
        _plusRows,
       
    _sum)
)

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.