I'm trying to create a measure to calculate the sum of last week's sales. My calendar is custom and held in a date table, starting in February and can be either 52 or 53 week years.
The code below is from this handy youtube tutorial (https://www.youtube.com/watch?v=jclWnA7pEvY) but I can't work out how to accomodate anything for week 1 if I'm not in a 52 week year.
In the code below, [Week_WOY] is a number from 1-53, and [Year_Uid] is the year - e.g. 2015,2016 etc.
I'm creating my tabular model in VS and then accessing using PowerBI in Live Query mode after deployment. Annoyingly that means I can't use some DAX expressions (e.g. SELECTEDVALUE).
Logically I think I want to: a) decide if the current week is week 1 b) find the number of weeks in the previous year c) return the sum of sales for that (b) week number
Here is the code I have at the moment:
LastWeekTest:=
VAR CurrentWeek = IF ( HASONEVALUE ( 'Date'[Week_WOY]), VALUES ( 'Date'[Week_WOY]) )
VAR CurrentYear = IF ( HASONEVALUE ( 'Date'[Year_Uid] ), VALUES ( 'Date'[Year_Uid] ) )
VAR MaxWeekNumber = CALCULATE(MAX('Date'[Week_WOY]), All('Date'))
RETURN
SUMX(
FILTER(ALL('Date'),
IF(CurrentWeek = 1,
('Date'[Week_WOY] = MaxWeekNumber&& 'Date'[Year_Uid] = CurrentYear - 1),
('Date'[Week_WOY] = CurrentWeek - 1 && 'Date'[Year_Uid] = CurrentYear)
)),
'Sales'[Sales - Inc VAT £]
)