Forum Discussion

monojchakrab's avatar
monojchakrab
Icon for Resolver III rankResolver III
3 years ago

compare previous month from a different year

Hiya good people,

 

I am trying to compare the sales results of the current month with the previous month. And was using two flags - one for the current month and the other for the previous month.

 

Code for current month flag :

 

 

Flag_current = 
var _Selectmonth = MONTH(SELECTEDVALUE('Date slicer'[Date]))
return
if (MONTH(MAX('Amazon YTD'[Reporting Date]))=_Selectmonth,1,0)

 

 

Previous code for Month-1 :

 

 

Flag_previous = 
var _Selectmonth = MONTH(SELECTEDVALUE('Date slicer'[Date]))
// var _Selectyear =YEAR(SELECTEDVALUE('Date slicer'[Date]))
return
// SWITCH(TRUE(),
// MONTH(MAX('Amazon YTD'[Reporting Date]))=_Selectmonth-1 &
// YEAR(MAX('Amazon YTD'[Reporting Date]))=_Selectyear-1,
// 1,0
// )
IF(MONTH(MAX('Amazon YTD'[Reporting Date]))=_Selectmonth-1,1,0)

 

The lines commented out are what I have added to include the year also as a variable but its returning a mismatch (text Vs. Integer) error.

While it was working till the Dec of 2022, its no longer working from Jan'23 as the year has changed. Am I missing something or is there a better way to work around this? the Flag for the current mont still works as its checking within the same year.

Thanks and really appreciate the leg-up.

2 Replies

  • monojchakrab , try measure like these with date table and time intellignece

     

    This month = CALCULATE([Net],DATESMTD(ENDOFMONTH('Date'[Date])))
    MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD('Date'[Date]))

     

    last year MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(dateadd('Date'[Date],-12,MONTH)))
    Previous year Month Sales = CALCULATE(SUM(Sales[Sales Amount]),previousmonth(dateadd('Date'[Date],-11,MONTH)))

     

    Year behind Sales = CALCULATE(SUM(Sales[Sales Amount]),dateadd('Date'[Date],-1,Year))

    2 Year behind Sales = CALCULATE(SUM(Sales[Sales Amount]),dateadd('Date'[Date],-2,Year))

     

    Time Intelligence, Part of learn Power BI https://youtu.be/cN8AO3_vmlY?t=27510
    Time Intelligence, DATESMTD, DATESQTD, DATESYTD, Week On Week, Week Till Date, Custom Period on Period,
    Custom Period till date: https://youtu.be/aU2aKbnHuWs&t=145s

     

    If you need to compare two months from slicers

     

    How to use two Date/Period slicers

    https://youtu.be/WSeZr_-MiTg

    • monojchakrab's avatar
      monojchakrab
      Icon for Resolver III rankResolver III

      amitchandak,

       

      I am looking for a flag type of solution that I had shared as the visual I am working on (as shown below), only works with flags for current month and previous month :

       

      Thanks.