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

Can I switch the Date Format of a field based on a logic, if date is within a month, then as Quarter

How can I switch Date Format for a field in Paginated report based on a logic.

The logic would be as follows:

  • (Finish Date – Current Date) > 3 Months – Display Finish Date as a Quarter (i.e. Q1.2022)
  • (Finish Date – Current Date) <= 3 Months and (Finish Date – Current Date) > 1 Month – Display Finish Date as Month (i.e. February, 2022)
  • (Finish Date – Current Date) <= 1 Month – Display Finish Date as Date (i.e. 2/15/2022)

My current expression for that field is as below-

=FormatDateTime(Fields!Finish_Date.Value, Microsoft.VisualBasic.DateFormat.ShortDate)

 

Thank you in advance.

1 ACCEPTED SOLUTION

Hi @Anonymous ,

Only one data type can exist in the same column, and the calculated column [Column] has data of both Date type ([Finish Date]) and Text type ( FORMAT ( [Finish Date], "MMMM, YYYY" ) and FORMAT ( [Finish Date], "QQ-YYYY") ), so it reported variant data-type error... You can change its formula to the following one:

DAX Fridays! #148: CONVERT and resolve variant data-type error

Column =
VAR _1 =
DATEDIFF (
TODAY (),
'Consolidated In Progress Status Report'[Finish Date],
MONTH
)
RETURN
SWITCH (
TRUE (),
_1 < 1, FORMAT ( [Finish Date], "MM/DD/YYYY" ),
_1 <= 3, FORMAT ( [Finish Date], "MMMM, YYYY" ),
_1 > 3, FORMAT ( [Finish Date], "QQ-YYYY" )
)

yingyinr_0-1632735416762.png

Best Regards

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

View solution in original post

10 REPLIES 10
HotChilli
Super User
Super User

The comma at the end requires a default value afterwards OR delete the comma if a default case is not required

Anonymous
Not applicable

Thank you for catching that. I made the change and now I am getting the "Expressions that yield variant data-type cannot be used to define calculated columns" error. 

 

bverma_0-1632415176501.png

 

I am not sure if you can use calculated column to solve my original problem. Maybe, I need to do something within the Paginated Report (Finish Date Query) to achieve this.

Hi @Anonymous ,

Only one data type can exist in the same column, and the calculated column [Column] has data of both Date type ([Finish Date]) and Text type ( FORMAT ( [Finish Date], "MMMM, YYYY" ) and FORMAT ( [Finish Date], "QQ-YYYY") ), so it reported variant data-type error... You can change its formula to the following one:

DAX Fridays! #148: CONVERT and resolve variant data-type error

Column =
VAR _1 =
DATEDIFF (
TODAY (),
'Consolidated In Progress Status Report'[Finish Date],
MONTH
)
RETURN
SWITCH (
TRUE (),
_1 < 1, FORMAT ( [Finish Date], "MM/DD/YYYY" ),
_1 <= 3, FORMAT ( [Finish Date], "MMMM, YYYY" ),
_1 > 3, FORMAT ( [Finish Date], "QQ-YYYY" )
)

yingyinr_0-1632735416762.png

Best Regards

Community Support Team _ Rena
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

Yes, there is an error in the code given to you.  I have faith in you to make the fix.  Start at the beginning of the squiggly red line.

Anonymous
Not applicable

I think I am very close. I just have one final error on the Switch syntax (that I need to figure out)

bverma_0-1632338095019.png

 

Column =
var _1 = datediff(TODAY(), 'Consolidated In Progress Status Report'[Finish Date] , Month)
return
Switch( True() ,
_1< 1, [Finish Date],
_1< 3, format([Finish Date], "MMMM, YYYY"),
format([Finish Date], "QQ-YYYY"),
)
HotChilli
Super User
Super User

You've copied the code verbatim but you need to create the column (with a name) and then put the code in.  

-1 is a variable which should be declared within the column.  That's why you're getting syntax errors.

Anonymous
Not applicable

@HotChilli Thank you for taking a look. I made some change (per your comment). I am getting "Too many arguments were passed" error now.

 

2021-09-22 14_21_58-StatusReport - Power BI Desktop.png

 

My update code is"

Column =
var _1 = datediff(TODAY(), 'Consolidated In Progress Status Report'[Finish Date] , Month)
return
Switch( True() ,
_1< 1, [Finish Date],
_1< 3, format([Finish Date], "MMMM, YYYY",
format([Finish Date], "QQ-YYYY"
)))
amitchandak
Super User
Super User

@Anonymous , a new column

var _1 = datediff(Current Date, Finish Date , Month)

return

Switch( True() ,

_1< 1, [Finish Date],

_1 <3, format([Finish Date], "MMMM, YYYY",

format([Finish Date], "QQ-YYYY"

)

Anonymous
Not applicable

@amitchandak I am still getting the "Arugment '8' in SWITCH fucntion is required" error, is there in the DAX code below-

 

Column =
var _1 = datediff(TODAY(), 'Consolidated In Progress Status Report'[Finish Date] , Month)
return
Switch(True() ,
_1< 1, [Finish Date],
_1<= 3, format([Finish Date], "MMMM, YYYY"),
_1> 3, format([Finish Date], "QQ-YYYY"),
)

 

bverma_0-1632402267538.png

 

Thank you for your time.

Anonymous
Not applicable

@amitchandak  Thank you for your quick response. So, I created a new calculated column on the same table (where the Finish date exists). I replaced the Current Date to TODAY (). But I am getting the following error (not sure what I am doing wrong)-

 

My updated query is-

 

var _1 = datediff(TODAY(), 'Consolidated In Progress Status Report'[Finish Date], month)
return
Switch( True() ,
_1< 1, [Finish Date],
_1 <3, format([Finish Date], "MMMM, YYYY",
format([Finish Date], "QQ-YYYY"
)

 

2021-09-22 12_37_16-StatusReport - Power BI Desktop.png

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.