Power BI dynamic ranking with some blank values and using slicers - powerbi

I have a table with 15 people that each month get 7-day scores. I want to use the RANKX formula in Power BI to rank the lowest (1) to the highest average score.
This works fine if I look at all, but start to act weirdly when I use a slicer and only look at one or two months for example. The ranking doesn't start with 1 anymore?
I use this formula:
Rank = RANKX(
ALLSELECTED('Score Table'[Person]);CALCULATE(AVERAGE('Score Table'[Score]));;ASC;Dense)
Look at the image attached, please.
Help much appreciated image showing the issue

Can you try this and see if it works?
Rank =
RANKX(
CALCULATETABLE(
VALUES( 'Score Table'[Person] ),
ALLSELECTED( 'Score Table'[Person] )
),
CALCULATE( AVERAGE( 'Score Table'[Score] ) ),
,
ASC,
Dense
)
Let's think about the original code step by step.
It iterates over "Person 1" to "Person 20" and calculates the average score of that person.
Evaluate the average score of the person of current filter context (say "Person 1").
Find the ranking position of "Person 1" in 20 persons.
In the step (1), it includes all Persons from 1 to 20 because there is no Person filter in the visual. Here, it looks there is no scores of Person 15 and 18 in the selected period, so it evaluates to BLANK.
Now, the document of RANKX says,
If expression or value evaluates to BLANK it is treated as a 0 (zero) for all expressions that result in a number, or as an empty text for all text expressions.
The average scores of Person 8 and 15 are BLANK, so RANKX treats it as 0. Now going back to Person 1, her average score was 62.43, and there were two people with average score of 0. Therefore, the rank of Person 1 will be 2.
By wrapping 'Score Table'[Person] with VALUES inside CALCULATETABLE, you can omit persons who has no scores in the selected period.

Related

what kind of measure or column will use here

[Please refer to the below image. I want to create a stacked column chart but I don't have a column that includes 0-30, 30-60, 60-90, etc. How did I put this range on X-axis?
And I didn't understand what kind of measure is used for "LAST 3 MONTH average" & "LAST 12 MONTH average ".
Can you please assist me with the same? ]
https://i.stack.imgur.com/AEnoa.png
Here is Microsoft documentation on using grouping and binning features in Power BI: https://learn.microsoft.com/en-us/power-bi/create-reports/desktop-grouping-and-binning
You can create a measure for last 3 month average like this:
3 Month Average = CALCULATE(
AVERAGE(Table[value]),
FILTER(Table,
Table[date] >= DATE(YEAR(TODAY()),MONTH(TODAY()-3),DAY(TODAY()))
)
)

DAX measure to extract summed quantity values for selected weeks only

I have production figures which sum the quantity of parts produced by each workstation on a weekly basis. These are shown in a matrix with workstation number on the rows and week numbers across the columns.
I want to be able to select 2 weeks and then
Display all the weeks in the range selected by a slicer.
Show the quantity values for only the first and last selected weeks.
The first part is working using a simple Week Slicer.
The second part, however, always shows the totals for all the selected weeks rather than the 2 individual week quantities.
In this example I have selected weeks 4 and 9 and therefore the expected values for quantities are 11,505 and 49,425 as shown in the Good Qty data with red frames.
The measures to extract the fist and last selected week numbers are working:
SelWeek1 = FIRSTNONBLANK(VALUES(vLink[Week]),0)
SelWeek2 = LASTNONBLANK(VALUES(vLink[Week]),0)
My measures for the week quantities are:
IF([SelWeek1]>0,
CALCULATE([Sum vGood Qty],FILTER(vLink, vLink[Week] = [SelWeek1])),
0
)
and
SelWeek2 Qty =
IF([SelWeek2]>0,
CALCULATE([Sum vGood Qty],FILTER(vLink, vLink[Week] = [SelWeek2])),
0
)
What am I missing?
Try to use below measures:
SelWeek1 = MIN(vLink[week])
Measure =
VAR _selWeek = [SelWeek1]
VAR result =
CALCULATE(
[Sum vGood Qty],
vLink[Week] = _selWeek
)
RETURN
result
and for selected week 2 change min to max in the first measure and _selWeek variable in the second measure respectively.

What AVERAGEX function does in this context?

I am trying to calculate monthly trend KPI over last 6 months. Whether it is positive or negative.
So I have columns: Current Month Premium, Previous Month Premium, Month Over Month
Then I am using AVERAGEX and DATESBETWEEN functions to find out whether trend is positive or negative.
WP 6M Trend =
VAR LastEffDate = LASTDATE(fact_Premium[EffectiveDate])
RETURN
AVERAGEX(
DATESBETWEEN(
dim_Date[Date],
DATEADD(STARTOFMONTH(LastEffDate), -5,MONTH), //creates rolling 6 months window [Ttl WP] and [PreviousMonthWP]
ENDOFMONTH(LastEffDate)
),
[MoM WP]
)
But I don't quiet understand how -72 exactly calculated here?
So I need to understand to know whether it is correct or not.
Your avergex is evaluating the last period in your dataset due the the fact that your LastEffDate calculates the last available date in a dataset or subset. As the total line refers to the entire context of all the data in your table, it computes the last date similarly to the last period you calculated, effectively the same calculation as the month august in this case.

MDX: How to get MAX value and the hour when MAX value occurs for each day

I have a cube with defined Hour dimension and a column in the fact table named numberofoccurrances. How can I get the maximum number of occurrances and the hour when the value happened for each day. I have tried the following
SELECT [Measures].[numberofoccurrances] ON 0,
TOPCOUNT([Hour].[Hour].MEMBERS, 1, [Measures].[numberofoccurrances])
ON 1
FROM [all_measurements]
which finds the hour where the max value happened but the sums up all values for that hour and return wrong value.
Is there a way to get each hour with max value for a day in two columns?
You need to use rank() and filter function. Take a look at the example below.
The query below list the internet sales for year -months
SELECT
{[Measures].[Internet Sales Amount]} ON COLUMNS,
non empty ({[Date].[Calendar Year].[CY 2011],[Date].[Calendar Year].[CY 2012],[Date].[Calendar Year].[CY 2013]},[Date].[Month of Year].[Month of Year])
ON ROWS
FROM
[Adventure Works]
Results
Now rank the months within years, and filter them first rand
WITH
MEMBER [Measures].[Monthly_Ranking_InternetSales] AS
RANK( [Date].[Month of Year].CurrentMember,
ORDER( [Date].[Month of Year].[Month of Year].Members , [Measures].[Internet Sales Amount], BDESC)
)
SELECT
{[Measures].[Internet Sales Amount]} ON COLUMNS,
non empty
filter( ({[Date].[Calendar Year].[CY 2011],[Date].[Calendar Year].[CY 2012],[Date].[Calendar Year].[CY 2013]},
ORDER ([Date].[Month of Year].[Month of Year], [Measures].[Monthly_Ranking_InternetSales] , BASC )
),[Measures].[Monthly_Ranking_InternetSales] <2)
ON ROWS
FROM
[Adventure Works]
Result

Calculate Number Months between 2 dates DAX

I am doing a rolling 12 month avarage over a specifik sum that are a calculated column if that matter? and my count over 12 month returns 355 day?
My sum column is [Volume]
I have a Calendar Table that are set up accordingly to a hierarchy as picture show below and more,
my code for the rolling avarage is,
12M tonnage Avarage =
CALCULATE(
SUM(STOWAGEACTUAL[VolyMton]);
DATESINPERIOD('Calendar'[DATE]; LASTDATE('Calendar'[DATE]);12;MONTH)
)
/
CALCULATE(
DISTINCTCOUNT('Calendar'[DATE]);
DATESINPERIOD('Calendar'[DATE];LASTDATE('Calendar'[DATE]);12;MONTH
))
All my values that i have in my SUM column looks like this,
And my Rolling AVG abowe calculates to this,
After some error searching i find that this piece of code,
DISTINCTCOUNT('Calendar'[DATE]);
DATESINPERIOD('Calendar'[DATE];LASTDATE('Calendar'[DATE]);12;MONTH
)
Gives me a Distinct Count value of 356. And when build my report to look on Days. My Rolling Average does compute correctly.
BUT, this is not what i want since i want this DAX to return 12 and i cant figure out why is doesnt?