Forum Discussion
EmmaWyeth
5 years agoFrequent Visitor
Measure based on Slicer Selection
Hi All I am trying to write a calculation measure in Power BI, to work out if this week's volumes are greater or less than the average of the last 12 weeks volumes. If greater then "yes" otherwi...
Anonymous
5 years agoNot applicable
Hi EmmaWyeth
Due to I don't know your data model, so I build a sample to have a test.
Sample Table =
ADDCOLUMNS (
CALENDAR ( DATE ( 2019, 12, 01 ), DATE ( 2020, 12, 31 ) ),
"Year", YEAR ( [Date] ),
"Month", MONTH ( [Date] ),
"Day", DAY ( [Date] ),
"WeekNum", WEEKNUM ( [Date], 2 ),
"Volumn", INT ( RAND () * 100 )
)
Sample Table:
My Volumn is Random for in order to convenience.
Add two calculated columns YearWeekNum and Rank.
YearWeekNum = 'Sample Table'[Year]*100+'Sample Table'[WeekNum]Rank = RANKX('Sample Table','Sample Table'[YearWeekNum],,ASC,Dense)
Build a Slicer Table by YearWeekNum.
Slicer = VALUES('Sample Table'[YearWeekNum])
Measure:
Measure =
VAR _Select =
SELECTEDVALUE ( Slicer[YearWeekNum] )
VAR _SelRank =
CALCULATE (
MAX ( 'Sample Table'[Rank] ),
FILTER ( 'Sample Table', 'Sample Table'[YearWeekNum] = _Select )
)
VAR _SUM12 =
SUMX (
FILTER (
ALL ( 'Sample Table' ),
'Sample Table'[Rank] < _SelRank
&& 'Sample Table'[Rank] >= _SelRank - 12
),
'Sample Table'[Volumn]
)
VAR _AVG12 =
DIVIDE ( _SUM12, 12 )
VAR _SUMSEL =
SUMX ( FILTER ( ALL ( 'Sample Table' ), 'Sample Table'[Rank] = _SelRank ), 'Sample Table'[Volumn] )
RETURN
IF ( _SUMSEL > _AVG12, "Yes", "No" )
Result:
You can download the pbix file from this link: Measure based on Slicer Selection
Best Regards,
Rico Zhou
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.