Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / SQL
Technical Blog

Random Notes on MDX Filtering

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
9 May 2013Apache 6.3K   1  
Random notes on MDX filtering

1. Filter() and IIF work funny in the columns hierarchy.

Consider this:

SQL
SELECT
FILTER([MarketValue], [MarketValue] >1000) ON COLUMNS,
[Region].[Region].[Region] ON ROWS
FROM [MyCube]

In this case, the MarketValue in italics actually means the global number for MarketValue, not the per region one, i.e., if the global MarketValue is 1024, then all regions will be returned. If global market value is 999, none of the regions will be returned. IIF works in a similar way, and so does IsEmpty. Thus,

SQL
SELECT NON EMPTY [MarketValue]

will return regions where market value is not empty, whereas:

SQL
SELECT FILTER([MarketValue], NOT IsEmpty([MarketValue]))

will return market value for all regions, empty or not, provided that the global market value is not empty.

To avoid this shenanigans, one should filter on ROWS. E.g.

SQL
SELECT
[MarketValue] ON COLUMNS,
FILTER( [Region].[Region].[Region], [MarketValue] > 1000) ON ROWS
FROM [MyCube]

2. Performance of FILTER on even medium sized sets is not so good.

Even when the filter is trivial, something like 1=1, the query still takes several times longer than without the filter. If you filter on multiple fields, it makes the situation even worse.

If you rewrite the query above like this...

SQL
WTIH MEMBER [BigMarketValue] AS IIF([MarketValue]>1000, [MarketValue], NULL)
SELECT NON EMPTY [BigMarketValue] ON COLUMNS,
[Region].[Region].[Region] ON ROWS
FROM [MyCube]

...you might get much better performance.

This article was originally posted at http://www.ikriv.com/blog?p=1277

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


Written By
Technical Lead Thomson Reuters
United States United States
Ivan is a hands-on software architect/technical lead working for Thomson Reuters in the New York City area. At present I am mostly building complex multi-threaded WPF application for the financial sector, but I am also interested in cloud computing, web development, mobile development, etc.

Please visit my web site: www.ikriv.com.

Comments and Discussions

 
-- There are no messages in this forum --