Forum Discussion

Peter_23's avatar
Peter_23
Advocate V
2 years ago

Comparing two periods

Hi Power Bi users, I would like a measure to compare two periods at least, e.g. Feb23 vs Feb22,  Apr23 vs Apr22, but sometimes it could be Feb23 vs Feb20, in case there is not data in 22,21..  

so I know there is: SAMEPERIODLASTYEAR function; but It's year based, so my requirements is over months, is there workaround for this?

 

thanks in advance.

4 Replies

  • Hi Peter_23 ,

    If you want it hardcoded, you can try something like this:

    January 2023 = 
    CALCULATE (
        [Total Revenue],
        FILTER (
            ALL ( Dates ),
            MONTH ( Dates[Date] ) = 1
                && YEAR ( Dates[Date] ) = 2023
        )
    )
    

    This approach is very manual and require a lot of measures for periods that you want to compare.

    Alternatively, I would use a two-slicer approach - for periods being compared. The second slicer is coming from a disconnected table which doesn't have  relationship to your fact table. Either approach will require a separate dates table.

     

    In the sample pbix, there are only two years of data but should work for more years.

     

  • Anonymous's avatar
    Anonymous
    Not applicable

    Thanks for the reply from danextian , please allow me to provide another insight: 
    Hi  Peter_23 ,

    I created some data:

     

    Here are the steps you can follow:

    1. Create measure.

    Measure =
    var _minmonth=MINX(ALLSELECTED('Table'),[MonthYear])
    var _maxmonth=MAXX(ALLSELECTED('Table'),[MonthYear])
    var _minmonthdate=
    MINX(FILTER(ALL('Table'),'Table'[MonthYear]=_minmonth),[Date]) //2022.2.1
    var _maxmonthdate=
    MAXX(FILTER(ALL('Table'),'Table'[MonthYear]=_maxmonth),[Date]) //2023.2.3
    var _maxmonthnotblank=
    MAXX(
        FILTER(ALL('Table'),'Table'[Date]<=_maxmonthdate&&[Amount]<>BLANK()&&MONTH([Date])=MONTH(_maxmonthdate)),[MonthYear]) // Feb23
    var _date=
    MINX(FILTER(ALL('Table'),'Table'[MonthYear]=_maxmonthnotblank),[Date])
    var _minmonthnotblank=
    MAXX(
        FILTER(ALL('Table'),'Table'[Date]<_date&&[Amount]<>BLANK()&&MONTH([Date])=MONTH(_minmonthdate)),[MonthYear]) // Feb23
    return
        SUMX(FILTER(ALL('Table'),'Table'[MonthYear]=_maxmonth),[Amount]) - SUMX(FILTER(ALL('Table'),'Table'[MonthYear]=_minmonthnotblank),[Amount])

    2. Result:

     

     

    Best Regards,

    Liu Yang

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

  • Hi,

    Please share some data to work with and show the expected result.  Share data in a format that can be pasted in an MS Excel file.

  • thanks guys, the aproach of situation is more likely to Anonymous , so you can only select one value e.g. Feb23 and the previuos value should be Feb22,  (automatically). I'll work with your proposal. I'm working with calculate and offset filter function or windows command Offset  , do you experience with these functions?