Hi All,
Here we are wrapping up the Problem of The Week # 5 with more than enough creative solutions to tackle the week-related calculations. I thank you all who participated in the challenge proving once again that there are many ways to tackle the same problem. I absolutely loved how the members presented the solution in a dashboard style format making these challenges even more exciting.
How I Approached the Problem?
If we had the data till the most recent date it could have been easier to tackle the week related calculation however, in practical life, it is not necessary that you have the data till the most recent date. For example, my office was closed in the last week of Christmas therefore, I didn’t have any sales or production data when I tried to calculate the Last Week production or Sales. To tackle this problem, I tried to restrict my weeks from the 1st day till the last date in which the data is available i.e. 11th Jan 2021 with the help of the following measure:
Week Offset Upper Limit =
Var _MaxDate = MAX('COVID Data'[Date])
RETURN
CALCULATE(
MAX( 'Date'[WeekOffset] ) -1,
'Date'[Date] <= _MaxDate
)
Then used this measure (set as variable called _UpperLimit) to get the range of weeks that has the data available till the last week:
CALCULATETABLE(
VALUES('Date'[WeekOffset]),
'Date'[WeekOffset] <= _UpperLimit)
After that used this virtual table inside the TOPN function to get the Last N Weeks dynamically with Parameter selection :
TOPN(
_WeekNumbersSelected, //this is a variable for weeks selection SELECTEDVALUE('Weeks
Selection'[Parameter])
CALCULATETABLE(
VALUES('Date'[WeekOffset]),
'Date'[WeekOffset] <= _UpperLimit),
'Date'[WeekOffset],
DESC
)
Once I got the required number of weeks, I used the ADDCOLUMNS to get the total cases for the weeks selected:
Var _vTable =
ADDCOLUMNS(
_TopNWeeksSelection,
"@Cases",
[Total Cases]
)
Finally, it was time to sum all the cases that are selected in the Parameter:
Var _LastNWeeksCases =
SUMX(
_vTable,
[@Cases]
)
For the Weeks selected that are before the Last N Weeks, the only change made in the calculation was to take the Week Offset Upper Limit and subtract it from the whatever weeks are selected dynamically and the rest of the calculations were same as above:
Var _PreviousNWeekOfYear = [Week Offset Upper Limit] - 'Weeks Selection'[Number Of Weeks]
For the more detailed solution, you can check out My Video for the POTW 5 solution.
Conclusion
I hope you all liked this challenge and learned something from it as I literally learned a lot from the submissions. Thank you once again for being active and amazing members of ENTERPRISE DNA.
I think that is it from my side & attaching the PBIX file of my solution.
STAY TUNED FOR POTW 6!
POTW 5 Solution - Mudassir Ali.pbix (457.5 KB)