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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
KT9
Regular Visitor

DAX

Hi all, I am new to power BI. trying to create a calculated column using DAX. But its throwing me the error says that " Expressions that yield variant data-type cannot be used to define calculated columns". Please help. Thanks in advance!

The Rule is
If Final = Negative, then Negative
If Final = positive AND type = ABCD, then display the associated value in numeric_result(Here numeric_result is a column name in sql query and it contains decimal value)

 

Result =
SWITCH(TRUE(),
Tablename[Final] = "Negative", "Negative",
Tablename[Final] = "Positive" && Tablename[Type] = "ABCD", max(Tablename[Numeric_Result]))

5 REPLIES 5
123abc
Community Champion
Community Champion

It seems like you're encountering an error in your DAX expression because the MAX() function expects a column of values to return the maximum from, but you're passing a single value, which is not allowed in calculated columns.

To fix this, you can use an IF() statement instead of MAX(). Here's how you can adjust your DAX expression:

 

Result =
SWITCH(
TRUE(),
Tablename[Final] = "Negative", "Negative",
AND(Tablename[Final] = "Positive", Tablename[Type] = "ABCD"), Tablename[Numeric_Result],
BLANK() // If none of the conditions are met, return blank
)

 

This DAX expression follows your specified rules:

  • If Final is "Negative", the result is "Negative".
  • If Final is "Positive" and Type is "ABCD", the result is the value in the Numeric_Result column.
  • If none of the conditions are met, it returns blank.

Make sure to replace "Tablename" with the actual name of your table in Power BI. This expression should work in creating the calculated column you desire.

 

If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.

 

In case there is still a problem, please feel free and explain your issue in detail, It will be my pleasure to assist you in any way I can.

KT9
Regular Visitor

Hi @123abc ,

Thank you so much for your response and the clear clarrification. I tried this DAX formula. But it throws an error says that "Function 'switch' does not support comparing values of type True/False with values of type Text. Consider using the VALUE or FORMAT funtion to convert one of the values."

  The Number_Result column contains the values like

0.000

1.099

-0.999

67.777

Jihwan_Kim
Super User
Super User

Hi,

Please try something like below if it suits your requirement.

 

Result CC =
SWITCH (
    TRUE (),
    Tablename[Final] = "Negative", "Negative",
    Tablename[Final] = "Positive"
        && Tablename[Type] = "ABCD", FORMAT ( MAX ( Tablename[Numeric_Result] ), "#,#0.00" )
)

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

Hi @Jihwan_Kim ,

Thank you so much for your response.  sorry i didn't communicate well. In Row 2 I am trying to write the code code without MAX function( to return largest no of value). But it is throwing the error ( Function "FORMAT" is not allowed as part of calculated column DAX expressions on DirectQuery models.) Please help.Thank you!

 

My exactl rule for the calculated column is ( using SQL server as a database)

1. If Final = Negative, then Negative

2. If Final = positive AND type = ABCD, then display the associated value in numeric_result (numeric_result is column name in sqlserver with decimal datatype values like 0.000 , -0.999)

3. IF Final = positive AND type = XYZ, then Positive

4. If Final = In Range, then In Range

 

amitchandak
Super User
Super User

@KT9 , seem like Numeric_Result a number data type. You can not have two datatype in the same column. I added additional code to convert it to a string

 

Result =
SWITCH(TRUE(),
Tablename[Final] = "Negative", "Negative",
Tablename[Final] = "Positive" && Tablename[Type] = "ABCD", max(Tablename[Numeric_Result]) & "" )

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.