将日期格式更改为更短的日期

时间:2014-04-02 14:56:01

标签: ssas mdx

我们有以下脚本:

WITH 
SET [Last56Days] as
 TAIL ( [Date].[Date - Calendar Month].[Calendar Day].members, 56 )
MEMBER [Measures].[DateValue] as
 [Date].[Date - Calendar Month].CURRENTMEMBER.member_value, format_string = "short date"
MEMBER [Measures].[DateValue2] as
 [Date].[Date - Calendar Month].CURRENTMEMBER.member_value, format_string = "dd/mm/yyyy"
SELECT
    { [Measures].[DateValue], [Measures].[DateValue2]} ON COLUMNS,
    Hierarchize ({ [Last56Days] } ) ON ROWS
FROM [Our Cube]

它返回:

enter image description here

我能否以某种方式更改日期格式,以便日期如此“2014年2月9日”,即更短?

2 个答案:

答案 0 :(得分:2)

从评论中我想问题是你有整数日期键和一个名称列,它在Analysis Services中始终是字符串类型。这些不适用于需要日期的日期格式(或包含自1900年1月1日以来的日期的双打,以及作为时间的分数,即上午8:30将是8.5 / 24)。

因此,您可以使用

MEMBER [Measures].[Date as int] as
       [Date].[Date - Calendar Month].CURRENTMEMBER.Properties('Key0', Typed)
MEMBER [Measures].[Date Year] as
       Fix([Measures].[Date as int] / 10000)
MEMBER [Measures].[Date Month] as
       Fix(([Measures].[Date as int] - [Measures].[Date Year] * 10000) / 100)
MEMBER [Measures].[Date Day] as
       [Measures].[Date as int] - [Measures].[Date Year] * 10000 - [Measures].[Date Month] * 100
MEMBER [Measures].[DateValue] as
       // convert it to Date data type and use a format string on that:
       DateSerial([Measures].[Date Year], [Measures].[Date Month], [Measures].[Date Day]),
       format_string = 'dd/mm/yyyy'

请参阅the documentation of Propertieslist of VBA functions like Fix and DateSerial

考虑到上面代码的复杂性,使用字符串逻辑而不是整数逻辑可能会更好,i。即将typed参数省略到Properties,然后使用LeftMidRight计算年,月和日,并将其转换为整数。但是"其细节留给了读者一个练习。

答案 1 :(得分:0)

尝试使用以下格式之一:

 =Format(Fields!myDateTime.Value, "M/d/yy") ... 6/15/09 
 =Format(Fields!myDateTime.Value, "M/d/yyyy h:mmtt") ... 6/15/2009 2:45PM 
 =Format(Fields!myDateTime.Value, "MM/dd/yy HH:mm") ... 06/15/09 14:45 
 =Format(Fields!myDateTime.Value, "MMM d, yyyy") ... Jun 15, 2009 
 =Format(Fields!myDateTime.Value, "Short Date") ... 6/15/2009 
 =Format(Fields!myDateTime.Value, "Long Date") ... Monday, June 15, 2009 
 =Format(Fields!myDateTime.Value, "ddd dd/MM/yyyy") ... Mon 04/04/2011 

Source Link。希望它有所帮助

修改 对不起!我最后没有看到评论。无论如何,我猜你已经看过这个Link,但你也许可以尝试使用 format_string

format_string = "dd/mmm/yyyy"