Forum Discussion

PavithraShan's avatar
PavithraShan
Regular Visitor
6 years ago

How to calculate dynamically Changing week

 

Hi,

Need to find dynamically changing week i.e., need to display previous week(sales) vs current week(sales) against fiscal year.

7 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi,

     

    you can add a custom Column in powerquery to your calendar:

    for example for previous week.

    = Table.AddColumn(#"YourPreviousStep", "LastWeek", each if Date.IsInPreviousWeek([Date]) then "Yes" else "No")

     

    and then you can you can build your measure with filter on those columns

     

    /Adam

  • If you have a date table and week slicer coming from that you can have 7 days behind measure to give you last week

     

    last week Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(dateadd('Date'[Date],-7,Day)))

     

    To get the best of the time intelligence function. Make sure you have a date calendar and it has been marked as the date in model view. Also, join it with the date column of your fact/s.

    Refer
    https://radacad.com/creating-calendar-table-in-power-bi-using-dax-functions
    https://www.archerpoint.com/blog/Posts/creating-date-table-power-bi

    https://www.sqlbi.com/articles/creating-a-simple-date-table-in-dax/

     

    Appreciate your Kudos. In case, this is the solution you are looking for, mark it as the Solution. In case it does not help, please provide additional information and mark me with @
    Thanks. My Recent Blog -
    Winner-Topper-on-Map-How-to-Color-States-on-a-Map-with-Winners , HR-Analytics-Active-Employee-Hire-and-Termination-trend
    Power-BI-Working-with-Non-Standard-Time-Periods And Comparing-Data-Across-Date-Ranges

    Connect on Linkedin

     

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      amitchandak 

      Isnt that formula just gonna give him - 7 days so it can be between weeks depending on what day it is today?

      I mean week 52 is 52 not half 51 & half 52 

       

      /Adam

      • amitchandak's avatar
        amitchandak
        Super User

        Try like

        This WEEK
        var _max_week = maxx(date,date[Fiscal Week])
        return
        calculate(sum(x),all(date[Fiscal Week]), Date[Fiscal Week] =_max_week)
        
        last week
        var _last_week = calculate(Max(Fiscal Week),datediff(date[date],-7,day))//
        
        return
        calculate(sum(x),all(date[Fiscal Week]), Date[Fiscal Week] =_last_week)

         

        I

         

    • PavithraShan's avatar
      PavithraShan
      Regular Visitor

      Then how to calculate Current week sales? Is That change dynamically for upcoming weeks?

      • amitchandak's avatar
        amitchandak
        Super User

        Check if this can help

        This week sales = 
        var _date = date(year(today()),month(today()),day(today()))
        var _Week_ED = _date + (-1*WEEKDAY(_date)+1)
        var _Week_SD= _date +(7-1*WEEKDAY(_date))
        
        Return 
        CALCULATE(sum(Sales[Sales Amount]),all('Date'),Sales[Sales Date]>=_Week_SD && Sales[Sales Date]<=_Week_ED)
        
        
        Last week sales = 
        var _date = date(year(today()),month(today()),day(today()))
        var _Week_ED = _date +( (-1*WEEKDAY(_date)+1)-7)
        var _Week_SD= _date +((7-1*WEEKDAY(_date))-7)
        
        Return 
        CALCULATE(sum(Sales[Sales Amount]),all('Date'),Sales[Sales Date]>=_Week_SD && Sales[Sales Date]<=_Week_ED)