Forum Discussion
Calculate difference from previous month
- 10 years ago
In this scenario, since you need to get the previous month data based on current slicing date, it's better to create a measure instead of a calculated column. Otherwise, you have to lookup previous row based on index column as ankitpatira suggested. Just create a measure like:
difference= sum(DW_Data_Salg[Inntekt])-CALCULATE(SUM(DW_Data_Salg[Inntekt]),PARALLELPERIOD(DW_Data_Salg[DATO].[Date],-1,MONTH))
Or
difference= sum(DW_Data_Salg[Inntekt])-CALCULATE(SUM(DW_Data_Salg[Inntekt]),PREVIOUSMONTH(DW_Data_Salg[DATO].[Date]))
Regards,
Tormod_GK First goto query editor in power bi desktop and under Add column, add index column from zero. Then under modelling tab create new column using below code.
Difference = TABLENAME[Value] - IF( TABLENAME[Value] = 0, TABLENAME[Value], LOOKUPVALUE( TABLENAME[Value], TABLENAME[Index], TABLENAME[Index]-1) )
Hi and thanx for your solution, but there is one problem. My table consists of 2 mill rows and 40 columns and the reference is department. So the lookup is on department and for the previous month.
| Index | Date | Department | Value | Difference |
| 100 | 15.02.2016 | 100 | 50 000 | 50 000 |
| 2345 | 15.03.2016 | 100 | 70 000 | 20 000 |
| 7585 | 15.04.2016 | 100 | 70 000 | 0 |
| 654325 | 15.05.2016 | 100 | 80 000 | 10 000 |
| 345321 | 15.06.2016 | 100 | 20 000 | -60 000 |
- v-sihou-msft10 years agoMicrosoft Employee
In this scenario, since you need to get the previous month data based on current slicing date, it's better to create a measure instead of a calculated column. Otherwise, you have to lookup previous row based on index column as ankitpatira suggested. Just create a measure like:
difference= sum(DW_Data_Salg[Inntekt])-CALCULATE(SUM(DW_Data_Salg[Inntekt]),PARALLELPERIOD(DW_Data_Salg[DATO].[Date],-1,MONTH))
Or
difference= sum(DW_Data_Salg[Inntekt])-CALCULATE(SUM(DW_Data_Salg[Inntekt]),PREVIOUSMONTH(DW_Data_Salg[DATO].[Date]))
Regards,
- rpul8 years agoFrequent Visitor
I have been trying to carry out a similar calculation, but whenever I use a DAX function, such as PREVIOUSYEAR, as a filter in CALCULATE nothing gets returned.
My current expression is:
Difference = CALCULATE(SUM('Yearly Summary'[Sales by Status per Year]), PREVIOUSYEAR('Yearly Summary'[Date].[Date]))
But this returns just blanks. Any idea why this may be or ideas for trouble shooting? If I drop the filter, it works. If I try other filters that are not DAX functions such as 'Yearly Summary'[Date].[Date]=2017 it works.
- jpatil7 years agoNew Member
I aslo got blank results after implementing PREVIOUSYEAR function same as you. However problem resolved when
1) I added Date table with all uninterrupted dates within specified period.
2) Marked it as date table with date column as key.
3) Established relation between date table and data table.
4) Use date table column in PREVIOUSYEAR function in stead of date column from data table.
It worked as expected after following steps above.
- fjcampos6 years agoFrequent Visitor
I have a similar problem, try this solution but it shows me the following:
Using this formula always results in zero for me MOV_NETO_RRC
I would like to filter from January to April and only show me the values enclosed with blue.
My applied measure is as follows:MOV_NETO_RRC = SUM(RRC[RESERVA_RIESGO_CURSO]) - CALCULATE(SUM(RRC[RESERVA_RIESGO_CURSO]);PARALLELPERIOD(DIM_TIEMPO[FECHA].[Date];-1;MONTH)) - ripstaur5 years agoHelper III
What is "DATO" in this expression?
- Tormod_GK5 years agoFrequent Visitor
DATO = DATE 🙂
- SantiagoBS6 years agoNew Member
Hi Tormod_GK
I encountered almost the same problem as you, my solution was to use a calculated index based on the lookup values I needed and using the concatenate function; you would first need to generate the index as a calculated column, as follows:
ConcatIndex = TABLENAME[Index] & TABLENAME[Date] & TABLENAME[Department]Then, use the ConcatIndex to retrieve the value for each Department & Index and the PREVIOUSMONTH function for the previous month:
Difference = TABLENAME[Value] - IF( TABLENAME[Value] = 0, TABLENAME[Value], LOOKUPVALUE( TABLENAME[Value], TABLENAME[ConcatIndex], (TABLENAME[Index] & (PREVIOUSMONTH(TABLENAME[Date] & TABLENAME[Department]))) )I'm not quite sure what's the purpose of the conditional on the last code, it's up to you to use it or not.
I hope this helps to you or someone else, even though i'm 4 years late hehe.