Last month average constant in Power-Bi - powerbi

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

Related

How to show previous month's growth rate, that also takes care of year change and Jan-Dec in Power BI using DAX?

I want to calculate and show month on month growth rate in power bi, in both visual - Table and scorecard. In Table all the month should have their respective growth rate, except for current month (in some cases, otherwise can consider current month as well). And in scorecard if no month is selected from the slicer, current/previous month's growth rate should be visible. I had no issue before the year change.
Previously I used the following DAX to get the output:
MoM = VAR CurrentMonth = MONTH(TODAY())
VAR CurrentYear = YEAR(TODAY())
VAR LastYear = CurrentYear-1
VAR CurrentMonthPrice = CALCULATE([TotalPrice], FILTER(AllCommodities, AllCommodities[Year]=CurrentYear && MONTH(AllCommodities[Date])= CurrentMonth))
VAR LastYearMonthPrice = CALCULATE([TotalPrice], FILTER(AllCommodities, AllCommodities[Year]=LastYear && MONTH(AllCommodities[Date])= CurrentMonth))
VAR growth = DIVIDE(CurrentMonthPrice,LastYearMonthPrice)-enter image description here1
RETURN
IF(SELECTEDVALUE(AllCommodities[Commodities])="NA", "-", growth)
But due to year change, I'm unable to calculate the M-o-M rate. I tried using the following formula:
New MoM =
DIVIDE(
SUMX(FILTER(ALLSELECTED(trail), MONTH(trail[Date]) = MONTH(SELECTEDVALUE(trail[Date],NOW())) && YEAR(trail[Date]) = YEAR(SELECTEDVALUE(trail[Date],NOW()))), trail[Revenue]),
SUMX(FILTER(ALLSELECTED(trail), MONTH(trail[Date]) = IF(MONTH(SELECTEDVALUE(trail[Date],NOW()))=1,12,MONTH(SELECTEDVALUE(trail[Date],NOW()))-1) && YEAR(trail[Date]) = IF(MONTH(SELECTEDVALUE(trail[Date],NOW()))=1,YEAR(SELECTEDVALUE(trail[Date],NOW()))-1,YEAR(SELECTEDVALUE(trail[Date],NOW())))), trail[Revenue])) - 1
But it is only good for table visual and if a particular month is selected, it gives -100% error. which should not happen and give that particular month's growth rate.
How do I modify my DAX ?
Old DAX :
MoM = VAR CurrentMonth = MONTH(TODAY())
VAR CurrentYear = YEAR(TODAY())
VAR LastYear = CurrentYear-1
VAR CurrentMonthPrice = CALCULATE([TotalPrice], FILTER(AllCommodities, AllCommodities[Year]=CurrentYear && MONTH(AllCommodities[Date])= CurrentMonth))
VAR LastYearMonthPrice = CALCULATE([TotalPrice], FILTER(AllCommodities, AllCommodities[Year]=LastYear && MONTH(AllCommodities[Date])= CurrentMonth))
VAR growth = DIVIDE(CurrentMonthPrice,LastYearMonthPrice)-1
RETURN
IF(SELECTEDVALUE(AllCommodities[Commodities])="NA", "-", growth)
But it doesn't works for year change.
New DAX:
New MoM =
DIVIDE(
SUMX(FILTER(ALLSELECTED(trail), MONTH(trail[Date]) = MONTH(SELECTEDVALUE(trail[Date],NOW())) && YEAR(trail[Date]) = YEAR(SELECTEDVALUE(trail[Date],NOW()))), trail[Revenue]),
SUMX(FILTER(ALLSELECTED(trail), MONTH(trail[Date]) = IF(MONTH(SELECTEDVALUE(trail[Date],NOW()))=1,12,MONTH(SELECTEDVALUE(trail[Date],NOW()))-1) && YEAR(trail[Date]) = IF(MONTH(SELECTEDVALUE(trail[Date],NOW()))=1,YEAR(SELECTEDVALUE(trail[Date],NOW()))-1,YEAR(SELECTEDVALUE(trail[Date],NOW())))), trail[Revenue])) - 1
But it doesn't work with scorecard or if a month is selected from the slicer , it gives -100% error.
[![Output required][1]][1]
[1]: https://i.stack.imgur.com/7FIV3.png

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

Rolling Average In PowerBI Exclude Current Month

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

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)

how to select last day (max day) of each month using dax

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