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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
nerdyplayer
Helper I
Helper I

need help with a switch function with a null value

Always having problems with blank value. 

i am trying to do a switch function, if the date value is blank "--" 

if not, formula will do a datediff between 2 dates.

 

testing = switch(
TRUE,
       ISBLANK(Policy[Final Concurrence Date]),"--",
        NETWORKDAYS(Policy[Date_Assigned],Policy[Final Concurrence Date],1)
)

 

 

formula is calculating the datediff correctly, but having a hard time if the date is blank/null.

tried IF/else as well, both gave me this error

 

expressions that yield variant data-type cannot be used to define calculated columns

Those who are smarter than I please help. ty

1 ACCEPTED SOLUTION
Rakesh1705
Resolver III
Resolver III

Hi @nerdyplayer 
the main issue is confliction of data type within the same column.
Hence to avoid this confliction I have converted the final value in string.
Please check the below snap

Rakesh1705_0-1722326483758.png

If this resolves your issue then please accept the same as your solution.



View solution in original post

4 REPLIES 4
nerdyplayer
Helper I
Helper I

thank you all. it was indeed the conflicted between the dash. I did need for the dashes for another column and it was useful for me. thank you all again.

Rakesh1705
Resolver III
Resolver III

Hi @nerdyplayer 
the main issue is confliction of data type within the same column.
Hence to avoid this confliction I have converted the final value in string.
Please check the below snap

Rakesh1705_0-1722326483758.png

If this resolves your issue then please accept the same as your solution.



v-yilong-msft
Community Support
Community Support

Hi @nerdyplayer ,

I create a table as you mentioned.

vyilongmsft_0-1722318721939.png

When I use your DAX code in a calculated column, it gives me the same error.

vyilongmsft_1-1722318957759.png

The error you’re encountering is due to the fact that calculated columns in DAX cannot return different data types. In your case, the SWITCH function is trying to return both a string (“–”) and a number (result of NETWORKDAYS), which is causing the issue.

If you still want to use calculated column, you can use this DAX code.

Column = 
SWITCH (
    TRUE,
    ISBLANK ( Policy[Final Concurrence Date] ), 0,
    NETWORKDAYS ( Policy[Date_Assigned], Policy[Final Concurrence Date], 1 )
)

vyilongmsft_2-1722319190677.png

If you want to still use "--", I think you can use Measure, here is the DAX code.

testing = 
SWITCH (
    TRUE,
    ISBLANK ( MAX('Policy'[Final Concurrence Date]) ), "--",
    NETWORKDAYS ( MAX('Policy'[Date_Assigned]), MAX('Policy'[Final Concurrence Date]), 1 )
)

vyilongmsft_3-1722319273992.png

 

 

 

Best Regards

Yilong Zhou

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

HotChilli
Super User
Super User

networkdays returns an integer, the other path returns a text string.  You can't have those options in a SWITCH/IF. You have to return the same type from all paths

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

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! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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