Use TREATAS to pass constant to a measure with CALCULATE - powerbi

I created a what-if parameter in a PowerBI desktop report called 'Default Target'[Default Target] which ranges from 0.8 to 1 in 0.001 increments.
Default Target = GENERATESERIES(0.8, 1.0, 0.001)
The what if parameter also built a measure for me called [Default Target Value] which is meant to return the SELECTEDVALUE() of the parameter, or its default value as the alternate result:
Default Target Value = SELECTEDVALUE('Default Target'[Default Target], 0.94)
I want to present [MyFancyMeasure], but prescribe certain [Default Targets] and load them into table columns for comparison. So I built several measures to use CALCULATE and TREATAS to get the results, as in this example:
CAR_86 = CALCULATE( [MyFancyMeasure], TREATAS({{0.86}},'Default Target'[Default Target]) )
but this didn't give me the expected result (I was expecting it to use 86% as the selected value of [Default Target], not 94%).
Instead, it returns all rows based on the <alternateResult> of the selectedvalue measure:
Why is [CAR_86] not aligning with the actual selectedvalue of the what-if parameter slicer, and also not aligning to the filtered value from the TREATAS function?
How can I effectively pass a constant to [MyFancyMeasure] so I don't have to rewrite the entire measure for each constant I want to present?
Also: I tried to filter the default target table in the calculate like this but it does the same thing:
CAR_86 = CALCULATE( [Default Target Value], FILTER(ALL('Default Target'),[Default Target]=0.86) )

Related

dax difference between filter vs filter values

Can someone explain how the first filter statement is different from second in DAX?
FILTER(Table, Table[Column] = "some text")
FILTER(VALUES(Table[Column]), (Table[Column] = "some text"))
The FILTER function is a table function, meaning it will return a table. In the case of your second example, it is likely that you will get a scalar value (a single value) because you are filtering a table (of one column of unique values) by a single value. In the first FILTER instance, however, you will be returning an entire table of the first argument, which has only been filtered by the conditional in the second argument. For reference, I have used the sample data built within the Power BI Desktop version to show you the key differences.
From your first FILTER example
FILTER( financials, financials[Country] = "Germany" )
Output
From your second FILTER example:
FILTER( VALUES( financials[Country] ), financials[Country] = "Germany" )
-- The extra parantheses around the second argument are not necessary...
Output
Therefore, the main functional difference is in the output. What are you wanting to return? An entire table or a specific value(s) of a column(s)?

How to use parameters for a report to refresh at runtime

I am calculating the score and weight of a result based on a target result and conditions for two measures. These is the dataset I have
GroupName Measure1 Measure2
Group1 0.8 0.7
Group2 0.4 0.8
Currently I am calculating the Score & Weight using two different if functions, like
Measure1 Weight Score =
var kk=IF(
'Sprints'[Velocity%_NEW] > 0.85,
"5",
IF(
'Sprints'[Velocity%_NEW] < 0.85 && 'Sprints'[Velocity%_NEW] >0.7,
"3",
If('Sprints'[Velocity%_NEW] < 0.7,"1"
)))
Return kk *0.5
What I am want is when a user use the report and asked to input the parameter for GroupName then the Score and Weight should change automatically by the value set for each GroupName and calculate the result? any help apperciated.
You can use parameters here. Refer: Create or edit a report parameter
In top header ribbon, choose Modeling and click on “What If” parameter
create a new parameter, what-if parameter screenshot to set the type and range of value that users can input. This will add a slicer to the page where user can choose.
Create a measure to interact with your report. You can add in your calculation for the result to be with your existing data and the user input(add parameter reference in your weight calculation measure).
Simple Example to multiply the source data with the input value from user:
Measure = [Parameter Value]*CALCULATE(SUM(Table1[Value]))
you can also create a measure to view the parameter selected using as below
Parameter Value = SELECTEDVALUE(Parameter[Parameter])

Does FILTER in CALCULATE add or modify the filter context?

In CALCULATE we can use FILTER(tblname,..)
Or
FILTER(ALL(tblname),..)
I want to ask whether FILTER in CALCULATE adds or modifies (clears and adds new) the existing filter context?
The answer is yes. It adds a new filter when no filter exists on tblname or it replaces (removes the existing filter and adds the new filter) when a filter already exists on tablename or some of its expanded table columns when CALCULATE is called. Existing filters on non-related columns are unaffected.
apart from the first parameter in CALCULATE, that is the expression to be computed in the modified filter context, all the other parameters are called filter arguments, since their purpose is to change the filter context.
CALCULATE( expression, table[column] = value )
is internally translated by DAX to
CALCULATE( expression, FILTER( ALL( table[column] ), table[column] = value ) )

Why is it said that only CALCULATE can modify filter context?

Why is it said that only CALCULATE can modify filter context?
For example I can use the following to modify the filter context as well.
SUMX(FILTER(ALL(DATES), ...), [MYMEASURE])
Because the FILTER expression doesn't modify the filter context, it returns a table and then, when SUMX iterates over this returned table, per each row of the iteration there is an implicit CALCULATE added by DAX when the measure is evaluated, that triggers a context transition that transforms the row context to a filter context.
if MYMEASURE is
MYMEASURE = SUM( Sales[Amount] )
THEN
SUMX(FILTER(ALL(DATES), ...), [MYMEASURE])
becomes
SUMX(FILTER(ALL(DATES), ...), CALCULATE( SUM( Sales[Amount] ) ) )
Without CALCULATE we have this formula
SUMX(FILTER(ALL(DATES), ...), SUM( Sales[Amount] ) )
in this case the SUM is executed using the filter context that exists when SUMX is executed. The row context created over the currently iterated row of ALL(DATES) is ignored since it is not transformed to a filter context through an implicit or explicit CALCULATE.

FIRSTNOTBLANK (DAX) Power BI - Without Auto-ordering

it seems like PowerBI is auto-ordering my table and I don't know why. So, I have this table (that I exported from a Excel file)
Colonne 1
7
25
1
8
3
Whenever I do a New Mesure, with this formula:
Mesure = FIRSTNONBLANK(Feuil1[Colonne 1];0)
It gives me the number 1. If I do LASTNONBLANK, it returns me the number 25. So it clearly auto-order my dataset, but whenever I look in the «Data» tab of the software, I see that my data are ordered the way I want them to be.
Any idea on how to keep the default ordering?
Unfortunately, that's the exact behavior of FIRSTNONBLANK() and LASTNONBLANK(). They iterate the values of a column based on their native sort order (i.e. data type). Therefore, FIRSTNONBLANK would return the smallest number 1 and and LASTNONBLANK the largest number 25. And in general, you cannot make assumptions on sort order of values in a table or in a column when using DAX.
Therefore, my suggestion is that you can explicitly program the logic to find the target value. Say the table is sorted based on date, you can find the key of the earliest/latest date (based on your use case), then lookup the value using the key.
Target =
VAR TargetKey =
CALCULATE(
VALUES(Table1[Key]),
FILTER(
Table1,
Table1[Date] = MIN(Table1[Date]) // or MAX(Table1[Date])
)
)
RETURN
LOOKUPVALUE(Table1[Colonne 1], Table1[Key], TargetKey)