Forum Discussion
Previous Month value in a dataset
Hi All,
Pls guide.
My dataset - I tried using dateadd (but my case does not have date field) - I am able to get previous month no using DAX but not the value corresponding to it.
Source
| Year | Month | Amount |
| 2021 | 1 | 1 |
2 | 2 | |
| 3 | 3 | |
| 4 | 4 | |
| 5 | 5 | |
| 6 | 6 | |
| 7 | 7 | |
| 8 | 8 | |
| 9 | 9 | |
| 10 | 10 |
target
| Year | Month | Amt | PreviousMonth |
| 2021 | 1 | 1 | |
| 2 | 2 | 1 | |
| 3 | 3 | 2 | |
| 4 | 4 | 3 | |
| 5 | 5 | 4 | |
| 6 | 6 | 5 | |
| 7 | 7 | 6 | |
| 8 | 8 | 7 | |
| 9 | 9 | 8 |
Thank you
- Anonymous4 years ago
HI Anonymous,
You can extract the current month and year field value from the current row context, then you can use the date function to concatenate them as a date value to calculate the previous date.
After these steps, you can extract the year and month part values from the variables as conditon to calcualte them in your expression.PrevAmount = VAR currMonth = MAX ( MOM[Month] ) VAR currYear = MAX ( MOM[Year] ) VAR prevDate = DATE ( currYear, currMonth - 1, 1 ) RETURN CALCULATE ( SUM ( MOM[Amount] ), FILTER ( ALLSELECTED ( MOM ), MOM[Month] = MONTH ( prevDate ) && MOM[Year] = YEAR ( prevDate ) ) )Regards,
Xiaoxin Sheng
2 Replies
- AnonymousNot applicable
HI Anonymous,
You can extract the current month and year field value from the current row context, then you can use the date function to concatenate them as a date value to calculate the previous date.
After these steps, you can extract the year and month part values from the variables as conditon to calcualte them in your expression.PrevAmount = VAR currMonth = MAX ( MOM[Month] ) VAR currYear = MAX ( MOM[Year] ) VAR prevDate = DATE ( currYear, currMonth - 1, 1 ) RETURN CALCULATE ( SUM ( MOM[Amount] ), FILTER ( ALLSELECTED ( MOM ), MOM[Month] = MONTH ( prevDate ) && MOM[Year] = YEAR ( prevDate ) ) )Regards,
Xiaoxin Sheng
- littlemojopuppy
Community Champion
Hi Anonymous
You need a date field. If you don't have a date field, make one up...first day of a month, last day of a month, the 16th of a month. It doesn't matter. Then create a date table and mark it appropriately. Then this becomes really easy with time intelligence.