Lets just say that I want to compare todays data (3/10/2021) against the data of the last day of the previous month (2/26/2021), I am using February 26 because is the last business day of the previous month, Is there a Way to do this?
We can get the last day of February that is not a weekend (DAX doesn't have holidays, because each country can have its own free days).
calcForLastDayPrevMonth_noWeekend =
var __firstOfMonth = DATE( YEAR(TODAY()), MONTH(TODAY()), 1)
var __lastDayPrevMonth = calculate( MAX(DateTable[Date]), FILTER(ALL(DateTable), DateTable[Date] < __firstOfMonth && WEEKDAY(DateTable[Date]) not in {1,7} ))
return
calculate( sum(Table[SomeVal]), Table[date] = __lastDayPrevMonth )
Related
Here's my DAX Function that gives me my total amount (say sales) for this month:
Current Month =
CALCULATE(
SUM (Budget[Amount]),
FILTER(
ALL ('Calendar'),
'Calendar'[MonthDate]= DATE(YEAR(TODAY()), MONTH (TODAY()),1)
)
)
How would I be able to edit this to give me the amount for last month, so that I can compare Month-over-month?
You can try this
Previous Month = CALCULATE (SUM (Budget[Amount]),
FILTER (ALL ('Calendar'),
'Calendar'[MonthDate]= DATE (YEAR ( TODAY ()), MONTH (TODAY () ) , -1 )))
To compare the current month sales with last month, you can store the current month in a variable like below:
Last_Month_Sales = var Current_Month= MONTH(TODAY()) return CALCULATE(SUM(Budget[Amount]),FILTER(ALL ('Calendar'),
'Calendar'[MonthDate]=Current_Month-1))
I have a measure showing Sales Amount for the same period year:
Sales LY = Calculate (Sum('Sales'[Product Sales]), SAMEPERIODLASTYEAR('Sales'[Payment Date].[Date]))
This measure gives me the Sales amount for same date last year. I am looking for a measure which will give me the Sales amount for same day last year.
So January 1st of 2021 was a Friday. So I need it compared to the first Friday in 2020.
Any assistance or guidance please!
You may try to check the first day of the current row year (and last year). January 1st of 2021 = Friday; January 1st of 2020 Wednesday; Diff between this two is 2. We can use this in our calculation. Example:
LastYearTheSameDay = var _firstDayOfYear = WEEKDAY(CALCULATE(MIN('Sample'[Date]), filter(ALL('Sample'), YEAR('Sample'[Date]) = YEAR(SELECTEDVALUE('Sample'[Date])))),1)
var _firstDayOfLastYear = WEEKDAY(CALCULATE(MIN('Sample'[Date]), filter(ALL('Sample'), YEAR('Sample'[Date]) = YEAR(SELECTEDVALUE('Sample'[Date]))-1)),1)
var _diff = _firstDayOfYear - _firstDayOfLastYear
var _corrDay = DAY(SELECTEDVALUE('Sample'[Date])) + _diff
return
if(HASONEVALUE( 'Sample'[Date]),
calculate( sum('Sample'[VAL]), filter(ALL('Sample'), 'Sample'[Date] = DATE(YEAR(SELECTEDVALUE('Sample'[Date]))-1,Month(SELECTEDVALUE('Sample'[Date])),_corrDay) ))
, BLANK() )
I need date functions as below.
Last month of a specific date selected and the same day last week.
Example
4th day of last week = today 2020-12-02 return 2020-11-25
same day last month = today 2020-12-02 return 2020-11-04
Can you give an example?
For the same week day last week it's easy. Since the DATE is kept as an integer representing the days elapsed since the 12/30/1899, it's enough to subtract 7 from the current date.
Same Week Day Last Week =
VAR CurrentDay = MAX('Date'[Date])
RETURN CurrentDay - 7
The same week day last month measure is lacking a precise definition.
It's not possible to map weeks over months and for this reason week based calendars exist.
Here is a DAX measure that tries to keep most of the days inside the month
Same Week Day last month =
VAR CurrentDay =
MAX ( 'Date'[Date] )
VAR WeekDaysDelta =
WEEKDAY ( EOMONTH ( CurrentDay, -1 ) ) - WEEKDAY ( EOMONTH ( CurrentDay, -2 ) )
VAR DaysAdjust =
IF (
WeekDaysDelta > 3,
WeekDaysDelta - 7,
IF ( WeekDaysDelta < -3, WeekDaysDelta + 7, WeekDaysDelta )
)
VAR SameWeekDay =
EOMONTH ( CurrentDay, -2 ) + DAY ( CurrentDay ) + DaysAdjust
RETURN
SameWeekDay
There is an ISO week date standard that defines week based calendars.
Here is a link to an article about working with weeks in DAX
Week-related calculations
I need help creating a measure that will count the total merch booked from a previous working day.
I currently have the following:
dimDate table
A. This table contains following:
i. Date Column
ii. Dayofweek column: 1=Sunday, 2=Monday, 3=Tuesday, 4=Wednesday, 5=Thursday, 6=Friday, 7=Saturday
iii. Working Day column: indicates whether it is a "Working" or "Non-Working" day based on the combination of "Dayofweek" and "Holiday" Column.
Total Merch Booked measure
Here are the conditions that this previous day measure should follow:
Weekday (TODAY()) = 2 (Monday), then it should look whether the Friday before was a working day, if so, then it should calculate Total Merch booked on that day, otherwise keep repeating to the previous day to it until it finds a working day and then calculate Total Merch Booked.
Weekday(TODAY()) =1 OR Weekday (TODAY()) =7 (Saturday or Sunday), then skip and do not calculate the Total Merch booked.
Weekday(TODAY()) = any other number besides 1, 2 or 7 (Tuesday thru Friday), then it should look at the previous day to see if it is a Working day, if so, then it should calculate Total Merch booked on that day, otherwise going in backwards until it finds a working day and then calculate Total Merch booked.
I tried to use the following, but i also need to count 'holidays' in to the mix:
IF(
WEEKDAY(TODAY()) = 2,
CALCULATE(
[Total Merch Booked],
'dimDate'[Date]= (TODAY()-3)
),
IF(
WEEKDAY(TODAY()) = 1,
BLANK(),
IF(
WEEKDAY(TODAY()) = 7,
BLANK(),
CALCULATE(
[Total Merch Booked],
'dimDate' [Date] = (TODAY()-1)
)
)
)
)
This is really difficult to suggest without sample data and expected output. But you can you try with this below measure-
total_merch_booked =
VAR previous_working_day =
CALCULATE(
MAX('dimDate'[Date]),
FILTER(
ALL('dimDate'),
'dimDate'[Date] < TODAY()
&& 'dimDate'[Working Day] = "Working"
)
)
RETURN
IF(
WEEKDAY(TODAY()) IN { 1, 7 },
BLANK(),
CALCULATE(
[Total Merch Booked],
FILTER(
ALL('dimDate'),
'dimDate'[Date]= previous_working_day
)
)
)
I have the following table:
I need to make a measure to return the values of "last day", but I cant use EOMONTH, because I have the current month, and the current month doesnt end yet, so, the last day of current month is today.
You can use EOMONTH with a bit of extra logic:
LastDay =
VAR CurrDate = MAX(Table1[Date])
RETURN CALCULATE(MAX(Table1[Date]),
FILTER(ALL(Table1),
Table1[Date] > EOMONTH(CurrDate, -1) &&
Table1[Date] <= EOMONTH(CurrDate, 0)))
This takes the max of the dates you have that occur between the end of the previous month and the end of the current month.
Once you have this measure, you can use it to calculate the sum of Value:
Last Date Sum = CALCULATE(SUM(Table01[Value]),
FILTER(Table01, Table01[Date] = [LastDay]))