Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
ydgcr607
Frequent Visitor

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
1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@ydgcr607 , 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])

View solution in original post

3 REPLIES 3
ydgcr607
Frequent Visitor

@amitchandak Thanks for your quick response. It worked!

amitchandak
Super User
Super User

@ydgcr607 , 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])

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

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.