cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
hongjyan
Helper II
Helper II

measure to sum column B if column A meets criteria otherwise sum column C

Hi,

I have a table like

 

Table_Test = 
DATATABLE
        (
        "name"      , STRING , 
        "Status"    , STRING ,
        "price"     , INTEGER ,
        "cost"      , INTEGER ,
        {
            {"A", "sold", 200, 100},
            {"A", "onHand", 200, 100},
            {"B","sold", 300, 150},
            {"B","onHand", 300,150}
        }    
        ) 

 

I want to create a measure like this:

 

Total cash = IF( [Status] = "sold", SUM([price]), SUMX(Table_Test, [cost] * -1) ) ​

 

of course, it can not work since [status] is not in row-context.  

And I do not want to new a column like this:

 

cash = IF([Status] = "sold", [price], [cost]*-1)

 

then sum it to realize my wish.
appreciate help, thanks!
2 ACCEPTED SOLUTIONS
amitchandak
Super User
Super User

@hongjyan , if they are from one table

sumx(Table_Test,IF( Table_Test[Status] = "sold", (Table_Test[price]), Table_Test[cost] * -1 ) )​



New Power BI Features
Datamarts: https://youtu.be/8tskWsJTEpg
Field Parameters : https://youtu.be/lqF3Wa1FllE?t=70
Did I answer your question? Mark my post as a solution! Appreciate your Kudos!! !!
Dashboard of My Blogs !! Connect on Linkedin !! Proud to be a Super User!
Want To Learn Power BI
Learn Power BI Beginners !! Advance Power BI Concepts !! Power BI For Tableau User !! Learn Power BI in Hindi !!
!! Subscribe to my youtube Channel !!

View solution in original post

Hi @hongjyan 

try

sumx( Table_Test,
           IF( Table_Test[Status] = "onHand",
                Table_Test[cost] * -1,
                IF(Table_Test[price] > 0, [price], 0)
           )
)

do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn

View solution in original post

4 REPLIES 4
amitchandak
Super User
Super User

@hongjyan , if they are from one table

sumx(Table_Test,IF( Table_Test[Status] = "sold", (Table_Test[price]), Table_Test[cost] * -1 ) )​



New Power BI Features
Datamarts: https://youtu.be/8tskWsJTEpg
Field Parameters : https://youtu.be/lqF3Wa1FllE?t=70
Did I answer your question? Mark my post as a solution! Appreciate your Kudos!! !!
Dashboard of My Blogs !! Connect on Linkedin !! Proud to be a Super User!
Want To Learn Power BI
Learn Power BI Beginners !! Advance Power BI Concepts !! Power BI For Tableau User !! Learn Power BI in Hindi !!
!! Subscribe to my youtube Channel !!

@amitchandak,

 

thanks you!!!

 

how about if I want to consider cost but ignore price if it is negetive when do sum, for example

 

 

Table_Test = 
DATATABLE
        (
        "name"      , STRING , 
        "Status"    , STRING ,
        "price"     , INTEGER ,
        "cost"      , INTEGER ,
        {
            {"A", "sold", 200, 100},
            {"A", "onHand", 200, 100},
            {"B","sold", 300, 150},
            {"B","onHand", 300,150},
            {"oil20200426", "won'tsold", -50, 100}
        }    
        ) 

 

 

 

 

 

 

how to realiza something like
sumx( Table_Test,
           IF( Table_Test[Status] = "onHand",
                Table_Test[cost] * -1,
                Table_Test[price] when [price] is positive)
           )
)​?

Hi @hongjyan 

try

sumx( Table_Test,
           IF( Table_Test[Status] = "onHand",
                Table_Test[cost] * -1,
                IF(Table_Test[price] > 0, [price], 0)
           )
)

do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn
AllisonKennedy
Super User
Super User

You can use the FILTER function instead.

 

Something like: 

Total cash = SUMX(FILTER(Table_Test, Table_Test[Status]= "sold"), Table_Test[price]) - SUMX(FILTER(Table_Test, Table_Test[status]<>"sold"),Table_Test[cost])

Has this post solved your problem? Please mark it as a solution so that others can find it quickly and to let the community know your problem has been solved. 

 

If you found this post helpful, please give Kudos.

I work as a trainer and consultant for Microsoft 365, specialising in Power BI and Power Query. 

https://sites.google.com/site/allisonkennedycv

 


Please @mention me in your reply if you want a response.

Check out my Olympics report with live stats - KUDOS much appreciated

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

Helpful resources

Announcements
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Power BI Dev Camp Session 23 768x460.jpg

Check it Out!

Mark your calendars and join us on Thursday, June 30 at 11a PDT for a great session with Ted Pattison!

June 20 episode 7 with aka link 768x460.jpg

The Power BI Community Show

Join us on June 20 at 11 am PDT when Kim Manis shares the latest on Azure Synapse analytics, the Microsoft Intelligent Data Platform, and notable Power BI Updates from Microsoft Build 2022.

Top Solution Authors
Top Kudoed Authors