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])
)
Related
In the following Power BI file, I have defined a calculation group 'CG - Last days'[Name] = "5 last days" as follow :
VAR tbl_FILTER =
TOPN(
10
, dim_DATES
, dim_DATES[Date]
, DESC
)
RETURN
CALCULATE(
SELECTEDMEASURE()
, KEEPFILTERS( tbl_FILTER )
)
My problem is it does not produce the expected output :
filtering on the 10 most recent dates of a date table with only January and February should return a count of 10 dates only for the month of February ; but that is not what I get :
If anyone knows why, I am all ears.
One problem here is that calculation items don't modify just any DAX expression, only measure references. Try defining a count measure COUNT( dim_DATES_2[dat_DATE] ) and using that in your test instead.
Please see this article: https://www.sqlbi.com/articles/understanding-calculation-groups/
In particular, this part
The application of a calculation item replaces a measure reference with the expression of the calculation item, still applying an implicit context transition. Focus your attention on this sentence: A measure reference is replaced. Without a measure reference, a calculation item does not apply any modification. For example, the following code is not affected by any calculation item because it does not contain any measure reference:
CALCULATE (
SUMX ( Sales, Sales[Quantity] * Sales[Net Price] ),
'Time Intelligence'[Time calc] = "YTD"
)
Edit: As mentioned in the comments, you also need to use ALL(dim_DATES) instead of dim_Dates inside the TOPN. This is because the dim_Dates has filter context applied to it from the visual, so only includes February dates in the February row and Januari dates in the Januari row. ALL removes this filter context and returns the top dates from the unfiltered dates table.
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.)
I use as a filter date between.
I would like to get the year of the end date. I try as measure yearmax=year(max(date)) but it returns wrong values. I need to extract only the year of the end date. For example, when the user select as date range 01/03/2020 till 04/04/2027, I need to get only the year of the end date and have 2027.
I put the pbix file here, any help would be appreciated.
https://drive.google.com/file/d/1AqrdiHBQdYjrQImP9AGO_sC7Duh9Yeb5/view?usp=drivesdk
This looks like a Power BI slicer visual bug.
You set filter into the slicer to restrict the selection over the last 90 days including TODAY. When selecting any day up to yesterday, your YEAR(MAX('Date'[Date])) measure works fine. But when you push the slicer up to today, then even if the slicer shows the correct date of today in the upper bound, the measure returns the last year in the date table, like when no filter exists. To fix that you might add a filter to your measure to limit the upper bound to TODAY() like follows
Year end date =
VAR MaxDate =
CALCULATE ( MAX ( 'Date'[Date] ), KEEPFILTERS ( 'Date'[Date] <= TODAY () ) )
RETURN
YEAR ( MaxDate )
Of course, maybe that the problem was just the existence of the filter in the slicer, and by removing it the original measure works ad desired
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.
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.