Previous Week Measure - Week 52 2019 and Week 1 2020 - powerbi

I currently use the below measure to retrieve a value for the previous week, however as it's now week 1 of a new year, the field is returning "blank" as it cannot find any previous weeks.
How would I adapt my measure to include something along the lines of... if week 1 use the last week of the previous year, if not use the previous week.
VAR CURRENT_WEEK = WEEKNUM(TODAY()) return
CALCULATE(AVERAGE(DATA_TABLE[VALUE]),
FILTER (DATA_TABLE, WEEKNUM(DATA_TABLE[DATE]) = CURRENT_WEEK -1))
Thanks in advance for your help

I would suggest using a start date and calculating the weeknum from that date. For example, if you choose 1 Jan 2018 as start date, then 1-7 Jan 2018 would be week 1 and first week in 2020 would be week 105. This would solve your issue.
The method you are using becomes really hard to handle if your data has multiple years. Weeknum 1 would denote the 1st week of 2020, 2019 and all the other years. This will mess up the calculation. The above method will make sure you have a unique number for a week.

Related

I'm trying to generate the week number in PowerBI for 2023 and its giving me incorrect figures

For instance week 1 for 2023 is 01/01/2023 to 07/01/202 which is wrong.
The correct week for week 1 of 2023 is 02/01/2023 to 08/01/2023
Screenshot of the calendar
Can someone please help?
I tried creating several times but to no avail
You can specify week systems in the WEEKNUM DAX function as an optional second argument.
The default for this optional parameter is system 1, where week 1 is the week that contains January 1st. System 2 sets week 1 as the week containing the first Thursday of the new year, which is ISO 8601 compliant.
Try this:
Week = WEEKNUM ( 'Date'[Date] , 2 )
If you are calculating this in Power Query, the calculation is much more complex, for some reason. See this link for a solution: https://datacornering.com/how-to-calculate-iso-week-number-in-power-query/

Same Period Last Month

I'm struggling to get the same period of last month.
I want to compare the current month period, for instance, today is 16June2021 so I want to get the sales from May 1st to May 16th.
I'm using this formula but I get the whole month total:
Prev MTD = calculate(sum(Sales[Sales_Amount]),DATEADD(filter(DATESMTD(Sales[Sale_Date]),Sales[Sale_Date]<=today()),-1,MONTH))
Creating a table with DATEADD(filter(DATESMTD(Sales[Sale_Date]),Sales[Sale_Date]<=today()),-1,MONTH), I also get every day of last month.
Is it mandatory to use a Date Table? Already tried but the results came empty.
Is it something regarding my date format? From the import it comes as date/time format.
Thank you very much
Try with:
Prev MTD = calculate(sum(Sales[Sales_Amount]),filter(ALL(Sales[Sale_Date]),Sales[Sale_Date] >= DATE( YEAR(TODAY()), MONTH(TODAY())-1, 1) && Sales[Sale_Date]<=today()))
where order_date between '2022-08-01' and DATE_SUB(CURRENT_DATE(),32)
last month 31 days that's why i am calculated 32 days.
if last month 30 day than you need to calculate 31 days.

Relative date filter on calendar week not calculating last date of the latest week correctly

I have date dimension table and sales fact table.
Created a chart visual with date week on x axis and values as sum of salesamt.
In the filter pane added relative filter to the date. In the last 4 calendar weeks.
I have a measure that shows LASTDATE(DateTable[Date]) also on the report.
The last date is showing as 6 Feb 2021 (Saturday)
Same value when I select the latest week on x axis.
However when I choose any prior week on x axis, then the last date value is Sunday 31 Jan 2021.
Why does the current week last date show as 6th Jan 2021 (saturday) instead of 7th Jan 2021 (sunday)?
Currently, calendar week is defined Sunday to Saturday when it comes to filtering using relative dates. You can work around it by introducing additional column (basically date - 1 day) and set your relative filter on that.

How to filter YTD to last complete month in Power BI

I am creating a report with buttons that use a slicer to show the last 3 calendar months, the default view, and YTD. The first two are all set and will continue to work fine, however i am having trouble with the YTD filter because i need it to exclude the current month (some of the key metrics for this slicer are only accurate monthly, even thought the data is updated Daily). Any idea how to accomplish this without me having to manually change it every month? An example of it working today would show me 2020 through August, since September is not complete. September would be included in the filter starting October first. I am thankful for your help/insights!
I typically build a calculated column on my date table called something like "Date in Range", that looks something like the below. You could also apply this to a date in a normal table if you are not using a date dimension.
Date in Range = IF ('MyTable'[Date] <
DATEADD(TODAY(), -1 * DAY(TODAY()), day),
1,
0)
This compares the date in the table row with TODAY(), e.g. 14 Sep 2020, minus the day of the month of today (14), effectively getting you back to the start of the current month. This will then return 1 for dates before the end of last month or 0. Filter on 1 or 0 to get your result (or use something more meaningful in place of the 1 or 0).

PowerBI - Convert Date to the WeekNumber of the Month

Date WeekNum Month Year
5/2/2018 Week 1 May 2018
6/1/2018 Week 1 June 2018
How would you get the WeekNum from the Date?
The WeekNumber needs to be week number of the particular month and not the Year.
One approach would be to take the day of the month and divide by seven:
WeekNum = ROUNDUP(DIVIDE(DAY(TableName[Date]), 7), 0)
You can use the next formula
1 + WEEKNUM(usage_users[row_date]) - WEEKNUM(STARTOFMONTH(usage_users[row_date]))
This gives you the number of the week.
basically you need to use STARTOFMONTH and WEEKNUM together:
Here is a good video explaining it also:
https://www.youtube.com/watch?v=Oq5WOmo94_Q