Forum Discussion
gopichilla
9 years agoResolver III
previous week sales.
Hi All,
I want previous week sales anyone can send me DAX query.
Thanks
gopichilla wrote:
Hi All,
I want previous week sales anyone can send me DAX query.
Thanks
You'll need a calendar table as below.
calendar = VAR TEMPTBL = ADDCOLUMNS ( CALENDAR ( "2016-12-31", "2017-12-31" ), "Year", YEAR ( [Date] ), "weekNo", WEEKNUM ( [Date] ), "YearWeek", CONCATENATE ( YEAR ( [Date] ), RIGHT ( CONCATENATE ( "0", WEEKNUM ( [Date] ) ), 2 ) ) ) RETURN ADDCOLUMNS ( TEMPTBL, "WeekIndex", RANKX ( TEMPTBL, [YearWeek],, ASC, DENSE ) )Then create a one to many relationship from calendar table to data table. Then use a measure as below to get the previous week's value.
previous week value = CALCULATE ( SUM ( data[value] ), FILTER ( ALLSELECTED ( 'calendar' ), MAX ( 'calendar'[WeekIndex] ) = 'calendar'[WeekIndex] + 1 ) )See more details in the attached pbix file.
6 Replies
- Eric_Zhang
Microsoft Employee
gopichilla wrote:
Hi All,
I want previous week sales anyone can send me DAX query.
Thanks
You'll need a calendar table as below.
calendar = VAR TEMPTBL = ADDCOLUMNS ( CALENDAR ( "2016-12-31", "2017-12-31" ), "Year", YEAR ( [Date] ), "weekNo", WEEKNUM ( [Date] ), "YearWeek", CONCATENATE ( YEAR ( [Date] ), RIGHT ( CONCATENATE ( "0", WEEKNUM ( [Date] ) ), 2 ) ) ) RETURN ADDCOLUMNS ( TEMPTBL, "WeekIndex", RANKX ( TEMPTBL, [YearWeek],, ASC, DENSE ) )Then create a one to many relationship from calendar table to data table. Then use a measure as below to get the previous week's value.
previous week value = CALCULATE ( SUM ( data[value] ), FILTER ( ALLSELECTED ( 'calendar' ), MAX ( 'calendar'[WeekIndex] ) = 'calendar'[WeekIndex] + 1 ) )See more details in the attached pbix file.
- gopichillaResolver III
Thanks it is working fine.
- ApurvaKhatriHelper III
I wonder why its not working for me
- Ashish_Mathur
Super User