DAX DateDiff with slicer - powerbi

Need help with DAX Syntax in what I am trying to accomplish. Here is what I have currently tried
Number of Months = ABS(DATEDIFF(myLeas[RENTDATE],TODAY(),Month))
The Problem is, in place of "Today" i need to pass the date coming from the Slicer on the visual. How do I do this?

Replace TODAY() with:
SELECTEDVALUE ( Table[Column] )
where Table[Column] is the column you have put on the slicer.
So your measure will be:
Number of Months =
ABS (
DATEDIFF (
myLeas[RENTDATE],
SELECTEDVALUE ( Table[Column] ),
MONTH
)
)
Search for additional tips:
capture slicer value pbi
For further tips:
https://powerpivotpro.com/2018/02/using-selectedvalues-capture-power-bi-slicer-selections/

Since it is a date slicer, you probably have start and end dates. In this case SELECTEDVALUE might not work. You could try using variables in this case:
Months =
VAR MAX_DATE = MAXX(ALLSELECTED(myLeas),myLeas[RentDate])
Return ABS(DATEDIFF(Table[RentDate],MAX_DATE,MONTH))
This should give the value you are looking for. Hope this helps.

Related

Remove visual filter context on dates but keep slicer filters on

I have two tables like this
I am trying to get NAV of the first date (in the selected dates). I tried multiple ways but couldn't get it. Here are a couple of expressions I tried. Any help would be greatly appreciated.
NAV First Date = CALCULATE(MIN(FundNAV[NAV]),ALLEXCEPT(FundNAV,FundNAV[Fund Name]),FIRSTDATE(Dates[Date]))
NAV First Date = CALCULATE(MIN(FundNAV[NAV]),FILTER(ALL(Dates[Date]),Dates[Date]=MIN(Dates[Date])))
I got it working with the below measure, but have a bug in the expression. As you can see for the third fund there is no record on the selected first date, so it's returning blank, ideally, it should return the next available NAV for all remaining days.
NAV First Date = VAR frist_date = CALCULATE ( FIRSTDATE( Dates[Date] ),ALLSELECTED(FundNAV),VALUES(FundNAV[Fund Name]))
RETURN CALCULATE(MIN(FundNAV[NAV]),Dates[Date]=frist_date)
Try this:
NAV First Date =
VAR mindate =
CALCULATE ( MIN ( FundNAV[NAV Date] ), ALLSELECTED ( Dates[Date] ) )
RETURN
CALCULATE ( MIN ( FundNAV[NAV] ), ALL(FundNAV[NAV Date]), FundNAV[NAV Date] = mindate )
This measure first calculated the minimum FundNAV[NAV Date], within the selected dates from the Dates tabel. Then it returns the FundNAV[NAV] for that date.
Please test below 2 measures. I think Both will solve your problems:
NAV First Date =
CALCULATE ( MIN ( FundNAV[NAV] ), ALL ( FundNAV[Fund Name]), ALL ( Dates[Date] ) )
and This one uses the entire fact table as filter. (Expanded tables logic. it also removes any filter on dates)
NAV First Date =
CALCULATE ( MIN ( FundNAV[NAV] ), ALL ( FundNAV ) )

Power BI count the occurrence of a value in a measure

I'm trying to count the occurrence of a value in a measure. I wonder if it is possible? This is what I have tried so far. [EUI Yearly (kbtu/ft2)] is a measure I made.
EUI Count =
CALCULATE (
COUNTROWS ( df ),
ALLSELECTED ( df ),
VALUES ( df[EUI Yearly (kbtu/ft2)] )
)
It returns an error: Column 'EUI Yearly (kbtu/ft2)' in table 'df' cannot be found or may not be used in this expression.
Any fix to this? Thanks in advance!
Try countx method, accept if help :)
CountX = COUNTX(
FILTER(Sheet1,EARLIER(Sheet1[item])= Sheet1[item]),
Sheet1[item])

Get year of end date within date between

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

PowerBI DAX - Using a measure as a filter within a CALCULATE function

I want to create a DAX measure in PowerBI that will provide an aggregate of sales on a specific date.
I need that date to be controlled by a date slicer. Specifically the the maximum date on the slicer.
I would expect this to be a Calculate() function. So something like the following if it was hard coded with a date.
=CALCULATE(SUM(FactInternetSales2[Sales]), DimDate2[Dim Date] = DATE(2018, 06, 18))
But I need the filter component of the the Calculate() function (i.e , DimDate2[Dim Date] = DATE(2018,06,18)) to be dynamically populated from the max date on the date slicer. I understand, however that a measure can't be used as a filter in a calculate function - so I can't create a measure such as follows to identify the maximum date
=LASTDATE(DimDate2[Dim Date])
and then use it in the Calculate() function such as
=CALCULATE(SUM(FactInternetSales2[Sales]), DimDate2[Dim Date] = LASTDATE(DimDate2[Dim Date])
Can anyone outline how I can use the maximum date from the slicer to filter the Calculate() function, or achieve the same outcome?
A copy of my working file is located here
https://drive.google.com/file/d/1d1JiyPm1jOD9XkVqv3Q5pm0vk1FMotH9/view?usp=sharing
Cheers
Steve
You can read in the parameter value into a variable.
SalesSum =
VAR EndDate = LASTDATE ( DimDate2[Dim Date] )
RETURN
CALCULATE ( SUM ( FactInternetSales2[Sales] ), DimDate2[Dim Date] = EndDate )
You can use the Calculate() function with a filter like this:
=CALCULATE(SUM(Table1[SalesValue]), FILTER(Table1, Table1[Year] = 2019))
To get the value from a slicer you go to Modeling > New Parameter. Here you can specifie your parameter needs and hit OK. Now you gat a new table and column for this parameter on the fields pane. Just reference now on this column with the following code:
=SELECTEDVALUE(ParameterTable[ParameterValue])

DAX Last Year to Date

So I know this question has been asked a few times, and I've religiously looked over different approaches, however I still don't quite understand why I'm getting an incorrect result.
Case: I have Sales Data from ~2016 -> 2019 (up until the 2/18/2019) I'm have a Measure to show me the YTD, however I'm looking for a measure for Last Years to date(the 18th in this particular circumstance).
Right now, I have this:
Total Sales LYTD =
CALCULATE (
[Total Sales],
SAMEPERIODLASTYEAR (
FILTER (
VALUES ( Sales[Completed Date] ),
Sales[Completed Date] <= MAX ( Sales[Completed Date] )
)
)
)
The logic to me makes sense, but I'm sure I'm missing something, has it appears it's grabbing the ENTIRE total of 2018 when in reality i'm looking for 01/01/2018 -> 2/18/2018
This is going to be dynamically uploaded with new sales data
What am I missing? Thank you so much!
Not sure I understand your table setup so lets look at this scenario and hopefully it helps.
Suppose you have the data in two tables, Sales and Calendar, and there's a 1:* relationship between the calendar and the sales tables. Then I would write the measures like this:
SalesToDateThisYear =
calculate(
Sum(Sales[Sales]);
Calendar[Year] = Year(Today())
)
and
SalesToDateLastYear =
var dateLastYear = Today() - 365
return
calculate(
Sum(Sales[Sales]);
Calendar[Year] = Year(dateLatsYear);
Calendar[Date] < dateLastYear
)
The two filter arguments are combined with a logic AND. So only dates from the first of last year to today's date last year will be included.
If you want to use the SamePeriod-function you can probably write something like this
SPLY =
calculate =
Sum(Sales[Sales]);
SamePeriodLastYear(
Filter(
Values(Calendar[Date]);
Calendar[Date] >= Date(year(today()); 1; 1) && Calendar[Date] < Today()
)
)
)
The SamePeriod-function takes a set of dates (this year) and converts them to dates last year.
Cheers