Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

Same column calculation with condition in another column

Hi,

 

I have a following table, and would like to calculate the cost difference from previous month per Product. For example, for Product A, the difference between May and June is 1, June and July is 4. The calculation is just a simple subtraction but I have no idea how to achieve this with DAX. It would be great if you provide a sample code. Thanks!

 

YearMonthProductCostCost difference from last month (Required result)
20225A100
20225B200
20225C300
20226A111
20226B211
20226C311
20227A154
20227B254
20227C354
  • Anonymous , Create a date column

    date = eomonth(date([year], [month], 1),0)

     

     

    then create a new column

    last month Cost =

    var _last = eomonth([Date],-1)

    return

    [cost] - maxx(filter(Table, [Product] = earlier([Product])  && eomonth([Date],0)  = _last) , [Cost])

3 Replies

  • Anonymous , Create a date column

    date = eomonth(date([year], [month], 1),0)

     

     

    then create a new column

    last month Cost =

    var _last = eomonth([Date],-1)

    return

    [cost] - maxx(filter(Table, [Product] = earlier([Product])  && eomonth([Date],0)  = _last) , [Cost])

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi amitchandak 

       

      I have another question relating the original one. I would like to know how to calculate the quartely difference per Product like below. The 1st quarter is from April to Jun, and the 2nd quarter starts from Jul. I am looking for a solution to calculate the difference of quartely aggregated cost per product. I would appreciate if you provide a sample DAX code. Thanks.

       

      YearMonthProductCostCost difference from last quarter (Required result)
      20224A100
      20224B200
      20225A100
      20225B200
      20226A100
      20226B200
      20227A1515
      20227B25-10
      202228A1515
      20228B25-10
      20229A1515