Forum Discussion

danishwahab's avatar
danishwahab
Icon for Helper I rankHelper I
1 year ago
Solved

Previous and Current month sales together

Hello Everybody,

 

I have a requirement where I have a single sales table, and I wanted to display data with two extra columns "PreviousMonth Sale" and Difference. There can be instances where an Item was not sold in current month in that case the record should be displayed with previous sales figures and current month sale will be zero. 

 

If I select slicer value as Nov-2024 than Previous month sale values will be from Oct-2024 and current month sales will be of Nov 2024.

 

Item ID 004 and 007 should also come in the November filter as they were present in the previous month. I am specifically facing challenge in getting items which were present in the previous month but missing in the current or selected month highlited in light blue color.

 

 

 

What is the best way to achieve this?

 

Thanks!

 

  • Anonymous's avatar
    Anonymous
    1 year ago

    Hi,

    Thanks for the solution PwerQueryKees offered, and i want to offer some more information for user to refer to.

    hello danishwahab ,  you can refer to the following solution.

    Sample data is the same as you provided.

    Create a calendar table, and create  a 1:N relationship between the tables.

     

    Calendar = CALENDAR(DATE(2023,1,1),DATE(2024,12,31))

     

    Then create the following measures.

     

    CurrentSales = CALCULATE(SUM('Table'[Sales]))
    PreviousSales = CALCULATE(SUM('Table'[Sales]),DATEADD('Calendar'[Date],-1,MONTH))

     

    Then create a slicer , put thre date field of calendar table to the visual, and create a table visual and put id and measures to it.

    Output

    Best Regards!

    Yolo Zhu

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

     

8 Replies

  • Source data left and result right

    Solution:

    let
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"ItemID", type text}, {"Sales", Int64.Type}}),
        // Assuming the most recent date to be in the current month. Change accroing to your use case.
        current_month = Date.StartOfMonth(List.Max(Source[Date])),
        previous_month = Date.AddMonths(current_month, -1),
        #"Added Month" = Table.AddColumn(Source, "Month", each let 
                    month = Date.StartOfMonth([Date])
                in
                    if month = current_month then "CurrentMonthSale" 
                    else if month = previous_month then "PreviousMonthSale"
                    else null),
        #"Removed Date" = Table.RemoveColumns(#"Added Month",{"Date"}),
        // Just in case there are more than just Current Month and Previous Month sales in the data (this is not the case in the test data set)
        #"Filtered out other months" = Table.SelectRows(#"Removed Date", each [Month] <> null),
        #"Pivoted Sales by Month (SUM)" = Table.Pivot(#"Filtered out other months", List.Distinct(#"Filtered out other months"[Month]), "Month", "Sales", List.Sum),
        // Note the ?? operator: A ?? B will return if A = null then B else A
        #"Grouped Rows" = Table.Group(#"Pivoted Sales by Month (SUM)", {"ItemID"}, {{"PreviousMonthSale", each List.Sum([PreviousMonthSale]) ?? 0, type nullable number}, {"CurrentMonthSale", each List.Sum([CurrentMonthSale]) ?? 0, type nullable number}}),
        #"Added Difference" = Table.AddColumn(#"Grouped Rows", "Difference", each [CurrentMonthSale]-[PreviousMonthSale])
    in
        #"Added Difference"

     Did I answer your question? Then please mark my post as the solution.
    If I helped you, please click on the Thumbs Up to give Kudos.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi,

    Thanks for the solution PwerQueryKees offered, and i want to offer some more information for user to refer to.

    hello danishwahab ,  you can refer to the following solution.

    Sample data is the same as you provided.

    Create a calendar table, and create  a 1:N relationship between the tables.

     

    Calendar = CALENDAR(DATE(2023,1,1),DATE(2024,12,31))

     

    Then create the following measures.

     

    CurrentSales = CALCULATE(SUM('Table'[Sales]))
    PreviousSales = CALCULATE(SUM('Table'[Sales]),DATEADD('Calendar'[Date],-1,MONTH))

     

    Then create a slicer , put thre date field of calendar table to the visual, and create a table visual and put id and measures to it.

    Output

    Best Regards!

    Yolo Zhu

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

     

    • danishwahab's avatar
      danishwahab
      Icon for Helper I rankHelper I

      Thanks both Anonymous  and PwerQueryKees for your solutions much appreciated.

      I'm going ahead with simple approach of adding sales information in the date dimention and joining them together with one to many relationship. 

       

      Thanks!

    • danishwahab's avatar
      danishwahab
      Icon for Helper I rankHelper I

       

      Oct-24  
      DateItemIDSales
      01-Oct-24A100
      02-Oct-24B200
      04-Oct-24D500
      05-Oct-24E160
      07-Oct-24F100

       

       

      Nov-24  
      DateItemIDSales
      01-Nov-24A100
      02-Nov-24C200
      04-Nov-24D680
      05-Nov-24E160
      07-Nov-24F80
      07-Nov-24G600

       

       

      Expected Data   
      ItemIDPreviousMonthSaleCurrentMonthSaleDifference
      A1001000
      B2000-200
      C0200200
      D500680180
      E1601600
      F10080-20
      G0600600
  • Single Sales table as you stated in your original post or 2 sales tables as in your sample?

    • danishwahab's avatar
      danishwahab
      Icon for Helper I rankHelper I

      Single table as mentioned in the original post and Expected Data. 

  • in the example, why for example for the row 2, the prevous mount cale is not equal to the value of current mount sale for row 1?

    is there any other thing should be considered?