PowerBI - Convert Date to the WeekNumber of the Month - powerbi

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

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/

DATEADD to calculate PYTD (Jan 1 till today's date) in DAX Power BI

I have a daily sales data for 3-4 years. I want to create the YTD and Prior Year To Date sales measure that will be updated daily. That is it should always be from beginning of the year (selected) to TODAY or the last day of the data (1 day lag from today and max date).
I used Sameperiodlastyear but it is problematic at the beginning of the month as it compares say Jan 1, 2022-June 8 2022 with Jan 1, 2021 with June 30, 2021.
Any suggestion how I can create a modified prior year to date measure to address this nuance?
This is a standard solution for the case. First, you get all dates, with a DATESYTD() function, for the current year or last visible year up today or last visible day, then you offset it.
SAMEPERIODLASTYEAR(DATESYTD(‘Date’[Date]))
It is equal to
DATEADD(DATESYTD(‘Date’[Date]),-1,YEAR))
Try this if you want to get exact days set:
VAR FirstDayThisYear =
SAMEPERIODLASTYEAR(STARTOFYEAR(‘Date’[Date])
VAR LastDayThisYear =
SAMEPERIODLASTYEAR(
LASTDATE(‘Date’[Date])
)
VAR SetOfDates=
DATESBETWEEN(
‘Date’[Date]
,FirstDayThisYear
,LastDayThisYear
)
RETURN
SetOfDates

Power Bi DAX: Total for the month

I need to make a total for each of the months based on the working days.
So far i have working days set as 1 if the day in the month is during Monday - Friday and 0 for Saturday and Sunday. I now need to total up the 1's and make it so that it is a single value for the month.
E.g Going the weekdays is 1 and weekend is 0, January will have 22 days on each row on the table in the data mode - January 1 = 232 January 2 = 22 etc. so i can use it to divide against my target which is set to the 1st of every month.
How can i do this??
try this. i haven't tested it.
=GROUPBY( FILTER(table, table[Working_Day]=1) , table[Month], ["new col", SUMX( CURRENTGROUP(), table[Month_Order])])
filter gives working day 1's data as table,then group by performed by month, then month order has been added and returns table.

Grand Total not working for sales per day last year function

I am trying to do a sales by day comparison where I compare the sales this year with the sales of the same day last year as a date of the week.
So I would be comparing Monday March 25 2019 with Monday march 24 2018 etc
Here is the formula I’m using for last year’s sale
Amount per Day LY = CALCULATE([Amount TY], FILTER(all(Dates), Dates[Date] = MAX(Dates[Date])-364))
However, my total isn’t working right for my sales last year. It will just be the total for 1 day (and that day seems to change as my date range increases)
Because you are having a one to one, you can do this with LookupValue:
SalesLY = LOOKUPVALUE(Sheet1[Sales];Sheet1[Date];DATEADD(Sheet1[Date];-364;DAY))
However if you have more rows for the same date, this will not hold, in this case you need to sum all the dates together,
SalesLY = CALCULATE(SUM(Sheet1[Sales]);FILTER(Sheet1; DATEADD(Sheet1[Date];364;DAY)= EARLIER(Sheet1[Date])))
Using DateAdd fixed my problem
Amt per Day LY = CALCULATE([Amount TY], DATEADD(Dates[Date], -364,DAY))

What is the opposite of EOMONTH() in Power BI (DAX)

What is the opposite of EOMONTH()? I am trying to find the beginning date of 12 months ago. Would it be EOMONTH(Current Date,12)-1?
Any direction would be great. TIA
To find the beginning of a month, go to the end of the month before and add a date to it (not sure if I understand the question)
Start of month one year ago = EOMONTH(TODAY(),-13)+1
In general, the opposit function is STARTOFMONTH:
=STARTOFMONTH('Date'[Current Date])
If you want to get the first day of the month 12 months ago:
= STARTOFMONTH ( DATEADD ( 'Date'[Current Date], -12, MONTH ) )
This measure will return the first date of the same month a year ago (i.e., for 2018-09-22, it will return 2017-09-01). Current month is included, so you will get a total of 13 months. If you need only 12 months total:
= STARTOFMONTH ( DATEADD ( 'Date'[Current Date], -11, MONTH ) )
If you are trying to compute something over a rolling 12 months period, this article should help:
12-month Rolling Average in DAX
In DAX, you could also use
= DATE(YEAR(Dates[Date])-1, MONTH(Dates[Date]), 1)
This backs up one year, keeps the month, and returns the first day of that month.
There is no straight opposite function for EOMONTH() in DAX. But you can still use EOMONTH() to get the first date of a month.
Last date of a month
= EOMONTH(Leaves[LeaveStart]
First date of a month
= EOMONTH(Leaves[LeaveStart],-1)+1
You can also just use 1 in place of date to get first day of a month, like:
= DATE(YEAR(Leaves[LeaveStart]), MONTH(Leaves[LeaveStart]), 1)
Please note that STARTOFMONTH() is opposite function for ENDOFMONTH() not for EOMONTH().