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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
camelia_
Frequent Visitor

How to subtract columns from 2 different table based on buy/sell condition

hii~

I need help in finding this relative price( to find profit/loss),where i need to subtract Traded Price(from Sample Transactions table) with Market Price(from Price table), based on the columns of buy/sell (from Sample Transactions table).

 

Here is what i did, but it can't be calculated and I can't put it in the matrix table.

Column = IF('Sample Transactions'[Buy/Sell]= "Sell",'Sample Transactions'[Total Trade Price*]-SUM('Price'[PRICE]), IF('Sample Transactions'[Buy/Sell]="Buy", -1*('Sample Transactions'[Total Trade Price*]-SUM('Price'[PRICE])),""))
Also, i did this one,
Measure = IF(ALL('Sample Transactions'[Buy/Sell])= "Sell", SUM('Sample Transactions'[Total Trade Price*])-SUM('Price'[PRICE] ), IF(ALL('Sample Transactions'[Buy/Sell])="Buy", -1*(SUM('Sample Transactions'[Total Trade Price*])-SUM('Price'[PRICE])),""))
 
camelia__0-1659679470583.png

 

But also can't be calculated. Hope someone can help me solve this problem.
Thanks in advance.
1 ACCEPTED SOLUTION
YalanWu_test
Helper I
Helper I

Hi, @camelia_ ;

Try it.

Measure =
IF (
    MAX ( 'Sample Transactions'[Buy/Sell] ) = "Sell",
    SUM ( 'Sample Transactions'[Total Trade Price*] ) - SUM ( 'Price'[PRICE] ),
    IF (
        MAX ( 'Sample Transactions'[Buy/Sell] ) = "Buy",
        -1
            * ( SUM ( 'Sample Transactions'[Total Trade Price*] ) - SUM ( 'Price'[PRICE] ) ),
        BLANK ()
    )
)

or

Column =
IF (
    'Sample Transactions'[Buy/Sell] = "Sell",
    'Sample Transactions'[Total Trade Price*] - SUM ( 'Price'[PRICE] ),
    IF (
        'Sample Transactions'[Buy/Sell] = "Buy",
        -1
            * ( 'Sample Transactions'[Total Trade Price*] - SUM ( 'Price'[PRICE] ) ),
        BLANK ()
    )
)

Best Regards,

 

View solution in original post

5 REPLIES 5
YalanWu_test
Helper I
Helper I

Hi, @camelia_ ;

Try it.

Measure =
IF (
    MAX ( 'Sample Transactions'[Buy/Sell] ) = "Sell",
    SUM ( 'Sample Transactions'[Total Trade Price*] ) - SUM ( 'Price'[PRICE] ),
    IF (
        MAX ( 'Sample Transactions'[Buy/Sell] ) = "Buy",
        -1
            * ( SUM ( 'Sample Transactions'[Total Trade Price*] ) - SUM ( 'Price'[PRICE] ) ),
        BLANK ()
    )
)

or

Column =
IF (
    'Sample Transactions'[Buy/Sell] = "Sell",
    'Sample Transactions'[Total Trade Price*] - SUM ( 'Price'[PRICE] ),
    IF (
        'Sample Transactions'[Buy/Sell] = "Buy",
        -1
            * ( 'Sample Transactions'[Total Trade Price*] - SUM ( 'Price'[PRICE] ) ),
        BLANK ()
    )
)

Best Regards,

 

hii @YalanWu_test 

If i have this function,

Gain/Loss = IF (
MAX ( 'Sample Transactions'[Buy/Sell] ) = "Sell",
(SUM ( 'Sample Transactions'[Total Trade Price*] ) - SUM ( 'Price'[PRICE] )) / SUM ( 'Price'[PRICE] ),
IF (
MAX ( 'Sample Transactions'[Buy/Sell] ) = "Buy",
(-1*( SUM ( 'Sample Transactions'[Total Trade Price*] ) - SUM ( 'Price'[PRICE] )) / SUM ( 'Price'[PRICE] )),
BLANK ()
)
)
 
how can i seperate it to put in 2 cards visualisation as Gain and Loss?
because i try to do seperate measure,
Gain = IF('Sample Transactions'[Gain/Loss] > 0, 'Sample Transactions'[Gain/Loss] , BLANK())
Loss = IF('Sample Transactions'[Gain/Loss] <= 0, 'Sample Transactions'[Gain/Loss] , BLANK())
but it appears blank the card

omg it worked,thanks a lot!

amitchandak
Super User
Super User

@camelia_ , You need to create a common column and then you can take diff of measures from two tables against common dimensions 

https://amitchandak.medium.com/power-bi-when-i-asked-you-to-create-common-tables-a-quick-dax-solutio...

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

 

Hi @amitchandak , I already have the combined table like in attached link, its just that i can't subtract it since it involves 2 different tables with condition.

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.