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.
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!
Year | Month | Product | Cost | Cost difference from last month (Required result) |
2022 | 5 | A | 10 | 0 |
2022 | 5 | B | 20 | 0 |
2022 | 5 | C | 30 | 0 |
2022 | 6 | A | 11 | 1 |
2022 | 6 | B | 21 | 1 |
2022 | 6 | C | 31 | 1 |
2022 | 7 | A | 15 | 4 |
2022 | 7 | B | 25 | 4 |
2022 | 7 | C | 35 | 4 |
Solved! Go to Solution.
@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])
@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.
Year | Month | Product | Cost | Cost difference from last quarter (Required result) |
2022 | 4 | A | 10 | 0 |
2022 | 4 | B | 20 | 0 |
2022 | 5 | A | 10 | 0 |
2022 | 5 | B | 20 | 0 |
2022 | 6 | A | 10 | 0 |
2022 | 6 | B | 20 | 0 |
2022 | 7 | A | 15 | 15 |
2022 | 7 | B | 25 | -10 |
20222 | 8 | A | 15 | 15 |
2022 | 8 | B | 25 | -10 |
2022 | 9 | A | 15 | 15 |
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
115 | |
100 | |
88 | |
68 | |
61 |
User | Count |
---|---|
150 | |
120 | |
100 | |
87 | |
68 |