Search code examples
sql-serverssasmdx

How to add filter (per week) in the sum for distinct value in this query


I found the following sample from internet, it calculates sum [Measures].[Job Advert Value] for distinct [JAP ID].[JAP ID].

However, it only shows total sum for everything, and I would like to sum it only for each week.

I tried to add FILTER for [Time].[Year - Week - Date].[Week].members that is Time dimension, but does not work.

Below query works without FILTER function.

SCOPE([Time].[Year - Week - Date].[Week].members, [Permanent Placement Value].[Value Type Name].&[Vacancies], [Measures].[Job Advert Value] );

    [Measures].[JAP Advert Value] = 
 SUM 
    (
            generate
            (
                [JAP ID].[JAP ID].MEMBERS AS s,
                s.CURRENT *
                TAIL(
                    NonEmpty
                        (
                         [Time].[Year - Week - Date].[Date].members, 
                         (s.CURRENT, [Measures].[Job Advert Value])
                        )
                    )
            )
        , 
        Measures.[Job Advert Value]
    );

END SCOPE; 

Solution

  • I'd suggest trying one of the following:

    SCOPE([Time].[Year - Week - Date].[Week].members
        , [Permanent Placement Value].[Value Type Name].&[Vacancies]
        , [Measures].[Job Advert Value] );
    
    [Measures].[JAP Advert Value] = 
    SUM(
      GENERATE(
        [JAP ID].[JAP ID].MEMBERS AS s
       ,s.CURRENT *
        TAIL(
         NONEMPTY(
           EXISTING [Time].[Year - Week - Date].[Date].MEMBERS
           ,(
             s.CURRENT
            ,[Measures].[Job Advert Value]
            )
          )
        )
      )
    ,Measures.[Job Advert Value]
    );
    
    END SCOPE;
    

    or

    SCOPE([Time].[Year - Week - Date].[Week].members
        , [Permanent Placement Value].[Value Type Name].&[Vacancies]
        , [Measures].[Job Advert Value] );
    
    [Measures].[JAP Advert Value] = 
    SUM(
      GENERATE(
        [JAP ID].[JAP ID].MEMBERS AS s
       ,s.CURRENT *
        TAIL(
          NONEMPTY(
           [Time].[Year - Week - Date].CURRENTMEMBER
           ,(
             s.CURRENT
            ,[Measures].[Job Advert Value]
            )
          )
        )
      )
    ,Measures.[Job Advert Value]
    );
    
    END SCOPE;
    

    Also I'd suggest experimenting with your scope with the following:

    SCOPE([Permanent Placement Value].[Value Type Name].&[Vacancies]
    , [Measures].[Job Advert Value] );