What AVERAGEX function does in this context? - powerbi

I am trying to calculate monthly trend KPI over last 6 months. Whether it is positive or negative.
So I have columns: Current Month Premium, Previous Month Premium, Month Over Month
Then I am using AVERAGEX and DATESBETWEEN functions to find out whether trend is positive or negative.
WP 6M Trend =
VAR LastEffDate = LASTDATE(fact_Premium[EffectiveDate])
RETURN
AVERAGEX(
DATESBETWEEN(
dim_Date[Date],
DATEADD(STARTOFMONTH(LastEffDate), -5,MONTH), //creates rolling 6 months window [Ttl WP] and [PreviousMonthWP]
ENDOFMONTH(LastEffDate)
),
[MoM WP]
)
But I don't quiet understand how -72 exactly calculated here?
So I need to understand to know whether it is correct or not.

Your avergex is evaluating the last period in your dataset due the the fact that your LastEffDate calculates the last available date in a dataset or subset. As the total line refers to the entire context of all the data in your table, it computes the last date similarly to the last period you calculated, effectively the same calculation as the month august in this case.

Related

Measure Not Totaling Correctly

I have a measure that is comprised of other measures. I will post below pictures. I am calculating the forecast for the month per location. I created a measure for that and the total is correct for that. I created another measure that calculates the working days in a month and when I divide by this measure, I get the correct results per the location but the total is wrong.
This measure shows the measure I am using to calculate the total forecast for the month per location.
"MPP = (CALCULATE(SUM('TF'[ Forecast ]), DATESBETWEEN ('TF'[Date],[Month Start-pp], [Month end_pp] )))"
This measure shows the measure I am using to calculate the working days in a month.
"Days per location =
CALCULATE (
COUNTROWS ( 'TF' ),
FILTER (
'TF',
'TF'[Date].[MonthNo] = MONTH ( TODAY () )
)
)"
The pic is showing the totaling. The first column is the measure without the division and is correct totaling. The second column is where the issue is.

Compare Month to last years Month total

In power Bi, I am creating a report that compares this years data to last years. For example, I have January data for 2022 but I need to calculate all of the data for January in 2021. In the pic, I have CY (current year) data for January. The second matrix would display the Previous year data. the data in the PY pic is not correct. Can someone help me figure out a dax formula for it?
So the answer is going to depend on whether you have a date table in your model or not. But essentially, you need to change the filter context for the previous/last year measure using the CALCULATE function.
As an aside, I noticed your column names between the pictures were not really similar, i.e. Sales Order seems different than Actual Shipping. But in any case...
With a date table
PY Calculation = CALCULATE(
SUM( YourTable[YourColumn] ), // or some other calculation...
SAMEPERIODLASTYEAR( DateTable[Date] )
)
Without a date table
PY Calculation = CALCULATE(
SUM( YourTable[YourColumn] ), // or some other calculation...
SAMEPERIODLASTYEAR( YourTable[SomeDateColumn].[Date] )
)
Let me know if that worked for you. (And yes, there are other ways to accomplish the same thing.)

Power BI - DAX - Bizarre Behavior for YTD calculation

Trying to use a simple year to date (YTD) measure. But things are not working as expected.
Data Model
dim_date (the weekend column is incorrect, but doesn't affect of principle)
fact
The YTD measure is defined as the following:
YTD = CALCULATE(
SUM('fact'[value]),
DATESYTD(dim_date[Date])
)
which give me the following incorrect results for YTD.
It looks like instead of calculating the YTD for the weekend and weekday separate.
Given there are16 rows with that have a date that is label as a weekend as well as fact that all of them falls in the same year, I expect the YTD and valueto be identical in the above.
Interesting enough, if I slightly change the DAX by using the dim_date[Date].[Date] instead of just dim_date[Date], everything just works as expected.
YTD = CALCULATE(
SUM('fact'[value]),
DATESYTD(dim_date[Date].[Date])
)
Can anyone help to explain what is actually going on here?
The example .pbix file is here:
https://drive.google.com/open?id=1y3ndL7yDE7T4x7Z2bPhMsa-NHRgGzYj0
As dax.guide points out,
DATESYTD ( <Dates>[, <YearEndDate>] )
is equivalent to
DATESBETWEEN (
<DATES>,
STARTOFYEAR ( LASTDATE ( <DATES> )[, <YEARENDDATE>] ),
LASTDATE ( <DATES> )
)
So what's happening is that since Jan 10 is the last False value for weekend and Jan 12 is the last True value for weekend and the DATESBETWEEN function returns a continuous range not filtered by the weekend evaluation context, you get all dates up to Jan 10 in one case and all dates up to Jan 12 in the other.
To make the measure take into account the weekend value instead of calculating over a contiguous date range, you can add that as a filter:
YTD = CALCULATE(
SUM('fact'[value]),
DATESYTD(dim_date[Date]),
dim_date[weekend] IN VALUES(dim_date[weekend])
)

Months To date formula not working properly on Power Bi

I am newbie in Power Bi. I am calculating months to date of a measure.
I have written following DAX formula for that,
MTD in Sales = CALCULATE([Total Sales], DATESMTD(Dates[Date]) )
it shows me correct total sales value of this month.But when i make day-wise, it shows me some unrealistic value.
I have attached a screenshots of my result..Please have a look.
I don't understand what wrong are going on? Can you please find out the problem plz?
DATESMTD(Dates[Date]) is equivalent to:
CALCULATETABLE(
FILTER(
ALL(Dates[Date]),
AND(
Dates[Date] <= MAX(Dates[Date]),
AND (
YEAR(Dates[Date]) = YEAR(MAX(Dates[Date])),
MONTH(Dates[Date]) = MONTH(MAX(Dates[Date]))
)
)
)
)
This only considers the maximum value of the dates in the external filter context, so for Tuesday (today), it will contain every day of the month up to today, for Monday (yesterday) it will contain every day of the month up to yesterday and so on. (Assuming that no Sales are linked to future dates).
If you want to further filter this to only include sales that occurred on the given day of the week, I'd suggest altering MTD in Sales to:
[MTD in Sales] := CALCULATE([Total Sales], DATESMTD(Dates[Date]), Dates[DayOfWeekName])
This will additionally filter the included dates to only those with DayOfWeekName values present in the external filter context.

DAX Previous Month to date total is giving entire previous month's entire total

I am using DAX in Power BI to calculate Previous Month sales total to date to create a KPI visual. i.e. if today is 7th Dec then I want to get sales total from 1st Nov to 7th Nov and compare with current month to date.
CurrentMTD = TOTALMTD(SUM(SALES_VOUCHERS[SaleValue]),DatesTable[Date])
This works fine.
However Previous Month YTD gives me total for entire month of November. I have tried the following so far
PMYTD = totalmtd(sum(SALES_VOUCHERS[SaleValue]),dateadd(DATESMTD(DatesTable[Date]),-1,month))
and
PMYTD = CALCULATE(sum(SALES_VOUCHERS[SaleValue]),
DATESBETWEEN(DatesTable[Date],
FIRSTDATE(PREVIOUSMONTH(DatesTable[Date])),
LASTDATE(DATEADD(DatesTable[Date],-1,MONTH))))
Both return the same answer which is total for the entire previous month .
If I simply hardcode the start and end date in datesbetween version above, then I do get the desired result. But that is not the solution.
I have linked the fact table (Sales_VOUCHERS) to a DatesTable and as of now there are no other visuals on the report page.
Kindly assist what I am missing out on and how I can get Previous Month year to date total
If you're aggregating at the month level (i.e. you're looking at December 2016 vs. November 2016), then the measure you have above will show you the entire month of December compared to the entire month of November (and since December is a partial month and November isn't, it causes the mismatch you see).
If you filter to the current date (e.g. 7th Dec), then both your MTD and Prior Month MTD measures will only show you through the 7th of their corresponding months.
Assuming you don't want to filter to the day level (not unreasonable), you could enhance your formula to filter out future dates. For example:
PMYTD = totalmtd(
sum(SALES_VOUCHERS[SaleValue]),
dateadd(
FILTER(
DATESMTD(DatesTable[Date]),
DatesTable[Date]<TODAY()
),
-1,
month
)
)
This says, if the date is after today, don't pass it into the TOTALMTD calculation (so it will only calculate the first 7 days of the month, for example, if today is Dec 8th - even if you're looking at full months on your report).
Side note: you can also write your previous month measure to re-use your MTD measure rather than redefining it. In this way, if you ever change the MTD calculation, the previous MTD calculation automatically updates.
PMYTD = CALCULATE(
[CurrentMTD],
DATEADD(
FILTER(
DatesTable[Date],
DatesTable[Date]<TODAY()
),
-1,
MONTH
)
)
Useful Resources:
https://www.powerpivotpro.com/2016/01/year-to-date-in-previousprior-year/ (article that covers this problem and a variety of solutions)
https://community.powerbi.com/t5/Desktop/Compare-MTD-with-previous-period/td-p/24656 (forum discussion about the same problem)
http://community.powerbi.com/t5/Desktop/Time-Intelligence-TOTALMTD-vs-DATESMTD-vs-DATEADD/td-p/10088 (forum discussion about DATESMTD vs TOTALMTD)