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
electrobrit
Post Patron
Post Patron

if this condition, calculate this, otherwise, calculate this

I need the dax for an "if this condition, calculate this, otherwise, calculate this" for each status in the table
If the balance is 0 then the total of Status is based on "price', if the balance is greater than 0 (or otherwise), then the total is based on "balance"
(my dax below does not work)

Total Status A=if(Transaction [balance] =0,
Calculate (
Sum (Transaction [price] ),
Transaction [status] = A, Sum (Transaction [balance]))))
 



Transaction Table  
 balanceprice
StatusA010
StatusA1020
StatusA2030
StatusA010
StatusB00
StatusB100
StatusB200
StatusB010
StatusB1010
StatusB100
StatusC010
StatusC100
StatusC010

 

For example, if looking at Status A for a card visual with the total
If the balance is 0 then the total of Status A is based on "price’, if the balance is greater than 0 (or my “otherwise”), then the total is based on “balance”

In this case, the total for status A = 50
line 1 =10 (based on price because balance is =0)
line 2=10 (based on balance because balance > 0)
line 3 =20 (based on balance because balance > 0)
line 4 = 10 (based on price because balance is =0)


Thank you in advance!

1 ACCEPTED SOLUTION

Here was the solution in case anyone needs something similar.
Create a calc column

BalPrice =

IF(
'Transaction'[BALANCE] = 0,
'Transaction'[PRICE],
'Transaction'[BALANCE]
)


Then a measure:

Sum by Status =

CALCULATE(
SUMX(
'Transaction',
[BalPrice]
),
VALUES( 'Transaction'[STATUS] )
)

View solution in original post

5 REPLIES 5
Greg_Deckler
Super User
Super User

I would suggest using a SWITCH(TRUE() ...) statement. 

 

https://docs.microsoft.com/en-us/dax/switch-function-dax

 


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler 
thanks for the idea.
I was seeing that prior to me asking the question but I could not get it written out correctly. If anyone else can assist, would appreciate help getting past the issue.

StatusA when Bal 0 = CALCULATE( SUM(Transaction[price]), Transaction[status] = "status A", SWITCH(IF(Transaction[balance]="0", sum(Transaction[balance]))))

I am sure there an equivalent in Power Query, in DAX it would be something like:

 

Measure = 
VAR __status = MAX(Transaction[status]
VAR __balance = MAX(Transaction[balance]
RETURN
SWITCH(TRUE(),
  __status = "status A",SUM(Transaction[price]),
  __balance = "0",SUM(Transaction[balance])
)

@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Well, I appreciate you trying. I figured out after trying a few times, I had to add in the commas, etc. since you didn't add them but in the end could't figure out what is between Return and Switch so it made it look like one word in Dax, thus an error. I tried comma, etc.

Anyway, if anyone else can assist. This might have worked but I'm new to some DAX and can't seem to find how to correct it.
Not sure if it likes the _status and _balance at the end either (red error lines) but it's only giving me an error on the Return syntax

 

Here was the solution in case anyone needs something similar.
Create a calc column

BalPrice =

IF(
'Transaction'[BALANCE] = 0,
'Transaction'[PRICE],
'Transaction'[BALANCE]
)


Then a measure:

Sum by Status =

CALCULATE(
SUMX(
'Transaction',
[BalPrice]
),
VALUES( 'Transaction'[STATUS] )
)

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.