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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

switch

Hello,
I would like to add a column to an existing table in the model but the error is below. Any suggestions please?:

 

Function 'SWITCH' does not support comparing values of type Text with values of type Integer. Consider using the VALUE or FORMAT function to convert one of the values.

note that [CalendarQuarterKey] is int and holds values such as:
20153
20154
...
20201
...

StartMonthDayKey =
SWITCH(
'myDate'[CalendarQuarterKey],
1, LEFT('myDate'[CalendarQuarterKey], 4) & "0105"),
2, LEFT('myDate'[CalendarQuarterKey], 4) & "0405"),
3, LEFT('Date'[CalendarQuarterKey], 4) & "0705"),
4, LEFT('myDate'[CalendarQuarterKey], 4) & "1005")
)

1 ACCEPTED SOLUTION
az38
Community Champion
Community Champion

Hi @Anonymous 

try CONVERT()

StartMonthDayKey =
var _CalendarQuarterKey = LEFT(CONVERT('myDate'[CalendarQuarterKey], STRING), 4)

RETURN 

SWITCH(
'myDate'[CalendarQuarterKey],
1, _CalendarQuarterKey  & "0105"),
2, _CalendarQuarterKey  & "0405"),
3, _CalendarQuarterKey  & "0705"),
4, _CalendarQuarterKey  & "1005")
)

do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn

View solution in original post

6 REPLIES 6
danextian
Super User
Super User

Hi @Anonymous,

 

Text functions do not work on numbers. To be able to use a number as a text it has to be converted first to text using FORMAT function. For example:

Text to Number =
FORMAT ( 'Table'[NumberField], "#" )

 

In your case, you can store the text version of myDate'[CalendarQuarterKey] in a variable and use that in SWITCH instead. You also need to remove the extra closing parenthesis after each "01##" as they will cause a syntax error.

StartMonthDayKey =
VAR TextToNumber =
    FORMAT ( 'myDate'[CalendarQuarterKey], "#" )
RETURN
    SWITCH (
        'myDate'[CalendarQuarterKey],
        1, LEFT ( TextToNumber, 4 ) & "0105",
        2, LEFT ( TextToNumber, 4 ) & "0405",
        3, LEFT ( TextToNumber, 4 ) & "0705",
        4, LEFT ( TextToNumber, 4 ) & "1005"
    )

 Also





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.
Anonymous
Not applicable

 
Anonymous
Not applicable

For the following DAX, the error is:
Function 'SWITCH' does not support comparing values of type Text with values of type Integer. Consider using the VALUE or FORMAT function to convert one of the values.

StartQuarterYearMonthDayKey =
VAR TextToNumber =
FORMAT ( left('Date'[Calendar Quarter Key], 4), "#" )
RETURN
SWITCH (
'Date'[CalendarQuarterKeyValue],
1, LEFT ( TextToNumber, 4 ) & "0105",
2, LEFT ( TextToNumber, 4 ) & "0405",
3, LEFT ( TextToNumber, 4 ) & "0705",
4, LEFT ( TextToNumber, 4 ) & "1005"
)

az38
Community Champion
Community Champion

Hi @Anonymous 

try CONVERT()

StartMonthDayKey =
var _CalendarQuarterKey = LEFT(CONVERT('myDate'[CalendarQuarterKey], STRING), 4)

RETURN 

SWITCH(
'myDate'[CalendarQuarterKey],
1, _CalendarQuarterKey  & "0105"),
2, _CalendarQuarterKey  & "0405"),
3, _CalendarQuarterKey  & "0705"),
4, _CalendarQuarterKey  & "1005")
)

do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn
Anonymous
Not applicable

, still getting error:

The syntax for ',' is incorrect. (DAX(var _CalendarQuarterKey = LEFT(CONVERT('Date'[Calendar Quarter Key], STRING), 4)RETURN SWITCH('Date'[CalendarQuarterKeyValue],1, _CalendarQuarterKey & "0105"),2, _CalendarQuarterKey & "0405"),3, _CalendarQuarterKey & "0705"),4, _CalendarQuarterKey & "1005")))).

az38
Community Champion
Community Champion

@Anonymous 

sorry, wrong parenthesys

StartMonthDayKey =
var _CalendarQuarterKey = LEFT(CONVERT('myDate'[CalendarQuarterKey], STRING), 4)

RETURN 

SWITCH(
'myDate'[CalendarQuarterKey],
1, _CalendarQuarterKey  & "0105",
2, _CalendarQuarterKey  & "0405",
3, _CalendarQuarterKey  & "0705",
4, _CalendarQuarterKey  & "1005"
)

do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.