Forum Discussion
Narasimha
9 years agoHelper 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. Thnaks in advance Thanks Narasimha Reddy
gooranga1
9 years agoPower 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"))