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) )
Related
I have a big model in PowerBI where there are many different aggregation and grouping based on columns being displayed or not on the final table.
Simplifying: I need to do a conditional statement doing the sum if the value of column 1 is A1 but doing the MAX() if the value of column 1 is A2.
I need to have that information in the same column of the final output.
How would you go for this one?
Thank you very much for your help!
if you have only two values you can do a simple IF like this :
Measure = IF ( SELECTEDVALUE('Table'[Column1]) = "A1", SUM('Table'[Column2]), MAX('Table'[Column2]))
Please try this code:
TblMeasure =
VAR TblSummary =
ADDCOLUMNS (
VALUES ( FactTable[Column1] ),
"Test",
IF (
FactTable[Column1] = "A1",
SUM ( FactTable[Column2] ),
MAX ( FactTable[Column2] )
)
)
RETURN
SUMX ( TblSummary, [Test] )
If we test it on a table visual:
I'm trying to use conditional formatting for the colours of a matrix. The goal is that for each row the colour of the cell will depend on it being above or below the average for that particular row.
I have managed to produce the following matrix using this DAX measure as the conditional formatting:
Conditional Formatting =
VAR SelectedMonth = CALCULATE(SUMX(Reviews_Table,Reviews_Table[Number_of_Reviews]))
VAR AveragePerCountry = [Average Reviews]
RETURN
SWITCH(
TRUE(),
SelectedMonth >= 1.5*AveragePerCountry, "#008651",
AND(SelectedMonth < 1.5*AveragePerCountry, SelectedMonth >= AveragePerCountry), "#82ac2b",
AND(SelectedMonth < AveragePerCountry, SelectedMonth >= 0.5*AveragePerCountry), "#ff8100",
SelectedMonth < 0.5*AveragePerCountry, "#FE2828" )
where [Average Reviews] measure has been defined as
Average Reviews =
VAR SUMFILTER =
CALCULATE (
SUM ( Reviews_Table[Number_of_Reviews] ),
ALLSELECTED( 'Reviews_Table' )
)
VAR COUNTFILTER =
CALCULATE (
DISTINCTCOUNT ( Reviews_Table[Number_of_Reviews] ),
ALLSELECTED( 'Reviews_Table' )
)
RETURN
DIVIDE ( SUMFILTER, COUNTFILTER )
However, as it stands, this conditional formatting seems to compare each value to the total average over all rows. Is it possible to obtain the average for each row (in this case "Market") and use that as conditional formatting? Hopefully the answer will respect filters (I have a slicer that changes the time interval, and the average should be for that specific interval).
All help would be appreciated!
PS: Just as additional info Reviews_Table has the following columns: Month, Market, Retailer (not depicted in the matrix but accessible by pressing "+") and Number_of_Reviews.
I need to calculate pareto but in graph we can only see top 10 items but pareto should be calculated from all items in the selected date period. So e.g. if I have date 1.10.2021 and I have 20 values for that date. I can display only 10 in powerbi, how can I ignore filter for TOPN10 in dax and calculate pareto from 20 values (in graph it wont show 100 percent for pareto calculation)
I am posting filters from PowerBI
DEFINE
VAR __DS0FilterTable =
TREATAS({FALSE,
BLANK()}, 'Breakdown'[Less then 30 sec])
VAR __DS0FilterTable2 =
TREATAS({TRUE}, 'Breakdown'[IsReportedInterval])
VAR __DS0FilterTable3 =
TREATAS({"Reported"}, 'Reported Filter'[Reported])
VAR __DS0FilterTable4 =
TREATAS({DATE(2021, 10, 1)}, 'Date'[Date])
VAR __DS0FilterTable5 =
TREATAS({"Morning"}, 'Shift'[Shift Name])
VAR __SQDS0BodyLimited =
TOPN(10, __SQDS0Core, [Breakdown__min_], 0)
Here is Pareto calculation - If I use ALLSELECTED pareto is calculated from rows filtered by powerbi filters, when I use ALL it will remove all filters which is not correct because I would get sum of all rows excluding date filter. Any ideas ?
Pareto Breakdown Description:=
VAR TotalQuantity =
CALCULATE ( SUM ( Breakdown[DurationSeconds] ), ALLSELECTED( Breakdown ) )
VAR AllBreakdowns =
SUM ( Breakdown[DurationSeconds] )
VAR SummarizedTable =
SUMMARIZE (
ALLSELECTED ( Breakdown ),
'Breakdown'[Description SK],
"Amount", SUM ( Breakdown[DurationSeconds] )
)
VAR CumulativeSum =
SUMX ( FILTER ( SummarizedTable, [Amount] >= AllBreakdowns ), [Amount] )
RETURN
DIVIDE( CumulativeSum, TotalQuantity )
It will sound maybe little bit silly but I found a workaround - so I removed the filter to show only top 10 rows and within the graph I changed minimum category width which at the end shows only 10 bars (10 are hidden)
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.
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
)