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().
Related
I have various needs for time calculations, but can't get the basic syntax right. The measure
First Day of Year = STARTOFYEAR('Calendar'[Date])
returns 1/1/2019, which is the earliest date on my calendar table. Today's date being Jun 2022, I want it to return 1/1/2022.
Related question: In order to return the first day of the fiscal year (October 1 of the prior year) I would expect something like
First Day of Fiscal Year = STARTOFYEAR(DATEADD('Calendar'[Date],-3,MONTH))
But this returns the same result, 1/1/2019. Desired result is 10/1/2021
If you want it reference this (the current) year, you need the TODAY() function.
eg:
First Day of Year = date(year(TODAY()),1,1)
First Day of Fiscal Year = date(year(today())-1,10,1)
if you want to get the first Date of the year of a table containing more Years you have to filter the date Table:
CALCULATETABLE(STARTOFYEAR('Calendar'[Date]), 'Calendar'[Year]=year(today()))
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
I have a quick question, is there a way to filter data from set days between 2 months?
E.g filter from 16-15 of Jan-Feb.
Is this possible?
For example i have used a measure to calculate days between dates
Last Assessment 16-15
= CALCULATE(SUM('Table1'[Duration1]),'Table1'[Start],
DATESBETWEEN('Calendar'[Date], [Assessment Date], [Assessment one month]))
Assessment Date = if(DAY(TODAY())<16,DATE(YEAR(TODAY()),MONTH(TODAY())-1,15),
DATE(YEAR(TODAY()),MONTH(TODAY()),15))
Assessment one month = EDATE([Assessment Date],-1)+1
Assessment 6 = EDATE([Assessment Date],-6)+1
Assessment 12 = EDATE([Assessment Date],-12)+1
The last assessment does show from the 16th of 2 months ago to last months 15th e.g Dec 16th - Jan 15th.
But i need to show from last 6 months and the last 1 year.
How can i work this out so i can show the Last 6 months and 1 year.
So far i have had to use a date filter to manually select the dates which i want to stop and have it be automatic.
If it is just the last 6 months or the last year you could make a custom column in the query editor (this would be the easiest way then). Like a filter flag:
'Includes the current month
Last 6 Months Flag = Date.IsInPreviousNMonths([YourDate], 6) or Date.IsInCurrentMonth([YourDate])
'Without the current month
Last 6 Months Flag = Date.IsInPreviousNMonths([YourDate], 6)
The same for last year:
Last Year Flag = Date.IsInPreviousYear([YourDate])
Drag and drop these custom columns as filter on your report and you are done.
consider following measure for Latest assessment (basically what you had before with EDATE for safe Dec/Jan handling and with variables for better performance)
Latest Assessment Date =
VAR __today = TODAY()
RETURN
IF (
DAY ( __today ) < 16,
DATE ( YEAR ( __today ), MONTH(EDATE(__today, - 1)), 15 ),
DATE ( YEAR ( __today ), MONTH ( __today ), 15 )
)
then you have this measure for handling 1/6/12 scenarios, you just replace 12 with whatever number of months you need
Measure 12 =
VAR __nrOfMonthsBack = 12
VAR __lastAssesment = [Latest Assessment Date]
RETURN
CALCULATE (
SUM ( Table1[Duration] ),
ALL('Calendar'), --this will reset any date filters coming from slicers
DATESBETWEEN (
'Calendar'[Date],
EDATE ( __lastAssesment, - __nrOfMonthsBack ) + 1,
__lastAssesment
)
)
EDIT: added ALL('Calendar') to disable filters coming from slicers
I have the start of every month in a table. What I am wanting to know is, how do you find the last day of the month if you have the starting month?
In other languages I would take the start date of a month, add a month and then subtract 1 day. How do you do this in Power BI?
In my example below, you will see the starting month in the Month field.
You can use EOMONTH function to calculate that.
=EOMONTH([Month], 0)
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