I'm developing a PowerBI report in DirectQuery.
I introduced a measure with the syntax ALLSelected (RemoveFilters) because I need to calculate some data removing a contextual filter based on the relationship between two tables. It works, but only when one rows is involved (for example, putting a specific filter on a column "Country"). If more countries are involved, the value for each row is the sum of all the rows involved (so, the number is always the same for all the rows). It seems that PowerBI is not able anymore with AllSelected function to "group by" the data.
mes_CountData =
VAR FirstFilter = MIN( pbi_Table1[Code1] )
VAR SecondFilter = MIN( pbi_Table1[Code2] )
VAR ThisRows = CALCULATE(COUNTROWS( FILTER(ALLSELECTED(pbi_Table1), pbi_Table1[Code1] <> FirstFilter && pbi_Table1[Code2] = SecondFilter), REMOVEFILTERS(pbi_Table1[OriginalCode]))
return ThisRows
How can I fix it? Any solutions or ideas are welcome.
I have no idea how REMOVEFILTERS( pbi_Table1[OriginalCode]) influence on your data model. May be if you change the transfomation order and do like this it will help:
mes_CountData =
VAR FirstFilter = MIN( pbi_Table1[Code1] )
VAR SecondFilter = MIN( pbi_Table1[Code2] )
VAR allSelectedTable =
FILTER(
ALLSELECTED(pbi_Table1)
,pbi_Table1[Code1] <> FirstFilter && pbi_Table1[Code2] = SecondFilter
)
VAR ThisRows =
CALCULATE(
COUNTROWS(allSelectedTable)
,REMOVEFILTERS(pbi_Table1[OriginalCode])
)
return ThisRows
Related
i have a table in Power BI that is the result of a combined query (3 tables), which means i have a column with 3 different project IDs (4010 = current project, 3844 = previous month's project, 3653 = baseline)
i have another column called "actual cost" and what i would like to do is add a calculated column that calculates for each row the difference between the actual cost in ID 4010 vs ID 3844, based on the denominator activity ID or name.
how would i go about doing this?
Personally I would prefer a Measure in such situations. A Calculated Column is less flexible and more overhead. However, since that it was you asked for:
=
VAR ThisActivityID = Table1[activity ID]
VAR ActualCost4010 =
CALCULATE(
SUM( Table1[actual cost] ),
FILTER(
ALL( Table1 ),
Table1[activity ID] = ThisActivityID
&& Table1[project ID] = 4010
)
)
VAR ActualCost3844 =
CALCULATE(
SUM( Table1[actual cost] ),
FILTER(
ALL( Table1 ),
Table1[activity ID] = ThisActivityID
&& Table1[project ID] = 3844
)
)
RETURN
ActualCost4010 - ActualCost3844
The average price is 28.87 and I want to calculate and visualize the number of products with a price higher than 28.87 on a row by row basis. The total number is 25.
In the table visual below, I only see the values above average when using the hardcoded value (_avg2), and not when using the formula (_avg). Please refer to the measure below. I only switch around _avg and _avg2 in line 9 in the two columns.
countAvg =
VAR _avg = AVERAGE( ( Products[UnitPrice] ) )
VAR _avg2 = 28.87
VAR _aboveAvg =
CALCULATE(
COUNT( Products[ProductName] ) ,
FILTER( Products , [Unit Price] > ( _avg ) ) )
RETURN _aboveAvg
Below you can see the difference in a snapshot of the visual.
Question: Is my defined variable _avg incorrect in terms of making it visible on a row level (evaluation context)? Anyhow, what should it be to make it work? Having it as a hardcoded value (_avg2) is not useful.
Thanks in advance!
Try changing the filter context of the _avg formula:
VAR _avg = CALCULATE(AVERAGE( Products[UnitPrice] ), ALLSELECTED(PRODUCTS) )
While executing the below DAX expression, I am getting an error "USERELATIONSHIP function can only use the two columns reference participation in relationship".
So could you please help me with that what's wrong with the expression?
Accuracy_Last_6_Month =
VAR ReferenceDate = MAX(Calender[Date])
VAR Last_6Month =
DATESINPERIOD(
Calendar_Last6Month[Date].[Date],
ReferenceDate,
-6,
MONTH
)
VAR Result =
CALCULATE(
[Accuracy],
REMOVEFILTERS(Calender[Date]),
KEEPFILTERS(Last_6Month),
USERELATIONSHIP(Calender[Date],Calendar_Last6Month[Date].[Date])
)
RETURN
Result
Relationship created between tables as inactivated form:
Columns used in both the table:
You should be able to use a single Calendar. Your second calendar is redundant.
I would write something like this:
Accuracy_Last_6_Month =
CALCULATE([Accuracy],
FILTER(ALL(Calender),
Calender[Date] > MAX(Calender[Date])-180 &&
Calender[Date] <= MAX(Calender[Date])))
I'm guessing the error is because you are using Calendar_Last6Month[Date].[Date] inside USERELATIONSHIP, which isn't actually a table column. Try deleting that .[Date] suffix everywhere in your measure:
Accuracy_Last_6_Month =
VAR ReferenceDate = MAX ( Calender[Date] )
VAR Last_6Month =
DATESINPERIOD ( Calendar_Last6Month[Date], ReferenceDate, -6, MONTH )
VAR Result =
CALCULATE (
[Accuracy],
REMOVEFILTERS ( Calender[Date] ),
KEEPFILTERS ( Last_6Month ),
USERELATIONSHIP ( Calender[Date], Calendar_Last6Month[Date] )
)
RETURN
Result
I generally avoid using these Time Intelligence suffixes entirely.
Need some help to calculate the following in two separate columns in a DAX formula
Earliest Reading for each equip
Latest Reading for each equip
Screenshot of sheet
I've been able to get the first date of each equip with this.
CALCULATE(FIRSTDATE(Transactions[Date]),ALLEXCEPT(Transactions,Transactions[Equip No]))
But cannot work out how to get the 'Reading' value that is associated with the first date
I've managed to do this with a measure, but would also like to get this in a calc. column.
Latest Reading =
SUMX (
VALUES( Transactions[Equip No] ),
CALCULATE ( MIN ( Transactions[Reading] ), FIRSTDATE ( Transactions[Date] ) )
)
this should help you.
Earliest Column
Earliest =
VAR __equipNumber = 'Transaction'[Equip No] //Get the Equip No to filter the main table and create an auxiliar table for every different Equip No.
VAR __minDate = CALCULATE( MIN('Transaction'[Date]), FILTER( 'Transaction', 'Transaction'[Equip No] = __equipNumber ) ) //Get the lowest date asociated to every Equip No.
VAR __subTable = FILTER( 'Transaction', 'Transaction'[Date] = __minDate ) //Create a table that contains 1 row asociate to the lowest date.
Return CALCULATE(SUM('Transaction'[Reading]), __subTable) //Operate over the auxiliar table to get the expected value.
Latest Column
Latest =
VAR __equipNumber = 'Transaction'[Equip No]
VAR __maxDate = CALCULATE( MAX('Transaction'[Date]), FILTER( 'Transaction', 'Transaction'[Equip No] = __equipNumber ) )
VAR __subTable = FILTER( 'Transaction', 'Transaction'[Date] = __maxDate )
Return CALCULATE(SUM('Transaction'[Reading]), __subTable)
I obtained the expected result
Please see the file I have shared in the following link:
https://drive.google.com/file/d/1q7FE85qHoB_OhAm4hlmFidh6WP3m-UnK/view?usp=sharing
I have a dataset with the value of various items on different dates. I first need to calculate ratio by dividing the value of an item on a particular date with the earliest value in the chosen date filters. I then need to show the per day ratio of all items (columns I & J) as the desired output.
I am able to get the "first value" in the filtered table by using the following formula:
first_cr =
VAR item = MAX('Table 1'[item])
VAR min_updated = CALCULATE(
MIN('Table 1'[last_updated]),
'Table 1'[item] = item,
ALLSELECTED('Table 1')
)
RETURN
CALCULATE(
AVERAGE('Table 1'[cr]),
'Table 1'[last_updated] = min_updated,
'Table 1'[item] = item,
ALLSELECTED('Table 1'[fk])
)
However, I am unable to figure out how to calculate the average of all the individual item ratios at just the date level. I guess I need to use AVERAGEX somehow, but not sure how. Perhaps my first_cr formula could use more refinement.
I'd appreciate any help or guidance in the right direction. Thanks for your time.
I was able to get the answer using the following formula. If anyone can refine it better, please do so, else I'll accept my answer after a few days.
ret =
var lastUpdated = MAX(Sheet1[Date])
var tbl = ALLSELECTED(Sheet1)
RETURN
CALCULATE(
AVERAGEX(
Sheet1,
var i = Sheet1[Item]
var minUpdated = CALCULATE(
MIN(Sheet1[Date]),
Sheet1[Item] = i,
tbl
)
var first_cr = CALCULATE(
AVERAGE(Sheet1[Return]),
Sheet1[Date] = minUpdated,
Sheet1[Item] = i,
tbl
)
RETURN Sheet1[Return] / first_cr
),
Sheet1[Date] = lastUpdated,
tbl
)