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]))
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))
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 )
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
Hopefully someone can help me.
I use the measure below to output a value from the previous month, however it appears to have broken this month (January).
In March it outputs Februarys value, and in April it outputs Marchs value. However as it is January it doen't appear to be outputting Decembers value.
Can anyone advise how I would adapt this measure to retrieve Decembers value?
Previous Month = var current_month= MONTH(TODAY()) return CALCULATE( AVERAGE('DATA_TABLE'[VALUE]), FILTER(DATA_TABLE,MONTH(DATA_TABLE[DATE])=current_month-1))
Thanks for your support.
Dax's Month commmand returns a number. In your case, you're asking for month 0, as january is month number 1. Since there is no month numbered as 0, it returns no data (blank).
You just need to add a condition for when the month is 0, to instead return data for the previous year's last month.
This is a possible solution:
VAR current_month = MONTH(TODAY())
VAR current_year = YEAR(TODAY())
VAR previous_month = current_month - 1
VAR previous_year = current_year - 1
RETURN
CALCULATE(
AVERAGE('Table'[Values]);
FILTER('Table';
IF(previous_month = 0;
MONTH('Table'[Date]) = 12 && YEAR('Table'[Date]) = previous_year;
MONTH('Table'[DATE]) = current_month - 1)
)
)
This way you accomodate for the 0 month value and filter according to it.
If it is not 0, the measure behaves as before. Otherwise it filters for the last month of the previous year.
I want to calculate the sum of NetSales for the products that had sales this year (2019) but NOT sales last year (2018).
This is what I'm trying (and a million variations more similar to this):
NetSales CY Not LY =
VAR ThisYear= 2019
VAR YearBefore= 2018
VAR TabelaThisYear = SUMMARIZE(FILTER(SUMMARIZE('Facts';Facts[ArticleNo];Facts[InvoiceDate];Facts[NetSalesCurr]);YEAR('Facts'[InvoiceDate])=ThisYear && Facts[NetSalesCurr]>0);Facts[ArticleNo])
VAR TabelaYearBefore = SUMMARIZE(FILTER(SUMMARIZE('Facts';Facts[ArticleNo];Facts[InvoiceDate];Facts[NetSalesCurr]);YEAR('Facts'[InvoiceDate])=YearBefore && Facts[NetSalesCurr]>0);Facts[ArticleNo])
VAR ProdutosOnlyThisYear = EXCEPT(TabelaThisYear;TabelaYearBefore)
RETURN
CALCULATE(SUM(Facts[NetSalesCurr]);ProdutosOnlyThisYear)
I would use the following approach, where we don't hardcode the years:
NetSales CY Not LY=
CALCULATE(
SUM(Facts[NetSalesCurr]),
FILTER(
VALUES(Facts[ArticleNo]),
CALCULATE(
COUNTROWS(Facts),
PREVIOUSYEAR(Calendar[Date])
) = 0
)
)
Let's break it down:
The inner FILTER function iterates through VALUES(Facts[ArticleNo]) - that is all ArticleNo's that have sales in the currently selected period.
For every one of those ArticleNo's, we calculate the number of rows on the fact table for the previous year. CALCULATE(COUNTROWS(Facts), PREVIOUSYEAR(Facts[InvoiceDate]))
We keep only those ArticleNo's where this number is = 0.
The list of ArticleNo's returned from FILTER is then used in the outer CALCULATE statement, so that we only get the sum of NetSales for articles that did not have any sales in the previous year.
For more information, please see the New and returning customers-pattern.
you can use the dax formula like at the below , if you have a calendar table ;
this year = CALCULATE ( SUM[measure];DATEADD(CALENDAR[DATE]),0,YEAR)
last year = CALCULATE ( SUM[measure];DATEADD(CALENDAR[DATE]),-1,YEAR)