Forum Discussion

newpbiuser01's avatar
newpbiuser01
Helper V
3 years ago
Solved

Identify differences based on date

Hello,

I have the following data that shows the sales by product for each month:

ProductMonthSales
PencilsNovember10
PensDecember50
PaperJanuary20
TonerFebruary30
PencilsMarch45
PensApril70
PaperMay50
TonerJanuary60
PaperFebruary45
TonerMarch10
PencilsApril10
PensMay5
PaperNovember5
PensMay10

 

I need to flag the differences in the sales by product to get the following: 

ProductPrevious Month (April)Current Month (May)Difference
Pens705-55
Paper05050
Pencils100-10

 

How could I do this? I tried grouping the data by product and month, but I can't figure out how to identify just the products with sales in the current and previous months and also how to show the difference in the sales. 

 

I'd appreciate any help!

 

6 Replies

  • newpbiuser01 , Create a date using month in case you do not have date

     

    Date=  datevalue("01-"&[Month] &"2022")

     

    Join the date of your table with date of date table(create a date table) and then use Time Intelligence 

     

    Calendar = Addcolumns(calendar(date(2012,01,01), date(2024,12,31) ), "Month no" , month([date])
    , "Year", year([date])
    , "Month Year", format([date],"mmm-yyyy")
    , "Month year sort", year([date])*100 + month([date])
    , "Qtr Year", format([date],"yyyy-\QQ")
    , "Qtr", quarter([date])
    , "Month",FORMAT([Date],"mmmm")
    , "Month sort", month([DAte])
    , "FY Year", if( Month(_max) <7 , year(_max)-1 ,year(_max))
    , "Is Today" ,if([Date]=TODAY(),"Today",[Date]&"")
    ,"Day of Year" , datediff(date(year([DAte]),1,1), [Date], day)+1
    , "Month Type", Switch( True(),
    eomonth([Date],0) = eomonth(Today(),-1),"Last Month" ,
    eomonth([Date],0)= eomonth(Today(),0),"This Month" ,
    Format([Date],"MMM-YYYY") )
    ,"Year Type" , Switch( True(),
    year([Date])= year(Today()),"This Year" ,
    year([Date])= year(Today())-1,"Last Year" ,
    Format([Date],"YYYY")
    )
    )

     

    example measures

     


    MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD('Date'[Date]))
    last MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(dateadd('Date'[Date],-1,MONTH)))
    last month Sales = CALCULATE(SUM(Sales[Sales Amount]),previousmonth('Date'[Date]))
    MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD('Date'[Date]))
    last MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(dateadd('Date'[Date],-1,MONTH)))
    last month Sales = CALCULATE(SUM(Sales[Sales Amount]),previousmonth('Date'[Date]))

     

     

    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

    • newpbiuser01's avatar
      newpbiuser01
      Helper V

      Thanks amitchandak

       

      One more question, if instead of just the sum of sales, I had to identify the differences in a text field - so for sales in May and April in the following table, identify where I had the sales on that product:

       

      RegionProductMonthYearSales
      EastPencilsNovember202210
      WestPensDecember202250
      SouthPaperJanuary202320
      NorthTonerFebruary202330
      EastPencilsMarch202345
      WestPensApril202370
      SouthPaperMay202350
      NorthTonerJanuary202360
      EastPaperFebruary202345
      WestTonerMarch202310
      SouthPencilsApril202310
      NorthPensMay20235
      EastPaperNovember20225
      WestPensMay202310

       

      Result:

      ProductRegion Sales Last MonthRegion Sales This Month
      PenWestNorth, West
      PencilsSouth 
      Paper South

       

      How would I aggregate based on the filtering criteria? I tried the concatenate function but that doesn't work. I'd appreciate any help!

       

  • Hi,

    It is quite easy to solve with the Date and Time Intelligence functions but for that we also need a year column.  If you have that, then please share that column as well.

    • newpbiuser01's avatar
      newpbiuser01
      Helper V

      Hi Ashish_Mathur , I do have the year as well. So the table would look as follows: 

      ProductMonthYearSales
      PencilsNovember202210
      PensDecember202250
      PaperJanuary202320
      TonerFebruary202330
      PencilsMarch202345
      PensApril202370
      PaperMay202350
      TonerJanuary202360
      PaperFebruary202345
      TonerMarch202310
      PencilsApril202310
      PensMay20235
      PaperNovember20225
      PensMay202310