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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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...

 

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors