简体   繁体   中英

Show rows only if previous measure member meets certain condition

Consider certain query in MDX

SELECT NON EMPTY {[Measures].[Income]} ON COLUMNS,
{
            SellDate.[year].Members
} ON ROWS
FROM [HD Kawiarnia]

that shows income of a company during each year. The question is how to show only this years in which income was higher then it was in the previous year?

WITH MEMBER [Measures].[MyMeasure] as 
IIF(
([Measures].[Income], [SellDate].[Year].currentmember) 
     <= ([Measures].[Income], [SellDate].[Year].CURRENTMEMBER.PREVMEMBER) 
, NULL, 
[Measures].[Income])

SELECT NON EMPTY {[Measures].[Income]} on COLUMNS
NON EMPTY {[SellDate].[Year].CHILDREN} on ROWS
FROM 
[HD Kawiarnia]

I used the IIF function to to evaluate the clause and return null if the previous year income is higher. I got the previous year by using the PREVMEMBER function. I also replaced your [SellDate].[Year].MEMBERS with [SellDate].[Year].CHILDREN. This removes the ALL member, which you probably didn't want to be returned.

Word of caution: although Iif is the simplest way to understand how to do this, it traditionally is a bit slow (although performance has improved with 2008 and 2012 versions of SQL Server). This one probably won't be too bad, but in general you can replace iif functions with scripting using scope to get better performance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM