Forum Discussion
How to show previous week data from present filtering week
Hi guys, please help me figure this problem out
I want to show previous data like this
can you help me how to measure it? or anything else
Thanks in advance
Hi , carade97
According to your description, you want to filter the [year], [Month] , [Week] fields and display this week item and the previous week item.
For your week num , you can use the WEEKNUM to get the week in year:
WEEKNUM function (DAX) - DAX | Microsoft LearnHere are the steps you can refer to :
(1)We can first add a caluclated column in your table :Column = WEEKNUM([date],2)(2)Then we need to add two calculated columns , the one is the Date table and the other is the Dim_Week table :
This is the Date Table:Date = ADDCOLUMNS( CALENDAR(DATE(2022,11,1),DATE(2023,12,31)) , "Week in Year" , WEEKNUM([Date],21) , "Year" , YEAR([Date]), "Month" , FORMAT([Date],"mmmm") )This is the Dim_Week Table:
And we do not need to create any relationship between tables.
(3)Then we can create two measures like this:
This week = var _date = VALUES('Date'[Date])var _week = MAX('Dim_Week'[Column2])var _this_month_table = FILTER(ALLSELECTED('Fact Table'),'Fact Table'[date] in _date)var _week_in_year = FILTER(_this_month_table , [week]=_week)var _selected_weekinYear = MAXX(_week_in_year,[Column])returnIF( _selected_weekinYear in VALUES('Fact Table'[Column]) ,1,-1)Last week = var _date = VALUES('Date'[Date])var _week = MAX('Dim_Week'[Column2])var _this_month_table = FILTER(ALLSELECTED('Fact Table'),'Fact Table'[date] in _date)var _week_in_year = FILTER(_this_month_table , [week]=_week)var _selected_weekinYear = MAXX(_week_in_year,[Column])var _pre_week = _selected_weekinYear-1returnIF( _pre_week in VALUES('Fact Table'[Column]) ,1,-1)(4)Then we can put the two measures on the visual's "Filter on this visual" and we can get the result as follows:
If this method does not meet your needs, you can provide us with your special sample data and the desired output sample data in the form of tables, so that we can better help you solve the problem. (You can also upload you sample .pbix [without sensitive data] to the OneDrive and share with the OneDrive link to me ! )
Thank you for your time and sharing, and thank you for your support and understanding of PowerBI!
Best Regards,
Aniya Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly
2 Replies
- carade97Frequent Visitor
raw data calculate date item week Count week in year? 30/04/23 zz 4 16 01/05/23 a 1 17 02/05/23 b 1 17 03/05/23 c 1 17 04/05/23 d 1 17 05/05/23 e 1 17 06/05/23 f 1 17 07/05/23 g 1 17 08/05/23 h 2 18 09/05/23 i 2 18 10/05/23 j 2 18 11/05/23 k 2 18 12/05/23 l 2 18 13/05/23 m0 2 18 14/05/23 m 2 18 15/05/23 n 3 18 16/05/23 o 3 19 17/05/23 p 3 19 18/05/23 q 3 19 19/05/23 r 3 19 20/05/23 s 3 19 21/05/23 t 3 19 22/05/23 u 4 20 23/05/23 v 4 20 24/05/23 w 4 20 25/05/23 x 4 20 26/05/23 y 4 20 27/05/23 z 4 20 - v-yueyunzh-msft
Community Support
Hi , carade97
According to your description, you want to filter the [year], [Month] , [Week] fields and display this week item and the previous week item.
For your week num , you can use the WEEKNUM to get the week in year:
WEEKNUM function (DAX) - DAX | Microsoft LearnHere are the steps you can refer to :
(1)We can first add a caluclated column in your table :Column = WEEKNUM([date],2)(2)Then we need to add two calculated columns , the one is the Date table and the other is the Dim_Week table :
This is the Date Table:Date = ADDCOLUMNS( CALENDAR(DATE(2022,11,1),DATE(2023,12,31)) , "Week in Year" , WEEKNUM([Date],21) , "Year" , YEAR([Date]), "Month" , FORMAT([Date],"mmmm") )This is the Dim_Week Table:
And we do not need to create any relationship between tables.
(3)Then we can create two measures like this:
This week = var _date = VALUES('Date'[Date])var _week = MAX('Dim_Week'[Column2])var _this_month_table = FILTER(ALLSELECTED('Fact Table'),'Fact Table'[date] in _date)var _week_in_year = FILTER(_this_month_table , [week]=_week)var _selected_weekinYear = MAXX(_week_in_year,[Column])returnIF( _selected_weekinYear in VALUES('Fact Table'[Column]) ,1,-1)Last week = var _date = VALUES('Date'[Date])var _week = MAX('Dim_Week'[Column2])var _this_month_table = FILTER(ALLSELECTED('Fact Table'),'Fact Table'[date] in _date)var _week_in_year = FILTER(_this_month_table , [week]=_week)var _selected_weekinYear = MAXX(_week_in_year,[Column])var _pre_week = _selected_weekinYear-1returnIF( _pre_week in VALUES('Fact Table'[Column]) ,1,-1)(4)Then we can put the two measures on the visual's "Filter on this visual" and we can get the result as follows:
If this method does not meet your needs, you can provide us with your special sample data and the desired output sample data in the form of tables, so that we can better help you solve the problem. (You can also upload you sample .pbix [without sensitive data] to the OneDrive and share with the OneDrive link to me ! )
Thank you for your time and sharing, and thank you for your support and understanding of PowerBI!
Best Regards,
Aniya Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly