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
Anonymous
Not applicable

Correct DAX formula with nested IF and totals calculation

Hi All,
I have a simple requirement to calculate the Refund Grand Total, the SQL looks like this:
SELECT
COALESCE(SUM(CASE WHEN (ct_transactions.dataset = 'ticketRefundOrder') THEN case when ct_transactions.currencycode = "CAD" then ct_transactions.grandtotal * 0.72
when ct_transactions.currencycode = "USD" then ct_transactions.grandtotal * 1
when ct_transactions.currencycode = "GBP" then ct_transactions.grandtotal * 0.81
when ct_transactions.currencycode = "EUR" then ct_transactions.grandtotal * 0.92
else 0 End ELSE NULL END), 0) AS ct_transactions_refund_sum_grand_total

I am trying to convert this SQL to DAX in PowerBI as below:

SWITCH(
TRUE (),
('Data Transactions'[Dataset]) = "ticketRefundOrder" && ('Data Transactions'[Currency Code]) = "CAD",
SUMX('Data Transactions',[Grand Total]*0.72) ,
('Data Transactions'[Dataset]) = "ticketRefundOrder" && ('Data Transactions'[Currency Code]) = "USD",
SUMX('Data Transactions',[Grand Total]*1),
('Data Transactions'[Dataset]) = "ticketRefundOrder" && ('Data Transactions'[Currency Code]) = "GBP",
SUMX('Data Transactions',[Grand Total]*0.81),
('Data Transactions'[Dataset]) = "ticketRefundOrder" && ('Data Transactions'[Currency Code]) = "EUR",
SUMX('Data Transactions',[Grand Total]*0.92))


But this gives me an error saying " A single value for column 'Dataset' in table 'Data Transactions' can not be determined.
This can happen when a measure formula refers to a column that contains many values without specifying an aggregation such as min, max, sum, or count to get a single result."
I have tried using Value/Values('Data Transactions'[Dataset]) but its functionality is to convert a text string to a number which is not right. Also, both the Columns 'Dataset and 'Currency Code' have Data Type as Text.

I am not sure what I am missing here? Can you please help me correct it or give me an alternate way? 

1 ACCEPTED SOLUTION
ebeery
Solution Sage
Solution Sage

@Anonymous the measure needs to be able to determine a single value for 'Data Transactions'[Dataset], otherwise how would it know whether it equals "ticketRefundOrder" or not?

What do you mean when you say that VALUES() is converting a text string to a number?  

Have you tried using SELECTEDVALUE('Data Transactions'[Dataset]) instead?  Have you tried writing this as a calculated column instead of a measure (you would need to re-work the SUMX() portion)

View solution in original post

2 REPLIES 2
ebeery
Solution Sage
Solution Sage

@Anonymous the measure needs to be able to determine a single value for 'Data Transactions'[Dataset], otherwise how would it know whether it equals "ticketRefundOrder" or not?

What do you mean when you say that VALUES() is converting a text string to a number?  

Have you tried using SELECTEDVALUE('Data Transactions'[Dataset]) instead?  Have you tried writing this as a calculated column instead of a measure (you would need to re-work the SUMX() portion)

Anonymous
Not applicable

Hi @ebeery,
Thanks for replying. Yes, SELECTEDVALUE worked. I knew I had to add something for the Columns 'Dataset and 'Currency Code' but was not sure what exactly.
Thank you.

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.