Get year of end date within date between - powerbi

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

Related

IF 'AND-OR' ISFILTERED combination in DAX giving problems

Below is the sample dataset
The data has two slicers ( date and category ) shown below
I am writing a DAX Statement to multiply the sum(values) * 10 only if the date range is in the current year 2023.
The StartYear gives the start of the current year, firstD gives the lowest date from the date slicer.
Formula =
var new = sum(Test[Value]) * 10
var startyear = DATE(YEAR(TODAY()),1,1)
var firstD = CALCULATE( MIN( Test[Date]), ALLSELECTED(Test[Date]) )
return if( ISFILTERED(Test[Categories]) && firstD >= startyear, new, 0 )
Now when I filter dates to 2023, the total value should be 2300 but it shows as 0
However the DAX works when I select A or B
If we remove the ISFILTERED function then, it gives wrong value, the expected value is 0 because the start date is in 2022, but it shows 650
let me know if that is the right syntax
It looks like you are not using a separate calendar table to handle this, which you need!
In your very last example you have set your slicer to some time late 2022, but the minimum value of 'Test'[Date] for your selected category is in year 2023. Hint: set the slicer to e.g. 2022-12-14, this will include a 2022-date for Category A in your data.
Your measure behaves exactly how it is supposed to, in other words!
To fix this, you need to do the following:
Create a calendar table in your model, this should contain contiguous dates, which is necessary for the filtering method you want
Establish a relationship between the calendar table and existing Test table.
Use the date column from your new calendar table in your slicer and as date reference in your measure
Exactly how to create a calendar table is thoroughly documented on Google, I recommend you search and find an article or video you understand for implementing this.
Lastly: Your use of ISFILTERED in this measure seems strange, since you mention nowhere the requirement of only showing a number if the column you are testing filtering on is filtered, if that makes sense.. :-) The way you describe your calculation, you only need to check whether the selected date range starts in current year.

How to create Last 12 months flag for grouped data in power bi

I have a table where where I want to create last 12 months flag column from the latest date available in each country. I have tried many Dax formula without success.
My table looks like below:
Here Y indicates the dates falls under last twelve months.
Please help me to find this flag.
You can create a calculated column to validate the date falls within the last 12 months, for each country. First calculate the maxDate for each country, then ensure the date is greater than the MaxDate with an if statement
Last 12 Months =
VAR MaxDate = CALCULATE (
MAX ( Country[Date] ),
ALLEXCEPT ( 'Country', 'Country'[Country] )
)
RETURN
IF (
Country[Date]> DATE(YEAR(MaxDate)-1, Month(MaxDate), Day(MaxDate)) ,1,0
)
This will then give you a filterable column to work from

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.

Power BI Rolling Average

I've used the new Quick Measures feature of Power BI to build a 3 month rolling average calculation and it's working well. The equation is displayed below. However, when I try to use this metric in a time series visualization, the calculations are displaying three months past the current month, but I'd like for the calculation to stop at the current month.
I've played around with the __DATE_PERIOD variable to no avail. My date filter for the page is set to show all dates in the current months or 12 months prior via a calculated column on the date table.
Is anyone aware of how I can get the visualization to end at the current month?
Average Days to Close Rolling Average =
IF(
ISFILTERED('Date'[Date]),
ERROR("Time intelligence quick measures can only be grouped or filtered by the Power BI-provided date hierarchy."),
VAR __LAST_DATE =
ENDOFMONTH('Date'[Date].[Date])
VAR __DATE_PERIOD =
DATESBETWEEN(
'Date'[Date].[Date],
STARTOFMONTH(DATEADD(__LAST_DATE, -3, MONTH)),
__LAST_DATE
)
RETURN
AVERAGEX(
CALCULATETABLE(
SUMMARIZE(
VALUES('Date'),
'Date'[Date].[Year],
'Date'[Date].[QuarterNo],
'Date'[Date].[Quarter],
'Date'[Date].[MonthNo],
'Date'[Date].[Month]
),
__DATE_PERIOD
),
CALCULATE(
'Closed Opportunities'[Average Days to Close],
ALL('Date'[Date].[Day])
)
)
)
In order to limit what is displayed within your chart, you need to filter the applicable date field so it only displays the dates you desire. In this case, you only want it to include dates <= today.
In order to automatically filter it when it is refreshed, I typically add a custom DAX column to the date table that I can filer on. In this case it would be something along the lines of:
excludeFutureDatesInd = 'Date'[Date] <= TODAY()
You can then add a visual, page, or report filter selecting all dates where [excludeFutureDatesInd] = True.
Not sure if you're still having issues with this, but I'd like to share a hack fix for those landing here. I fixed this issue by filtering on the base data (in your example, this would be "Average Days to Close"). Set a visual-level filter to include only those items where Average Days to Close > 0, and you should get the extra dates cut off the end of the graph.
So long as all of your base data passes through the filter, you should be good.