DAX FILTER works with DATE() literal, but not SELECTEDVALUE()? - dax

In PowerBI, I have a table of prices (ItemID, ItemName, Price, EffectiveStartDate, EffectiveEndDate), and I'm trying to return all the prices that are effective on a given date (selected by the user or the evaluation context. On my report canvas I have a table visual with all the items and the details (above). If I declare a variable and assign a literal value to it, the filters work fine.... like this:
VAR CurrentDate =
DATE(2022, 3, 15)
RETURN
FILTER (
Ingredients,
[EffectiveStartDate] < CurrentDate
&& [EffectiveEndDate] > CurrentDate
)
but if I try to assign CurrentDate the value from my harvester measure, it fails. (somehow the inequality filters are being ignored ). I have a slicer (dropdown) to pick a date, and SELECTEDVALUE('DimDate'[Date}) to retrieve the value. But when I try to use that in my filter, it fails:
VAR CurrentDate =
SELECTEDVALUE('DimDate'[Date])
RETURN
FILTER (
Ingredients,
[EffectiveStartDate] < CurrentDate
&& [EffectiveEndDate] > CurrentDate
)
Any idea why this is happening? And how do I fix it?
I was expecting the two filters to act the same (the literal filter with DATE() and with a variable, but not so. When I set the value of a variable using SELECTEDVALUE(), it seems to ignore the variable in my FILTER() statement.

If you want a calculation to depend on a slicer, You need a Measure, NOT a Calculated column or Calculated table. Calculated columns and calculated tables are generated on refresh and physically stored in your model, so the slicers can filter them, but the slicers can't change the value of the calculations.
Measures are not persistent but are calculated as needed based on changes to filters and slicers. E.g. you could use a measure like the following one to filter ingredients:
Filtered Ingredients =
VAR CurrentDate = SELECTEDVALUE(DimDate[Date])
RETURN
CALCULATE(
MAX(Ingredients[Name]),
Ingredients[EffectiveStartDate] < CurrentDate
&& Ingredients[EffectiveEndDate] > CurrentDate
)

Related

HOW TO REFERENCE A MEASURE IN A FILTER (DAX)

I have created a measure
Report_Day = MAX(DATE_LT[Date])-2
I want to create measures where they reference this measure so i can just change -2 to -1 and the rest of measures work depending on the reporting date.
when i use this measure:
Day_outlet_Txns = CALCULATE(
[outlet_Dep_Txns],
FILTER(ALL(DATE_LT),[Report_Day])
)
i get a value equal to tatal transactions in a table yet i want for transactions of that report day.
When i try something else
Day_outlet_Txns = CALCULATE(
[Issuer_Dep_Txns],
FILTER(ALL(DATE_LT), DATE_LT=[Report_Day])
)
i get an error: The expression refers to multiple columns. Multiple columns cannot be converted to a scalar value.
enter image description here

Reuse calculated table variable within DAX formula

I have the following DAX formula for a measure, and it works fine.
Gross Area =
var as_on = LASTDATE('Calendar'[Date])
var all_latest_dates =
ADDCOLUMNS(
VALUES('Unit Revision'[Unit Id]),
"Last Event", CALCULATE(
MAX('Unit Revision'[Event Date]),
'Unit Revision'[Event Date] <= as_on
)
)
var unit_latest_dates = TREATAS(all_latest_dates, 'Unit Revision'[Unit Id], 'Unit Revision'[Event Date])
RETURN
CALCULATE( SUM('Unit Revision'[Gross Area]), unit_latest_dates )
I need to calculate more metrics using a similar logic, where the DAX formula is the same until the RETURN statement, and just the column name within the final CALCULATE( SUM() ) differs.
I then need to add all these measures to a Matrix. In such as case, will the variable calculation of theĀ unit_latest_dates table be calculated for every DAX measure, or will it be cached? To ensure it is cached, is it possible to reuse the initial set of DAX code within a calculated table measure or something, just like how we can reuse a scalar measure in another measure?
I can't use this in a Calculated Table, as the calculation changes based on changes in the Calendar Table Date slicer.
Yes, it should be cached as long as DAX Engine can understand that a similar query was executed earlier and it didn't involve a CALLBACKDATAID and was executed only by Storage Engine and not Formula Engine, try to use DAX Studio and see if it is doing that or not.

DAX How to Adjust Dates Based on Selected Value of Slicer

Here is my setup:
I have a date slicer that I created by doing this:
d_date = Calendar(min('Table_1'[CTD]),max('Table_1'[CTD])
Here is what my dax looks like for my variables currently:
var FirstVisibleVDate = Min('d_date'[Date])
var LastVisibleDate = Max('d_date'[Date])
My problem(s):
Problem 1: I am doing a running average, I want the running average to adjust itself based on the dates selected in the slicer. I don't think this is possible, is it?
So basically, something like this:
var FirstVisibleVDate = Min(value set in the date slicer)
var LastVisibleDate = Max(value set in the date slicer)
Date range=
Var MinDate = CALCULATE(MIN(Table1[transaction date]),ALLSELECTED(Table1[transaction date]))
Var MaxDate = CALCULATE(MAX(Table1[transaction date]),ALLSELECTED(Table1[transaction date]))
Source:
https://community.powerbi.com/t5/Desktop/Capture-Time-Slicer-Minimum-Value/m-p/820372
If you are going to make a selection over the slicer then that will limit the rows that are visible in the Matrix visual, you will only see the result for the dates selected, so you will have to create a duplicate disconnected Date table that will not filter the rows of the visual.
I have made a couple of forum post on this topic as well as some videos:
Posts:
https://forum.enterprisedna.co/t/dax-calculation-for-choose-n-specific-date/16408
https://forum.enterprisedna.co/t/learning-resources-about-disconnected-date-tables/15416/2
https://forum.enterprisedna.co/t/cumulative-transactions/11803/19
Videos:
https://www.youtube.com/watch?v=WFJThyfQt-A&ab_channel=AntrikshSharma
https://www.youtube.com/watch?v=fLWfdlAaHlQ&ab_channel=AntrikshSharmaAntrikshSharma

Filter Recent date in filter

I want the Slicer in Power BI to select the most recent date of the selection to be selected automatically.
Here is an example of the drop down:
https://i.imgur.com/IykHSlI.png
This drop down differs from the Client selection.
I solved this issue the following way:
I created one Report with a filter to Default_Date (which opens first)
I used a Calculated Column [Default_Date] to populate the filter (which is hidden)
In my case the user wanted to see Yesterday's data as the default date so I selected 'Yesterday' on my filter.
Then I put a button that opens another duplicated copy of the Report [Hidden Tab] that contains a full calendar filter, so the user can select any other dates he likes, this hidden report has another button that returns the user to the main report [if he wants to].
picture of my filter (which I collapse and hide under a color box/banner)
Here is the formula for the calculated column (used in the filter):
Default_Date =
VAR TodaysDate =
TODAY()
VAR YesterdayDate =
TODAY() - 1
VAR reportDate =
SWITCH(TRUE(),
'Calendar'[Date] = TodaysDate, "Today",
'Calendar'[Date] = YesterdayDate, "Yesterday",
"Before Yesterday"
)
RETURN
reportDate
Use Default_Date in your filter, and you can replace TODAY() with
CALCULATE(Max(Table[Date]),All(Table))
and remove what you don't need.
If you want to get the Last Date of selected items, then
CALCULATE(Max(Table[Date]),ALLSELECTED(Table))
Table may need to be in quotes to work: 'Table'[Date]
I hope this helps.
There isn't to set a default value in Power BI, but there are a few around about ways. First you can try Persistent Filters that can preserve the filters selected. The other option, is to create in your data set, if you have a calendar table, or are able to add it to your column a current date flag, that you can apply as a filter to your report.
For example you can use the TODAY() to return todays date, and check against your date column.
Calculated column = IF(MONTH(TODAY()) = MONTH('table'[DateColumn]) && YEAR(TODAY()) = YEAR('table'[DateColumn]), "Y", "N")
You can run the report and it will always filter on the latest date, however you will have to remove the filter if you want to see other dates. You could set up bookmarks so that you can easily add/remove filter to the report. So you would have one bookmark with latest date, and the other to remove it. You can allow the slicer box to appear when you select the 'remove current month' bookmark
Hope that helps

PowerBi DAX equivalent for SUMIFS with current row value as filter

In Excel I could, if I was in a table called 'Sales' that had four columns
Sales
Month, CustomerId, ProductId, TotalQuantity
Jan,1, CAR,
Feb,1, CAR,
I could add a formula:
=SUMIFS(Sales[Quantity],Sales[CustomerId],[#[CustomerId]])
That would go to the Sales table and sum the CustomerID column filtered by the CustomerID of the current row where the formula has been entered.
I am attempted to replicate this in a PowerBI Calculated Row but I can't get the # working for a row reference. It comes across like
TotalQuantity = CALCULATE(SUM(Sales[Quantity]),Sales[CustomerId] = Sales[CustomerId]))
Any idea how to get the equivalent # working?
I think the key function you are missing is EARLIER. That is not surprising because it has a misleading name - it really means "Current Row". You also need a FILTER function in the Filter parameter of CALCULATE, to reset the filter context to the entire table.
So your New Column function might look like this:
TotalQuantity = CALCULATE(SUM(Sales[Quantity]), FILTER(Sales, Sales[CustomerId] = EARLIER (Sales[CustomerId])))
Here's a neat example, from the most accessible source site for DAX formulas:
http://www.powerpivotpro.com/2013/07/writing-a-subtotal-calc-column-aka-the-simplest-use-of-the-earlier-function/
And FWIW here is the official doco on EARLIER:
https://msdn.microsoft.com/en-us/library/ee634551.aspx

Resources