Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Measure for Previous year with custom date table

Hi al,

 

I have a table with this years sales and last year sales on which I want to filter on both Month and Week. However the weeks are based on the ISO weeknumbers and I want to compare ISO week numbers against previous year. 

For your infomation below a representation of the ISO week numbers in the date table.

 

DateNormal weekISO week numerMonth
1-1-20211531
2-1-20211531
3-1-20201531
4-1-2020111
5-1-2020111

 

In 2020 it looks like:

 

datenormal week numberIso week numermonth
1-1-2020111
2-1-2020111
3-1-2020111
4-1-2020111

 

So if I select the ISO week number 1 want to compare the dates which are on week 1 of last year. 

Fortunatly I found the formula below which does that perfectly

Sales Last year =
VAR CurrentWeek = SELECTEDVALUE( 'Calendar'[ISO Week Number] )
VAR CurrentYear = SELECTEDVALUE( 'Calendar'[Year] )

RETURN
CALCULATE( [Sales],
FILTER( ALL( 'Calendar' ),
        'Calendar'[ISO Week Number] = CurrentWeek && 'Calendar'[Year] = CurrentYear - 1))
 
 
However this measure does not work if I select a Month. I can understand why but I do not know how to incorporate the normal month numers in this measure. 

Hope you can help me. 



  • Hi Anonymous ,

     

    Modify the measure as below:

    Sales Last year =
    VAR CurrentWeek =
        SELECTEDVALUE ( 'Calendar'[ISO Week Number] )
    VAR CurrentYear =
        SELECTEDVALUE ( 'Calendar'[Year] )
    VAR currentMonth =
        SELECTEDVALUE ( 'Calendar'[MonthNum] )
    RETURN
        IF (
            NOT ( ISFILTERED ( 'Calendar'[Year] ) ),
            "You didnt select a year",
            IF (
                ISFILTERED ( 'Calendar'[Year] ),
                IF (
                    ISFILTERED ( 'Calendar'[ISO Week Number] )
                        && NOT ( ISFILTERED ( 'Calendar'[MonthNum] ) ),
                    CALCULATE (
                        [Sales],
                        FILTER (
                            ALL ( 'Calendar' ),
                            'Calendar'[ISO Week Number] = CurrentWeek
                                && 'Calendar'[Year] = CurrentYear - 1
                        )
                    ),
                    IF (
                        ISFILTERED ( 'Calendar'[ISO Week Number] )
                            && ISFILTERED ( 'Calendar'[MonthNum] ),
                        CALCULATE (
                            [Sales],
                            FILTER (
                                ALL ( 'Calendar' ),
                                'Calendar'[ISO Week Number] = CurrentWeek
                                    && 'Calendar'[Year] = CurrentYear - 1
                                    && 'Calendar'[MonthNum] = currentMonth
                            )
                        ),
                        IF (
                            NOT ( ISFILTERED ( 'Calendar'[ISO Week Number] ) )
                                && ISFILTERED ( 'Calendar'[MonthNum] ),
                            CALCULATE (
                                [Sales],
                                FILTER (
                                    ALL ( 'Calendar' ),
                                    'Calendar'[MonthNum] = currentMonth
                                        && 'Calendar'[Year] = CurrentYear - 1
                                )
                            ),
                            IF (
                                NOT ( ISFILTERED ( 'Calendar'[ISO Week Number] ) )
                                    && NOT ( ISFILTERED ( 'Calendar'[MonthNum] ) ),
                                CALCULATE (
                                    [Sales],
                                    FILTER ( ALL ( 'Calendar' ), 'Calendar'[Year] = CurrentYear - 1 )
                                ),
                                BLANK ()
                            )
                        )
                    )
                )
            )
        )
    

    And you will see:

     

    For the related .pbix file,pls see attached.

     

     

    Best Regards,
    Kelly

    Did I answer your question? Mark my post as a solution!

7 Replies

  • Anonymous , Check if this can work

     

    Sales Last year =
    VAR CurrentWeek = maxx(allselected('Calendar'[), 'Calendar'[ISO Week Number] )
    VAR CurrentYear = maxx(allselected('Calendar'[), 'Calendar'[Year] )

    RETURN
    CALCULATE( [Sales],
    FILTER( ALL( 'Calendar' ),
    'Calendar'[ISO Week Number] = CurrentWeek && 'Calendar'[Year] = CurrentYear - 1))

    • Anonymous's avatar
      Anonymous
      Not applicable

      amitchandak 

      Unfortunatly not. Selecting a Month gives wierd results. Weeks still works fine. Have another Idea?

  • v-kelly-msft's avatar
    v-kelly-msft
    Icon for Community Support rankCommunity Support

    Hi  Anonymous ,

     

    Based on your description,I cant competely reproduce your senario,could you pls provide some sample data with expected output for a test?

    Best Regards,
    Kelly

    Did I answer your question? Mark my post as a solution!

      • v-kelly-msft's avatar
        v-kelly-msft
        Icon for Community Support rankCommunity Support

        Hi  Anonymous ,

         

        After checking,if you have selected a year,then you wont see an error,so modify your measure as below:

        Sales Last year = 
        VAR CurrentWeek = SELECTEDVALUE( 'Calendar'[ISO Week Number] )
        VAR CurrentYear = SELECTEDVALUE( 'Calendar'[Year] )
        
        RETURN
        IF(ISFILTERED('Calendar'[Year]),
        CALCULATE( [Sales],
        FILTER( ALL( 'Calendar' ),
                'Calendar'[ISO Week Number] = CurrentWeek && 'Calendar'[Year] = CurrentYear - 1)),
                CALCULATE( [Sales],
        FILTER( ALL( 'Calendar' ),
                'Calendar'[ISO Week Number] = CurrentWeek && 'Calendar'[Year] =YEAR(TODAY()) - 1)))+0

        And you will see:

         

        For the related .pbix file,pls see attached.

         

         

        Best Regards,
        Kelly

        Did I answer your question? Mark my post as a solution!