
{"id":1277,"date":"2013-05-09T11:26:50","date_gmt":"2013-05-09T15:26:50","guid":{"rendered":"http:\/\/www.ikriv.com\/blog\/?p=1277"},"modified":"2013-05-09T11:26:50","modified_gmt":"2013-05-09T15:26:50","slug":"random-notes-on-mdx-filtering","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=1277","title":{"rendered":"Random Notes on MDX Filtering"},"content":{"rendered":"<p><b>1. Filter() and IIF work funny in the columns hierarchy.<\/b><br \/>\nConsider this:<\/p>\n<p><code>SELECT<br \/>\nFILTER([MarketValue], <font color=\"red\"><b>[MarketValue]<\/b><\/font> >1000) ON COLUMNS,<br \/>\n[Region].[Region].[Region] ON ROWS<br \/>\nFROM [MyCube]<\/code><\/p>\n<p>In this case the <font color=\"red\"><b><code>MarketValue<\/code><\/b><\/font> in red actually means the <b>global<\/b> number for MarketValue, not the per region one. I.e., if the global <code>MarketValue<\/code> is 1024, then <i>all<\/i> regions will be returned. If global market value is 999, none of the regions will be returned. <code>IIF<\/code> works in a similar way, and so does <code>IsEmpty<\/code>. Thus,<\/p>\n<p><code>SELECT NON EMPTY [MarketValue]<\/code><\/p>\n<p>will return regions where market value is not empty, whereas<\/p>\n<p><code>SELECT FILTER([MarketValue], NOT IsEmpty([MarketValue]))<\/code><\/p>\n<p>will return market value for all regions, empty or not, provided that the global market value is not empty.<\/p>\n<p>To avoid this shenanigans one should filter on ROWS. E.g.<\/p>\n<p><code>SELECT<br \/>\n[MarketValue] ON COLUMNS,<br \/>\nFILTER( [Region].[Region].[Region], [MarketValue] > 1000) ON ROWS<br \/>\nFROM [MyCube]<\/code><\/p>\n<p><b>2. Performance of FILTER on even medium sized sets is not so good.<\/b><br \/>\nEven 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 situation even worse.<\/p>\n<p>If you rewrite the query above like this<\/p>\n<p><code>WTIH MEMBER [BigMarketValue] AS IIF([MarketValue]>1000, [MarketValue], NULL)<br \/>\nSELECT NON EMPTY [BigMarketValue] ON COLUMNS,<br \/>\n[Region].[Region].[Region] ON ROWS<br \/>\nFROM [MyCube]<\/code><\/p>\n<p>you might get much better performance.<a style=\"display:none\" href=\"http:\/\/www.codeproject.com\/script\/Articles\/BlogFeedList.aspx?amid=1181663\" rel=\"tag\">CodeProject<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Filter() and IIF work funny in the columns hierarchy. Consider this: SELECT FILTER([MarketValue], [MarketValue] >1000) ON COLUMNS, [Region].[Region].[Region] ON ROWS FROM [MyCube] In this case the MarketValue in red <a href=\"https:\/\/ikriv.com\/blog\/?p=1277\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[14],"tags":[],"class_list":["entry","author-ikriv","post-1277","post","type-post","status-publish","format-standard","category-db"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1277","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1277"}],"version-history":[{"count":0,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1277\/revisions"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}