Remove visual filter context on dates but keep slicer filters on - powerbi

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

Related

Use RankX based on a measure with dynamic max date

I have a data set of game logs for a basketball season. I want to do a ranking for a statistic comparing all teams, but only up to the current date of that specific row. Since not every team plays every day, the current row should be the max date and it should rank based on the most recent value up to that date. I have a measure that does the correct calculation:
LatestTmDVP =
VAR thisdate =
MAX ( [DATE] )
VAR maxdate =
CALCULATE ( MAX ( [DATE] ), ALLSELECTED ( 'NBA-PLAYER-FEED'[DATE] ) )
VAR result =
CALCULATE (
MAX ( 'NBA-PLAYER-FEED'[TmDVPoToDt] ),
'NBA-PLAYER-FEED'[DATE] = maxdate
)
RETURN
IF ( thisdate = maxdate, result )
If I put this in a report with a date slicer, I can see that it is working correctly:
So I need to get this measure into a column, with the max date set as the date of the current row, and then do a rank based on that, but I just can't get the RankX syntax right for this. Appreciate any help!

How to count dates in Power BI DAX

I have a table like below.
I want to show count of LicenseEndDate. Like In my slicer I have taken WeekEnding column suppose if I select weekending date like 09/25/2022 then I want show the count of LicenseEndDate dates that are ended between 09/18/2022 to 09/25/2022. How to create measure for this. I want to show that count in card visual. I created a measure like below
License_Expired_Count = COUNT(Trade[LicenseEndDate]).
But It giving me count of all. That mean the License which are expiring in the feature count also it was showing. But want to show count of license which are expired in the selected period. How create measure for this.
Please check this code, let me know If It works for you.
License_Expired_Count =
CALCULATE (
COUNT ( Trade[LicenseEndDate] ),
Trade[LicenseEndDate] <= SELECTEDVALUE ( Trade[WeekEndingDate] ),
Trade[LicenseEndDate]
> SELECTEDVALUE ( Trade[WeekEndingDate] ) - 7
)
try this :
License_Expired_Count =
VAR _slt =
SELECTEDVALUE ( Trade[WeekEndingDate] )
RETURN
CALCULATE (
COUNT ( Trade[LicenseEndDate] ),
ALLSELECTED ( Trade[WeekEndingDate] ),
DATESBETWEEN ( Trade[LicenseEndDate], _slt - 7, _slt )
)
check sample file attached

DAX use slicer selection in calculate statement

I am trying to use a calculate statement with a measure to filter results of a table but I need the values to change if the user selects 1 date from a slicer. Below is the code I am using.
PCurrentDay = calculate(sum('Sheet0 (2)'[Toys_Count]),datediff('Sheet0 (2)'[SCAN_Date],'Sheet0 (2)'[SHIP_Date],DAY)=0)
This returns a value but it won't update if the user selects a specific scan date. Any ideas?
Can you replace the measure with following
Measure =
CALCULATE (
SUM ( 'Table'[val] ),
FILTER (
'Table',
DATEDIFF (
CALCULATE ( MAX ( 'Table'[scan] ) ),
CALCULATE ( MAX ( 'Table'[ship] ) ),
DAY
) = 0
)
)
CALCULATE inside DATEDIFF provides the correct context for the filter to work.

Power BI: Use unrelated date table to create a measure

Hi I have been struggling with this for a bit now and I hope I didn't miss a previous question. I am trying to get a count of the vehicles we have based on an EOMONTH date. We are buying and selling cars on a regular basis and for reporting we need to know how many we had at the end of each month and the report is a rolling 12 months.
I've tried creating the relationship with the purchasedate of the vehicle to the date of my date table but when I create the measure (Used to calculate the number of vehicles purchased but haven't been sold):
SalesBlank = CALCULATE(
COUNT(Vehicles[MVANumber]),
FILTER(Vehicles, Vehicles[purchasedate] <= RELATED('Date'[EOMONTH]) && ISBLANK(Vehicles[saledate])))
I only get a count of vehicles purchased that month and don't have a sale date - I'm not surprised because my relationship with the date table is the purchase date.
How can I set up a measure to look at the date table and filter the vehicles table with this logic:
purchasedate <= date[EOMONTH] && ISBLANK(salesdate)
Any help would be greatly appreciated!!
Thanks,
Matt
Sample Data and Desired Results
Relationships
If I understand you correctly, you want to get a count of the vehicles on hand at the end of each month. That could be calculated by counting the vehicles with a purchase date less than or equal to the selected end of month and subtracting the count of vehicles with a sale date less than or equal to the selected end of month.
You can create an active relationship between Vehicle[PurchaseDate] and Date[Date]. Then create an inactive relationship based upon Vehicles[SaleDate] and Date[Date].
You could use a measure that is something like this:
Inventory Count =
VAR MaxDate =
MAX ( 'Date'[Date] )
VAR MinDate =
CALCULATE ( MIN ( 'Date'[Date] ), ALL ( 'Date' ) )
VAR Purch =
CALCULATE (
COUNT ( 'Vehicles'[VehicleID] ),
DATESBETWEEN ( 'Date'[Date], MinDate, MaxDate )
)
VAR Sales =
CALCULATE (
COUNT ( 'Vehicles'[VehicleID] ),
USERELATIONSHIP ( 'Date'[Date], Vehicles[Sale Date] ),
DATESBETWEEN ( 'Date'[Date], MinDate, MaxDate )
)
VAR MaxPurDt =
CALCULATE ( MAX ( 'Vehicles'[Purchase Date] ), ALL ( 'Vehicles' ) )
VAR MaxSlDt =
CALCULATE ( MAX ( 'Vehicles'[Sale Date] ), ALL ( 'Vehicles' ) )
RETURN
IF (
MIN ( 'Date'[Date] ) <= MaxPurDt
|| MIN ( 'Date'[Date] ) <= MaxSlDt,
Purch - Sales
)
This measure gets a cumulative count of purchases and a cumulative count of sales and then subtracts them. The IF check is to avoid propagation of cumulative totals beyond the maximum date in the Vehicle table.
I'm not sure how to interpret your showing of just 3 months in the desired results. This will produce the same answers as what you have, but without a filter applied to the table, it starts at 3/31/2016 (the date of the first sale).
Edit: There's probably a more efficient way along the lines you were thinking, but it is escaping me at the moment.

Using summarize and userelationship to generate a sum based on a condition

Story:
I have two date columns in my fact table, one is for orderdate and the second is for orderdate/refund/cancelled date.
I created two relationships between the date table and the fact table.
Active: Date > OrderDate
Inactive: Date > OtherDate
I would like to sum the # of refunds per day using the inactive relationship.
What I tried:
Returns =
VAR summary =
SUMMARIZE (
FILTER ( Query1, Query1[kind] = "refund" ),
Query1[orderId],
"returns", MAX ( Query1[amount] )
)
RETURN
CALCULATE (
MAX ( Query1[amount] ),
USERELATIONSHIP ( Query1[OtherDate], DateTable[Date] ),
summary
)
For some reason, it's using the active date column. Any suggestion on how to fix the above formula?
I'm not sure I understand how you are intending to use the summary variable here, but note that USERELATIONSHIP doesn't affect it at all since it's already computed.
You might not need that variable at all. Try this:
Returns =
CALCULATE (
MAX ( Query1[amount] ),
USERELATIONSHIP ( Query1[OtherDate], DateTable[Date] ),
Query1[kind] = "refund"
)