Rolling Average In PowerBI Exclude Current Month - powerbi

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

Related

Calculate the total annual sum for the previous year in Power BI

How can I calculate the total annual sum for the last year? Specifically, I need to calculate last year's total sales.
Here's what I do:
total_sales_last_year =
CALCULATE(
SUM(fact_sale_order[sales_amount]),
DATEADD(dim_date[formatted_date], -1, YEAR),
REMOVEFILTERS(dim_date[month_name])
)
However, if for example, I filter for January, in sales_last_year I won't get the sales of the whole year, but I will only have those of January 2021. The result I'm looking for is the total of the previous year.
Solution:
total_sales =
VAR last_year = YEAR(MAX(dim_date[formatted_date]))-1
VAR sales_last_year =
CALCULATE(
SUM(fact_sale_order[sales_amount]),
FILTER(ALLEXCEPT(dim_date, dim_date[year]), YEAR(dim_date[formatted_date]) = last_year )
)
VAR sales_current_year =
CALCULATE(
SUM(fact_sale_order[sales_amount]),
DATESYTD(dim_date[formatted_date])
)
RETURN sales_last_year + sales_current_year

Power BI Compare Sales amount for today with same day last year

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() )

Last month average constant in Power-Bi

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

DAX Row Total is Wrong For Sum Date Difference

I have table which includes start_date and end _date. I want to get date difference between these columns by minute. There is also date and time slicer and if max date slicer is smaller than end_date, I have to get date difference between start date and max date slicer + max time slicer.
I created a measure about this but row total is not true. How can I fix it?
this is my measure I wrote;
Zaman 3 =
var MaxDateTime =MAX(vwDimDate[Date]) + TIME(HOUR(MAX(vwDimTime[Time])), MINUTE(MAX(vwDimTime[Time])), SECOND(MAX(vwDimTime[Time])))
var sonuc1 = SUMX(vwFactATM_STATUS_LOG,DATEDIFF(vwFactATM_STATUS_LOG[STARTING_DATE],vwFactATM_STATUS_LOG[END_DATE],MINUTE))
var sonuc2 = SUMX(vwFactATM_STATUS_LOG,DATEDIFF(vwFactATM_STATUS_LOG[STARTING_DATE],MaxDateTime,MINUTE))
var deger = IF(MaxDateTime>MAX(vwFactATM_STATUS_LOG[END_DATE]),sonuc1,sonuc2)
//var a=MAX(vwFactATM_STATUS_LOG[END_DATE])
return deger
And table output;
As You see row total is not true. How can I fix it?
I solved the problem. We need calculate inside "if block" look I how I corrected
Var MaxDate=CALCULATE(MAX(vwDimDate[Date]) + TIME(HOUR(MAX(vwDimTime[Time])), MINUTE(MAX(vwDimTime[Time])), SECOND(MAX(vwDimTime[Time]))),ALLSELECTED(vwDimDate),ALLSELECTED(vwDimTime))
VAR Tarih=Sumx(
vwFactATM_STATUS_LOG, If(MaxDate<vwFactATM_STATUS_LOG[END_DATE], DATEDIFF(vwFactATM_STATUS_LOG[STARTING_DATE],MaxDate, MINUTE) , DATEDIFF(vwFactATM_STATUS_LOG[STARTING_DATE],vwFactATM_STATUS_LOG[END_DATE],MINUTE) ))
return Tarih

Calculate sales of products that sold this year but not the year before

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)