Forum Discussion

Narasimha's avatar
Narasimha
Helper I
9 years ago

Last4 Weeks DAX

Hi Team,

 

Could you pleae help me to create Last4 weeks dax formula. Am getting below error while trying to create DAX for Last4Weeks data.

Thnaks in advance

 

Thanks

Narasimha Reddy

 

5 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Using the error message it's fairly easy to repair the calculation. All you need to do is to makes sure the Date in a current row is not greater than the date returned by TODAY() function:

     

    Last4Weeks1 = IF(DimDate[Date]>TODAY();"NO";IF(DATEDIFF(DimDate[Date];TODAY();WEEK)<4 && WEEKNUM(DimDate[Date])<>WEEKNUM(TODAY());"YES";"NO"))
    • Narasimha's avatar
      Narasimha
      Helper I

      Thanks for quick response. am getting below result while applying last4weeks data dax forumala. seems it is worng results.

      Below is the correct output . below visual created manually with correct created/Closed data.

       

       

       

      • v-qiuyu-msft's avatar
        v-qiuyu-msft
        Community Support

        Hi Narasimha,

         

        Which DAX did you write to return Closed and Created values? Can you share some sample data and logic to return expected results?

         

        Best Regards,
        Qiuyun Yu

  • gooranga1's avatar
    gooranga1
    Power Participant

    If you are using a dimdate based from a sql table you can create a column in the dimension to calculate the weeks from the current date as an integer. We have a few week counters for the different ways to count weeks. The datediff in powerbi itself is limited in that it will not count ngeative date differences.

     

    SELECT  dd.*
                  , IIF(DATEPART(YEAR, GETDATE()) - 1 <= dd.year, 1, 0) AS 'Last2Years'
                  , DATEDIFF(WEEK, dd.start_date_of_week, xx.StartDate) AS 'WeeksFromCurrentday'
                  , DATEDIFF(WEEK, dd.iso_start_date_of_week, xxx.StartDate) AS 'WeeksFromCurrentdayISO'
                  , DATEDIFF(WEEK, dd.bybox_start_date_of_week, xxxx.StartDate) AS 'WeeksFromCurrentdayByBox'
            FROM    dim_date_dsv AS dd
                    OUTER APPLY ( SELECT    MAX(dsvx.start_date_of_week) AS 'StartDate'
                                  FROM      dbo.dim_date_dsv AS dsvx
                                  WHERE     dsvx.sql_date = CONVERT(DATE, GETDATE())
                                ) AS xx
                    OUTER APPLY ( SELECT    MAX(dsvx.iso_start_date_of_week) AS 'StartDate'
                                  FROM      dbo.dim_date_dsv AS dsvx
                                  WHERE     dsvx.iso_year = YEAR(DATEADD(DAY,
                                                                  ( 4
                                                                  - DATEPART(WEEKDAY,
                                                                  GETDATE()) ),
                                                                  GETDATE()))
                                            AND dsvx.iso_week_of_year = DATEPART(iso_WEEK,
                                                                  GETDATE()) - 1
                                ) AS xxx
                    OUTER APPLY ( SELECT    MAX(dsvx.bybox_start_date_of_week) AS 'StartDate'
                                  FROM      dbo.dim_date_dsv AS dsvx
                                  WHERE     dsvx.sql_date = CONVERT(DATE,GETDATE())
                                ) AS xxxx;

    Once you have this it's relatviely easy to set a new column in power bi to filter, we use 13 weeks but you can easily chnage to 4. 

     

    The following column is on our dimdate table in powerbi to use in filters.

     

    Last 13 Weeks ByBox = if(Dim_Date[WeeksFromCurrentdayByBox]<=13 && Dim_Date[WeeksFromCurrentdayByBox]>0,"Last 13 Weeks",if(Dim_Date[WeeksFromCurrentdayByBox]=0,"Current Week","> 13 Weeks"))