Saturday, May 25, 2024

How to format Date Time values using FORMAT function in Power BI DAX

How to format Date and Time strings using FORMAT function in Power BI DAX
The FORMAT is a formatting function in DAX that formats a value based on a format string.
FormatDateTime=
VAR _DateVal  = DATE ( 2023, 3, 1 )
VAR _TimeVal  = TIME ( 15, 30, 5 )
VAR _DateTimeVal = (_DateVal + _TimeVal)

/* Formatting Date and Time Values */
VAR _DateValue1 = ROW ( "Value_to_Format", _DateTimeVal ,
                "Format_String","dd/mm/yyyy",
                "Formatted_Value",FORMAT (_DateTimeVal, "dd/mm/yyyy" ) )
VAR _DateTimeVal1 = ROW ( "Value_to_Format", _DateTimeVal ,
                "Format_String","mm/dd/yyyy hh:mm:ss AM/PM",
                "Formatted_Value",FORMAT (_DateTimeVal, "mm/dd/yyyy hh:mm:ss AM/PM" ) )
VAR _GeneralDate = ROW ( "Value_to_Format", _DateTimeVal ,
                "Format_String","General Date",
                "Formatted_Value",FORMAT (_DateTimeVal, "General Date" ) )
VAR _LongDate = ROW ( "Value_to_Format", _DateTimeVal ,"Format_String","Long Date",
                "Formatted_Value",FORMAT (_DateTimeVal, "Long Date" ) )
VAR _MedDate = ROW ( "Value_to_Format", _DateTimeVal ,"Format_String","Medium Date",
                "Formatted_Value",FORMAT (_DateTimeVal, "Medium Date" ) )
VAR _ShortDate = ROW ( "Value_to_Format", _DateTimeVal ,"Format_String","Short Date",
                "Formatted_Value",FORMAT (_DateTimeVal, "Short Date" ) )
VAR _US_DateFormat1 = ROW ( "Value_to_Format", _DateTimeVal ,"Format_String","en-US",
                "Formatted_Value",FORMAT (_DateTimeVal, BLANK(), "en-US" ) )
VAR _US_DateFormat2 = ROW ( "Value_to_Format", _DateTimeVal ,"Format_String","en-US",
                "Formatted_Value",FORMAT (_DateTimeVal,"mm/dd/yyyy", "en-US" ) )
VAR _UK_DateFormat1 = ROW ( "Value_to_Format", _DateTimeVal ,"Format_String","en-GB",
                "Formatted_Value",FORMAT (_DateTimeVal, BLANK(), "en-GB" ) )              
VAR _UK_DateFormat2 = ROW ( "Value_to_Format", _DateTimeVal ,"Format_String","en-GB",
                "Formatted_Value",FORMAT (_DateTimeVal, "dd/mm/yyyy", "en-GB" ) )
VAR _TimeValue1 = ROW ( "Value_to_Format", _DateTimeVal ,
                "Format_String","hh:mm:ss AM/PM",
                "Formatted_Value",FORMAT (_DateTimeVal, "hh:mm:ss AM/PM" ) )
VAR _TimeValue2 = ROW ( "Value_to_Format", _DateTimeVal ,"Format_String","hh:mm:ss",
                "Formatted_Value",FORMAT (_DateTimeVal, "hh:mm:ss" ) )
VAR _TimeValue3 = ROW ( "Value_to_Format", _DateTimeVal ,"Format_String","hh:nn:ss",
                "Formatted_Value",FORMAT (_DateTimeVal, "hh:nn:ss" ) )
VAR _LongTime = ROW ( "Value_to_Format", _DateTimeVal ,"Format_String","Long Time",
                "Formatted_Value",FORMAT (_DateTimeVal, "Long Time" ) )
VAR _MedTime = ROW ( "Value_to_Format", _DateTimeVal ,"Format_String","Medium Time",
                "Formatted_Value",FORMAT (_DateTimeVal, "Medium Time" ) )
VAR _ShortTime = ROW ( "Value_to_Format", _DateTimeVal ,"Format_String","Short Time",
                "Formatted_Value",FORMAT (_DateTimeVal, "Short Time" ) )

/* Format Day */
VAR _DayFormat1 = ROW ( "Value_to_Format", _DateTimeVal ,"Format_String","d",
                "Formatted_Value",FORMAT (_DateTimeVal, "d" ) )
VAR _DayFormat2 = ROW ( "Value_to_Format", _DateTimeVal ,"Format_String","dd",
                "Formatted_Value",FORMAT (_DateTimeVal, "dd" ) )
VAR _DayFormat3 = ROW ( "Value_to_Format", _DateTimeVal ,"Format_String","ddd",
                "Formatted_Value",FORMAT (_DateTimeVal, "ddd" ) )
VAR _DayFormat4 = ROW ( "Value_to_Format", _DateTimeVal ,"Format_String","dddd",
                "Formatted_Value",FORMAT (_DateTimeVal, "dddd" ) )
VAR _DayFormat5 = ROW ( "Value_to_Format", _DateTimeVal ,"Format_String","ddddd",
                "Formatted_Value",FORMAT (_DateTimeVal, "ddddd" ) )
VAR _DayFormat6 = ROW ( "Value_to_Format", _DateTimeVal ,"Format_String","dddddd",
                "Formatted_Value",FORMAT (_DateTimeVal, "dddddd" ) )

/* Format Month */
VAR _MonthFormat1 = ROW ( "Value_to_Format", _DateTimeVal ,"Format_String","m",
                "Formatted_Value",FORMAT (_DateTimeVal, "m" ) )
VAR _MonthFormat2 = ROW ( "Value_to_Format", _DateTimeVal ,"Format_String","mm",
                "Formatted_Value",FORMAT (_DateTimeVal, "mm" ) )
VAR _MonthFormat3 = ROW ( "Value_to_Format", _DateTimeVal ,"Format_String","mmm",
                "Formatted_Value",FORMAT (_DateTimeVal, "mmm" ) )
VAR _MonthFormat4 = ROW ( "Value_to_Format", _DateTimeVal ,"Format_String","mmmm",
                "Formatted_Value",FORMAT (_DateTimeVal, "mmmm" ) )

RETURN

UNION (
    _DateTimeVal1, _DateValue1,_GeneralDate,_LongDate,_MedDate,_ShortDate,
    _US_DateFormat1,_US_DateFormat2,_UK_DateFormat1,_UK_DateFormat2,
    _TimeValue1,_TimeValue2,_TimeValue3,_LongTime,_MedTime,_ShortTime,
    _DayFormat1,_DayFormat2,_DayFormat3,_DayFormat4,_DayFormat5,_DayFormat6,
    _MonthFormat1,_MonthFormat2,_MonthFormat3,_MonthFormat4
    ) Result:

--------------------------------------------------------------------------------------------------------
Thanks, TAMATAM ; Business Intelligence & Analytics Professional
--------------------------------------------------------------------------------------------------------

No comments:

Post a Comment

Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.

Featured Post from this Blog

How to compare Current Snapshot Data with Previous Snapshot in Power BI

How to Dynamically compare two Snapshots Data in Power BI Scenario: Suppose, we have a sample Sales data, which is stored with Monthly Snaps...

Popular Posts from this Blog