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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
qianqianat007
Regular Visitor

Calculate sum of distinct category plus value of each blank category

Dear community, I am looking for a way to calculate sum of a column ("Value" colume in the table shown below). Appreciate any help you can provide. Thanks.

 

 Category 1Value
  50
  40
 A30
 A30
 B20
 C10
 C10
Expected result50+40+30+20+10150
1 ACCEPTED SOLUTION

@qianqianat007 
Please refer to attached sample file

1.png

Sum = 
SUMX (
    VALUES ( 'Table'[Category] ),
    IF (
        'Table'[Category] = BLANK (),
        SUMX ( CALCULATETABLE ( 'Table' ), 'Table'[Value] ),
        SUMX ( CALCULATETABLE ( VALUES ( 'Table'[Value] ) ), 'Table'[Value] )
    )
)

View solution in original post

13 REPLIES 13
tamerj1
Super User
Super User

Hi @qianqianat007 

please try

Sum =
SUMX (
VALUES ( 'Table'[Category] ),
IF (
'Table'[Category] = BLANK (),
SUMX ( CALCULATETABLE ( 'Table' ), 'Table'[Value] ),
SUMX ( CALCULATETABLE ( VALUES ( 'Table'[Value] ) ), 'Table'[Value] )
)
)

Dear @tamerj1 thanks for sharing your idea! I tried it and get 240 as result (expect 200).

@qianqianat007 

Can you please paste the code that you have tried?

@qianqianat007 

Can you please paste the code that you have tried?

Dear @tamerj1 I tried your this code and got 240 as result (expect 200).

 

Sum =
SUMX (
VALUES ( 'Table'[Category] ),
IF (
'Table'[Category] = BLANK (),
SUMX ( CALCULATETABLE ( 'Table' ), 'Table'[Value] ),
SUMX ( CALCULATETABLE ( VALUES ( 'Table'[Value] ) ), 'Table'[Value] )
)
)

@qianqianat007 
Please refer to attached sample file

1.png

Sum = 
SUMX (
    VALUES ( 'Table'[Category] ),
    IF (
        'Table'[Category] = BLANK (),
        SUMX ( CALCULATETABLE ( 'Table' ), 'Table'[Value] ),
        SUMX ( CALCULATETABLE ( VALUES ( 'Table'[Value] ) ), 'Table'[Value] )
    )
)

wow it works! thanks @tamerj1 

qianqianat007
Regular Visitor

Dear community, I am looking for a way to calculate sum of a column ("Value" colume in the table shown below). Appreciate any help you can provide. Thanks.

 

 Category 1Value
  50
  40
 A30
 A30
 B20
 C10
 C10
Expected result: 50+40+30+20+10=150

@Greg_DecklerIt works very well for this data sample, thank you! But in my real data, empty category rows can sometimes have same value (see below, expected result is 200). Any idea how can I solve this?

CategoryValue
 50
 40
 50
A30
A30
B20
C10
C10

@qianqianat007 I'm not quite clear as to the logic of what is supposed to be summed and what is not supposed to be summed. What is supposed to be summed in this latest example in order to get you to 200?



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Dear @Greg_Deckler, in the latest example, for rows where Category is empty (row 1, 2, 3), each Value should be counted (50 + 50 + 40); for rows where Category is not empty (row 5 to 9), only distinct value should be counted (30 + 20 + 10).  Hope that's clear now.. Thanks!

 

CategoryValue   
 50   
 50   
 40   
A30   
A30   
B20   
C10   
C10   
expected result: 50 + 50 + 40 + 30 + 20 + 10 = 200

a small correction: row 5 to 9 --> row 4 to 8

@qianqianat007 Maybe something like SUMX(DISTINCT('Table'[Value]), [Value]) ?



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.