I am trying to find a way to calculate the sum of a value for the last n days of the previous year.
For example:
Lets assume n = 1
today's date = 31.05.2017, then my last date is 30.05.2017(as n=1) then the sum for 31.05.2016 should be calculated.
Lets assume n = 5
today's date = 31.05.2017, then my last 5 days would be from 25.05.2017 to 30.05.2017 and then the sum for previous year last 5 days 25.05.2016 to 30.05.2016 should be calculated
Hope somebody can help me. Thanks.
Here is some hints for generating the start and end date using DAX-
start_date =
var n = 5
return TODAY() - n - 365
end_date =
var n = 5
return TODAY() - 1 - 365
You can create them both inside a DAX and store them in 2 variable. Then calculate your desired value using the date range from start to end date.
Related
I am working in Power-Bi desktop. I need to compute a constant measure (same for all rows) that indicates me the daily average of the amounts stored in a column named "Values" considering the last month as window. The distinct-count for the days of the last month has to exclude the last two days of the week. The window has to start Today() - [DELTA], where DELTA is a constant number of days.
An example below (which is rolling, not constant, and does not include DELTA):
dailyAverageValue =
var nobs = CALCULATE(DISTINCTCOUNT(TABLE[DATE]), PREVIOUSMONTH(TABLE[DATE]), TABLE[WEEK_DAY_NO] <> 6, TABLE[WEEK_DAY_NO] <> 7)
var summ = CALCULATE(SUM(TABLE[VALUES], PREVIOUSMONTH(TABLE[DATE]))
return summ/nobs
dailyAverageValue =
var nobs = CALCULATE(DISTINCTCOUNT(TABLE[DATE]), DATESINPERIOD(TABLE[DATE], TODAY() - [DELTA], -[WINDOW], DAY))
var summ = CALCULATE(SUM(TABLE[VALUES]), DATESINPERIOD(TABLE[DATE], TODAY() - [DELTA], -[WINDOW], DAY))
return summ/nobs
I have created a rolling average of total sales for the past 3 months. I want it to exclude the current month. I think my answer may be to combine the following measure I wrote with the following logic but I am having some challenges making it work. Could you anyone assist?
Again my goal is to exclude the current month. An example would be for this month calculate Monthly Average of Oct-Dec of 2019
Moving AVG Measure:
Moving X Months AVG = SUMX(DATESINPERIOD(DSS_DATA[Run_Date],LASTDATE(DSS_DATA[Run_Date]),-3,MONTH),[Total Internal Samples])/3
VAR LastDayofPrevMonth = DATEADD(STARTOFMONTH('Calendar'[Date]), -1, DAY)
VAR FirstDayofLast3Month = DATEADD(STARTOFMONTH(LastDayofPrevMonth), -2, MONTH)
Any help would be much appreciated
You can create the start and end date variable using TODAY fubnction and use that as a filter to calculate the running average:
Column 2 =
VAR Start1 =
DATE(
IF(MONTH(TODAY())<=3,YEAR(TODAY())-1,YEAR(TODAY())),
IF(MONTH(TODAY())=1,12,IF(MONTH(TODAY())=2,11,IF(MONTH(TODAY())=3,10,MONTH(TODAY())-1))),
1)
VAR End1 =
DATE(
YEAR(TODAY()),
MONTH(TODAY()),
1)-1
VAR Average1 =
CALCULATE(
AVERAGE(DSS_Data[Total Internal Samples]),
FILTER(DSS_Data,DSS_DATA[Run_date]>=Start1 && DSS_Data[Run_date]<=End1))
RETURN Average1
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)
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]))