【问题标题】:Changing a date format to a shorter date将日期格式更改为较短的日期
【发布时间】:2014-04-04 00:06:00
【问题描述】:

我们有以下脚本:

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]

它返回这个:

我能否以某种方式更改日期格式,使日期类似于“2014 年 2 月 9 日”,即更短?

【问题讨论】:

  • 我猜了一下:也许CURRENTMEMBER.member_value 已经是处理维度时创建的字符串,因此格式化根本不会改变它。您可以尝试使用CurrentMember.Propertities('key0', TYPED) 看看格式化是否有效果。
  • @FrankPl 我喜欢你的新关键字Propertities
  • 使用类型键属性的方法有帮助吗?
  • 它们现在回到 DateKey 格式 - 我们在仓库中使用整数,例如20140403

标签: ssas mdx


【解决方案1】:

从 cmets 中,我认为问题在于您有整数日期键和一个名称列,该列在 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

看我上面代码的复杂度,使用字符串逻辑而不是整数逻辑可能会更好,即。 e.将typed 参数省略到Properties,然后使用LeftMidRight 计算年、月和日,并将其转换为整数。但是“其中的细节留给读者作为练习”。

【讨论】:

    【解决方案2】:

    尝试以下格式之一:

     =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"
    

    【讨论】:

    • 查看 Bob 在该源链接底部的评论 - 我同意这是 SSRS 中的表达式解决方案,而不是 MDX 解决方案...或者您可以针对自己的环境进行测试并给出完整的脚本答案?
    • 对不起,我无法测试,因为我没有分析服务了
    • 我将在 OP 中添加另一个度量
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-04
    • 2022-11-30
    • 2023-02-03
    • 1970-01-01
    相关资源
    最近更新 更多