I recently published an article on MSSQLTips.com titled Format Durations in SQL Server Analysis Services. For those who haven’t read it I’ll summarize it quickly: it explains how to create an MDX formula that formats durations into an [hh]:mm:ss format, where [hh] can go over 24 hours. For more detailed information, please check out the article itself.

An example:

FormattedDurations

However, I recently found out at a project that the formula doesn’t really deal well with null values. For instance, when I browse the sample data I get the following result:

NullProblem

The returned data set contains (null) values for the Unknown member, although empty cells should not be included. If I browse the same set with the unformatted measure, the empty cells are not returned so it the issue is caused with the formatting formula.

Unformatted

My educated guess is that SSAS doesn’t know upfront if the result of the formula will be null or not, so it calculated the measure anyway and displays everything on the grid. The solution is to add an extra check for null values into the formula.

vb.net
1
2
3
4
5
6
7
8
9
CREATE MEMBER CURRENTCUBE.[Measures].[DurationFormatted]
 AS   Iif(IsEmpty([Measures].[Duration])
        ,null
        ,   Cstr((Int([Measures].[Duration]) * 24)
        +   CInt(FORMAT(CDate([Measures].[Duration]), "HH")) )
        +   FORMAT(CDate([Measures].[Duration]), ":mm:ss")
        )
,VISIBLE = 1
,ASSOCIATED_MEASURE_GROUP = 'Customer Service';
CREATE MEMBER CURRENTCUBE.[Measures].[DurationFormatted]
 AS   Iif(IsEmpty([Measures].[Duration])
        ,null
        ,   Cstr((Int([Measures].[Duration]) * 24)
        +   CInt(FORMAT(CDate([Measures].[Duration]), "HH")) )
        +   FORMAT(CDate([Measures].[Duration]), ":mm:ss")
        )
,VISIBLE = 1
,ASSOCIATED_MEASURE_GROUP = 'Customer Service';

When browsing the cube now, the Unknown member is now ignored due to the explicit check for null values.

IssueFixed