Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Narasimha
Helper I
Helper I

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.

WeekDAX.PNG

Thnaks in advance

 

Thanks

Narasimha Reddy

 

5 REPLIES 5
gooranga1
Power Participant
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"))
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"))

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

WeekDAX1.PNG

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

 

WeekDAX2.PNG

 

 

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

Community Support Team _ Qiuyun Yu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Below is sample data. I want last 4 weeks created/Closed like below. below output is created from excel data manualy entered week values.

 

WeekDAX2.PNG

 

below is sample data.

IDStartDateEnddate
3232232236/16/17 2:13 AM6/29/17 7:39 AM
3232232246/14/17 9:13 PM6/29/17 7:39 AM
3232232256/29/17 7:26 AM6/29/17 7:29 AM
3232232266/29/17 6:56 AM6/29/17 6:58 AM
3232232276/17/17 10:20 PM6/29/17 6:42 AM
3232232286/22/17 10:10 PM6/29/17 6:40 AM
3232232296/22/17 5:47 AM6/29/17 6:40 AM
3232232306/22/17 5:47 AM6/29/17 6:40 AM
3232232316/22/17 4:39 AM6/29/17 6:40 AM
3232232326/22/17 5:33 AM6/29/17 6:38 AM
3232232336/29/17 6:21 AM6/29/17 6:25 AM
3232232346/29/17 5:51 AM6/29/17 5:54 AM
3232232356/28/17 10:46 AM6/29/17 5:33 AM
3232232366/28/17 10:46 AM6/29/17 5:33 AM
3232232376/28/17 10:46 AM6/29/17 5:33 AM
3232232386/28/17 10:46 AM6/29/17 5:33 AM
3232232396/28/17 10:46 AM6/29/17 5:33 AM
3232232406/28/17 10:46 AM6/29/17 5:33 AM
3232232416/28/17 10:46 AM6/29/17 5:33 AM
3232232426/28/17 10:46 AM6/29/17 5:33 AM
3232232436/28/17 10:46 AM6/29/17 5:33 AM
3232232446/28/17 10:46 AM6/29/17 5:33 AM
3232232456/28/17 10:46 AM6/29/17 5:33 AM
3232232466/28/17 10:46 AM6/29/17 5:33 AM
3232232476/28/17 10:46 AM6/29/17 5:33 AM
3232232486/28/17 10:46 AM6/29/17 5:33 AM
3232232496/28/17 10:46 AM6/29/17 5:33 AM
3232232506/28/17 10:46 AM6/29/17 5:33 AM
3232232516/28/17 10:46 AM6/29/17 5:33 AM
3232232526/28/17 10:46 AM6/29/17 5:33 AM
3232232536/28/17 10:46 AM6/29/17 5:33 AM
3232232546/28/17 10:46 AM6/29/17 5:33 AM
3232232556/28/17 10:46 AM6/29/17 5:33 AM
3232232566/28/17 10:46 AM6/29/17 5:33 AM
3232232576/28/17 10:46 AM6/29/17 5:33 AM
3232232586/28/17 10:46 AM6/29/17 5:33 AM
3232232596/28/17 10:46 AM6/29/17 5:33 AM
3232232606/28/17 10:46 AM6/29/17 5:33 AM
3232232616/28/17 10:46 AM6/29/17 5:33 AM
3232232626/28/17 10:46 AM6/29/17 5:33 AM
3232232636/28/17 10:46 AM6/29/17 5:33 AM
3232232646/28/17 10:46 AM6/29/17 5:33 AM
3232232656/28/17 10:46 AM6/29/17 5:33 AM
3232232666/29/17 5:21 AM6/29/17 5:24 AM
3232232676/28/17 11:53 PM6/29/17 5:20 AM
3232232686/28/17 11:53 PM6/29/17 5:20 AM
3232232696/28/17 11:53 PM6/29/17 5:20 AM
3232232706/28/17 11:53 PM6/29/17 5:20 AM
3232232716/28/17 11:53 PM6/29/17 5:20 AM
3232232726/28/17 11:53 PM6/29/17 5:20 AM
3232232736/28/17 11:53 PM6/29/17 5:20 AM
3232232746/28/17 11:53 PM6/29/17 5:20 AM
3232232756/29/17 4:46 AM6/29/17 4:48 AM
3232232766/29/17 4:14 AM6/29/17 4:17 AM
3232232776/29/17 3:41 AM6/29/17 3:44 AM
3232232786/29/17 3:13 AM6/29/17 3:16 AM
3232232796/24/17 2:57 AM6/29/17 2:47 AM
3232232806/29/17 2:41 AM6/29/17 2:43 AM
3232232816/29/17 2:16 AM6/29/17 2:18 AM
3232232826/29/17 1:46 AM6/29/17 1:48 AM
3232232836/29/17 1:12 AM6/29/17 1:14 AM
3232232846/29/17 12:37 AM6/29/17 12:40 AM
3232232856/29/17 12:02 AM6/29/17 12:05 AM
3232232866/28/17 7:08 PM6/28/17 11:46 PM
3232232876/28/17 7:16 PM6/28/17 11:46 PM
3232232886/20/17 5:14 PM6/28/17 11:46 PM
3232232896/28/17 11:27 PM6/28/17 11:29 PM
3232232906/28/17 10:57 PM6/28/17 10:59 PM
3232232916/28/17 10:22 PM6/28/17 10:24 PM
3232232926/28/17 9:57 PM6/28/17 9:59 PM
3232232936/2/17 9:15 AM6/28/17 9:38 PM
3232232946/28/17 9:22 PM6/28/17 9:25 PM
3232232956/28/17 8:52 PM6/28/17 8:54 PM
3232232966/28/17 3:11 PM6/28/17 8:42 PM
3232232976/28/17 8:17 PM6/28/17 8:19 PM
3232232986/28/17 7:47 PM6/28/17 7:49 PM
3232232996/28/17 7:17 PM6/28/17 7:19 PM
3232233006/28/17 6:42 PM6/28/17 6:44 PM
3232233016/28/17 6:11 PM6/28/17 6:13 PM
3232233026/20/17 12:28 AM6/28/17 5:36 PM
3232233036/28/17 5:32 PM6/28/17 5:34 PM
3232233046/28/17 5:02 PM6/28/17 5:05 PM
3232233056/28/17 10:44 AM6/28/17 4:38 PM
3232233066/28/17 11:49 AM6/28/17 4:38 PM
3232233076/28/17 10:52 AM6/28/17 4:38 PM
3232233086/28/17 10:36 AM6/28/17 4:38 PM
3232233096/28/17 10:36 AM6/28/17 4:38 PM
3232233106/28/17 9:48 AM6/28/17 4:38 PM
3232233116/28/17 4:27 PM6/28/17 4:30 PM
3232233126/28/17 3:57 PM6/28/17 4:00 PM
3232233136/23/17 2:44 AM6/28/17 3:46 PM
3232233146/28/17 3:22 PM6/28/17 3:24 PM
3232233156/28/17 2:52 PM6/28/17 2:55 PM
3232233166/28/17 2:17 PM6/28/17 2:19 PM
3232233176/27/17 3:16 PM6/28/17 2:08 PM
3232233186/28/17 1:42 PM6/28/17 1:45 PM
3232233196/28/17 1:12 PM6/28/17 1:15 PM
3232233206/28/17 11:40 AM6/28/17 11:57 AM
3232233216/2/17 1:07 PM6/28/17 11:57 AM
3232233226/28/17 11:22 AM6/28/17 11:25 AM

 

Thanks

Narasimha

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.