Forum Discussion

carade97's avatar
carade97
Frequent Visitor
3 years ago
Solved

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 Learn

     

    Here 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])
    return
    IF( _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-1
    return
    IF( _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

  • carade97's avatar
    carade97
    Frequent Visitor
    raw datacalculate
    dateitemweekCount week in year?
    30/04/23zz416
    01/05/23a117
    02/05/23b117
    03/05/23c117
    04/05/23d117
    05/05/23e117
    06/05/23f117
    07/05/23g117
    08/05/23h218
    09/05/23i218
    10/05/23j218
    11/05/23k218
    12/05/23l218
    13/05/23m0218
    14/05/23m218
    15/05/23n318
    16/05/23o319
    17/05/23p319
    18/05/23q319
    19/05/23r319
    20/05/23s319
    21/05/23t319
    22/05/23u420
    23/05/23v420
    24/05/23w420
    25/05/23x420
    26/05/23y420
    27/05/23z420
  • 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 Learn

     

    Here 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])
    return
    IF( _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-1
    return
    IF( _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