Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

Last 12 months data from current month

Hello, 1.Is there a way to display last 12 month from max date from sales tabel? So if its current month of Nov as for today, it'll show Dec 2021 - Nov 2022, when Dec become current month Jan-Dec? I...
  • Anonymous's avatar
    Anonymous
    3 years ago

    Hi Anonymous ,

    Please try below steps:

    1. below is my test table

    Table:

     

    2. create measure with below dax formula

     

    Combine =
    VAR cur_date =
        SELECTEDVALUE ( 'Table'[Date] )
    VAR _value =
        YEAR ( cur_date ) * 100
            + MONTH ( cur_date )
    RETURN
        _value
    

     

     

    RK = RANKX(ALL('Table'),[Combine],,ASC,Dense)
    Measure =
    VAR last_date =
        MAXX ( ALL ( 'Table' ), [Date] )
    VAR rk =
        CALCULATE ( [RK], FILTER ( ALL ( 'Table' ), [Date] = last_date ) )
    VAR tmp =
        CALCULATETABLE (
            VALUES ( 'Table'[Date] ),
            FILTER ( ALL ( 'Table' ), [RK] > rk - 12 )
        )
    VAR cur_date =
        SELECTEDVALUE ( 'Table'[Date] )
    RETURN
        IF ( cur_date IN tmp, 1, 0 )
    
    Year and Qtr =
    VAR cur_date =
        SELECTEDVALUE ( 'Table'[Date] )
    VAR cur_qtr = [Qtr]
    VAR _value =
        YEAR ( cur_date ) * 100 + cur_qtr
    RETURN
        _value
    
    RK For Qtr = RANKX(ALL('Table'),[Year and Qtr],,ASC,Dense)
    Condition for Qtr =
    VAR last_date =
        MAXX ( ALL ( 'Table' ), [Date] )
    VAR rk =
        CALCULATE ( [RK For Qtr], FILTER ( ALL ( 'Table' ), [Date] = last_date ) )
    VAR tmp =
        CALCULATETABLE (
            VALUES ( 'Table'[Date] ),
            FILTER ( ALL ( 'Table' ), [RK For Qtr] > rk - 4 )
        )
    VAR cur_date =
        SELECTEDVALUE ( 'Table'[Date] )
    RETURN
        IF ( cur_date IN tmp, 1, 0 )
    
    Year and Week Num =
    VAR cur_data =
        SELECTEDVALUE ( 'Table'[Date] )
    VAR wn = [Week Num]
    RETURN
        YEAR ( cur_data ) * 100 + wn
    
    RK for Week Num = RANKX(ALL('Table'),[Year and Week Num],,ASC,Dense) 
    Condition for Week Num =
    VAR last_date =
        MAXX ( ALL ( 'Table' ), [Date] )
    VAR rk =
        CALCULATE ( [RK for Week Num], FILTER ( ALL ( 'Table' ), [Date] = last_date ) )
    VAR tmp =
        CALCULATETABLE (
            VALUES ( 'Table'[Date] ),
            FILTER ( ALL ( 'Table' ), [RK for Week Num] > rk - 52 )
        )
    VAR cur_date =
        SELECTEDVALUE ( 'Table'[Date] )
    RETURN
        IF ( cur_date IN tmp, 1, 0 )
    

     

    3. add a table visual with fields, add measure to it filter pane

    Please refer the attached .pbix file.

     

    Best regards,
    Community Support Team_ Binbin Yu
    If this post helps, then please consider Accept it as the solution to help the other members find it more quick