Forum Discussion

Amrselim1989's avatar
Amrselim1989
Icon for Helper II rankHelper II
3 years ago
Solved

Allselected with filtration

hello Guys, i hope you can help me with this,   i have this code VAR MaxSales = MAXX ( ALLSELECTED ( dimDate[Year], dimDate[Month], DimDate[MMM] ), [Sum of Net Sales Value] ) VAR MinSales = MIN...
  • Martin_D's avatar
    3 years ago

    Hi Amrselim1989 ,

    I have two options in my mind to solve this. 

    #1 You could in you measure get the latest date with sales data, derive year and month from this date, and filter your data table for including only months before that current month.
    # 2 You can add a column to your dimDate table that marks current and previous months and then in the measure you can use this column to only include previous months. This is what I'm gonna show you.

     

    Add this as a calculated column to you dimDate Table:

     

    Running Month = DATEDIFF ( TODAY(), 'dimDate'[Date], MONTH )

     

    This will give you a column that contains 0 for the current month, -1, -2, -3, ... for past months and 1, 2, 3, ... for future month. Instead of using TODAY() in the code, you can alternatively relate to your last date with sales as today if there is some delay in your data processing pipeline until sales data actually arrives in your report.
    Then wrap you code into a CALCULATE() function that exludes the current (and future) months:

     

    CALCULATE (
    
        VAR MaxSales = MAXX ( ALLSELECTED ( dimDate[Year], dimDate[Month], DimDate[MMM] ), [Sum of Net Sales Value] ) 
        VAR MinSales = MINX ( ALLSELECTED ( dimDate[Year], dimDate[Month], dimDate[MMM] ), [Sum of Net Sales Value] ) 
        VAR Result = IF ( [Sum of Net Sales Value] = MaxSales || [Sum of Net Sales Value] = MinSales, 
        [Sum of Net Sales Value],
         " " ) 
        RETURN Result
    
        ,KEEPFILTERS( 'dimDate'[Running Month] < 0 )
    )

     

     BR

    Martin