cancel
Showing results for
Did you mean: ## Rankx with measure doesn't seem to work correctly

Here's the link to pbix file: https://www.dropbox.com/s/9k0fth7qed7t33n/sample%20data.pbix?dl=0

On the "D: Top 10" tab, I have certain attributes listed in the table as well as calculated measures. I'm trying to get the row entries with top 10 "R3M error" values to be displayed in the top table regardless of the attributes listed in the table (region, month year, warehouse, account group, product name, sales person), but it should be based on the filters applied on the table. For example, if these filters, Region=E1, Month Year=Feb 2021, were applied, then the rank function should only rank all the "Final Table" entries that apply to those filters, based on the "R3M error" measure, and only the top 10 "R3M error" values should be displayed in the table.

I've tried using the top N filtering option, but when I do that, all my "LM ..." and "2PM ..." measures don't work. I think it's currently ranking each individual region, month year, warehouse, account group, product name, and sales person combination, but that's not what I want.

This is my measure for the rank:

``````Over rank =
RANKX(
ALLSELECTED('Final Table'),
[R3M Error],
,
DESC,
Skip
)``````

This is my measure for the R3M error and R3M requested and forecast:

``````R3M Error = [R3M Forecast]-[R3M Requested]

R3M Forecast =
CALCULATE(
SUM('Final Table'[Forecast Requested Volume]),
DATESINPERIOD(
'Date Table'[Date],
LASTDATE('Date Table'[Date]),
-3,
MONTH
)
)

R3M Requested =
CALCULATE(
SUM('Final Table'[Requested Volume]),
DATESINPERIOD(
'Date Table'[Date],
LASTDATE('Date Table'[Date]),
-3,
MONTH
)
)``````

Thank you!

1 ACCEPTED SOLUTION Super User

Hi again @zaminamina

I've had a further look and believe I've come up with a version of Over rank that works.

The reason it's a little bit complicated is that we have to mimic the behaviour of SUMMARIZECOLUMNS (the function that Power BI uses within the DAX query generated by the visual), without using SUMMARIZECOLUMNS ourselves. SUMMARIZECOLUMNS does not support evaluation within a context transition, which we would need if we were to use it in this measure.

The measure instead uses GENERATECROSSJOIN/SUMMARIZE to construct a table whose rows correspond to the rows of the table visual (including removing rows where all measures are blank). Also, in order to mimic Auto-exist behaviour, we can't allow 'Date Table' filters to filter 'Final Table' when deciding which combinations of values from 'Final Table' are included.

The table is constructed by CROSSJOINing distinct combinations from 'Final Table' (ignoring the 'Date Table' filters) and 'Date Table', then removing rows that have all-blank measures. The rank is then calculated using this table as the first argument of RANKX.

I put some comments in the code that hopefully make it clearer. Also, I have written a simpler version of the measure that I think will work just as well. The simpler version assumes that [R3M Error] will be nonblank in every row of the table visual.

Full version:

``````Over rank =
VAR RankingTable =
CALCULATETABLE (
GENERATE (
-- The CROSSJOIN (...) code produces the set of rows that would be included in the visual
-- if no measures were included (i.e. before removing rows with all measures blank.
CROSSJOIN (
-- Summarize columns of 'Final Table'.
-- Filters on 'Date Table' are removed using REMOVEFILTERS, to mimic Auto-exist behaviour:
-- Auto-exist means the values from 'Final Table' are not filtered by 'Date Table'
-- for the purpose of determining combinations to appear in the visual.
CALCULATETABLE (
SUMMARIZE (
'Final Table',
'Final Table'[Account Group],
'Final Table'[Salesperson],
'Final Table'[Region/Forecasting Entity],
'Final Table'[Warehouse],
'Final Table'[Product Name]
),
REMOVEFILTERS ( 'Date Table' )
),
-- Summarize columns of 'Date Table'.
-- Since 'Final Table' doesn't filter 'Date Table', we don't need REMOVEFILTERS here.
SUMMARIZE (
'Date Table',
'Date Table'[Month Year],
'Date Table'[Year Month Number]
)
),
-- Evaluate all measures included in the visual.
-- This could be reduced to a minimal set of measures.
-- In sample data model, [R3M Error] could be sufficient.
VAR Measure_Error = [Error]
VAR Measure_Forecast_Requested = [Forecast Requested]
VAR Measure_Requested = [Requested]
VAR Measure_LM_Forecast = [LM Forecast]
VAR Measure_LM_Requested = [LM Requested]
VAR Measure_2PM_Forecast = [2PM Forecast]
VAR Measure_2PM_Requested = [2PM Requested]
VAR Measure_R3M_Forecast = [R3M Forecast]
VAR Measure_R3M_Requested = [R3M Requested]
VAR Measure_R3M_Error = [R3M Error]
VAR Include_Row =
-- Only include rows where at least one measure is nonblank
-- (i.e. where not all measures are blank)
NOT (
ISBLANK ( Measure_Error ) && ISBLANK ( Measure_Forecast_Requested )
&& ISBLANK ( Measure_Requested )
&& ISBLANK ( Measure_LM_Forecast )
&& ISBLANK ( Measure_LM_Requested )
&& ISBLANK ( Measure_2PM_Forecast )
&& ISBLANK ( Measure_2PM_Requested )
&& ISBLANK ( Measure_R3M_Forecast )
&& ISBLANK ( Measure_R3M_Requested )
&& ISBLANK ( Measure_R3M_Error )
)
RETURN
-- Add the [R3M Measure] value to the table, but exclude the row if all measures are blank.
FILTER ( ROW ( "@R3M Error", Measure_R3M_Error ), Include_Row )
),
-- Use ALLSELECTED to ensure RankingTable is constructed in the overall filter context of the visual
ALLSELECTED ()
)
RETURN
-- Finally, return the rank of [R3M Error] in current filter context, compared with all values in RankingTable
RANKX ( RankingTable, [@R3M Error], [R3M Error], DESC, SKIP )``````

Simplified version:

``````Over rank =
VAR RankingTable =
CALCULATETABLE (
GENERATE (
CROSSJOIN (
CALCULATETABLE (
SUMMARIZE (
'Final Table',
'Final Table'[Account Group],
'Final Table'[Salesperson],
'Final Table'[Region/Forecasting Entity],
'Final Table'[Warehouse],
'Final Table'[Product Name]
),
REMOVEFILTERS ( 'Date Table' )
),
SUMMARIZE (
'Date Table',
'Date Table'[Month Year],
'Date Table'[Year Month Number]
)
),
VAR Measure_R3M_Error = [R3M Error]
VAR Include_Row =  NOT ISBLANK ( Measure_R3M_Error )
RETURN
FILTER ( ROW ( "@R3M Error", Measure_R3M_Error ), Include_Row )
),
ALLSELECTED ()
)
RETURN
RANKX ( RankingTable, [@R3M Error], [R3M Error], DESC, SKIP )``````

Kind regards,

Owen

Owen Auger

My Blog
5 REPLIES 5 Super User

There is no way to create a rank measure that adapts to whatever level of detail of the fields included in the table visual (not yet, anyway).

However, you can write a measure that is defined based on a specific level of detail, using the fields currently in your table visual.

Here is an example for Over rank, that should work with the first table on the "D: Top 10" page:

``````Over rank =
VAR RankingTable =
CALCULATETABLE (
CROSSJOIN (
SUMMARIZE (
'Final Table',
'Final Table'[Region/Forecasting Entity],
'Final Table'[Warehouse],
'Final Table'[Account Group],
'Final Table'[Product Name],
'Final Table'[Salesperson]
),
VALUES ( 'Date Table'[Month Year] )
),
"@R3M Error", [R3M Error]
),
ALLSELECTED ()
)
RETURN
RANKX (
RankingTable,
[@R3M Error],
[R3M Error],
DESC,
SKIP
)``````

The measure is a bit long-winded, as it has to explicitly list the fields by which the visual is grouped, in order to create a table to calculate ranks against.

One thing to note: because of the definition of [R3M Error] which looks back over 3 months, the dates for which the measure is nonblank don't have to correspond to combinations that exist in 'Final Table'. This is the reason 'Date Table'[Month Year] is not included in SUMMARIZE, but instead VALUES ( 'Date Table'[Month Year] ) is cross-joined with SUMMARIZE (...).

There could well be a way to simplify this measure, but I haven't come up with it yet!

Regards,

Owen

Owen Auger

My Blog Hi Owen,

Thanks for the speedy response!

I tried the measure out, but I'm seeing an issue where if the "Error" column is blank, the "Over rank" function tends to repeat the ranking for those. How can we prevent this, cos it doesn't matter if "error" is blank or not, the "over rank" should just rank based on "R3M error". For the image below, I've selected the slicers: Region - E1, Month Year - Dec 2020. Super User

Thanks for the reply @zaminamina .

Agreed, my measure is clearly not working as intended in that case!

The measure was intended to give a valid rank for the combinations of grouping fields that exist in the table, and the blank values for the [Error] measure do appear to be a clue.

I must have made a logical error somewhere along the way. I'll take a further look and get back to you.

Regards,

Owen

Owen Auger

My Blog Super User

Hi again @zaminamina

I've had a further look and believe I've come up with a version of Over rank that works.

The reason it's a little bit complicated is that we have to mimic the behaviour of SUMMARIZECOLUMNS (the function that Power BI uses within the DAX query generated by the visual), without using SUMMARIZECOLUMNS ourselves. SUMMARIZECOLUMNS does not support evaluation within a context transition, which we would need if we were to use it in this measure.

The measure instead uses GENERATECROSSJOIN/SUMMARIZE to construct a table whose rows correspond to the rows of the table visual (including removing rows where all measures are blank). Also, in order to mimic Auto-exist behaviour, we can't allow 'Date Table' filters to filter 'Final Table' when deciding which combinations of values from 'Final Table' are included.

The table is constructed by CROSSJOINing distinct combinations from 'Final Table' (ignoring the 'Date Table' filters) and 'Date Table', then removing rows that have all-blank measures. The rank is then calculated using this table as the first argument of RANKX.

I put some comments in the code that hopefully make it clearer. Also, I have written a simpler version of the measure that I think will work just as well. The simpler version assumes that [R3M Error] will be nonblank in every row of the table visual.

Full version:

``````Over rank =
VAR RankingTable =
CALCULATETABLE (
GENERATE (
-- The CROSSJOIN (...) code produces the set of rows that would be included in the visual
-- if no measures were included (i.e. before removing rows with all measures blank.
CROSSJOIN (
-- Summarize columns of 'Final Table'.
-- Filters on 'Date Table' are removed using REMOVEFILTERS, to mimic Auto-exist behaviour:
-- Auto-exist means the values from 'Final Table' are not filtered by 'Date Table'
-- for the purpose of determining combinations to appear in the visual.
CALCULATETABLE (
SUMMARIZE (
'Final Table',
'Final Table'[Account Group],
'Final Table'[Salesperson],
'Final Table'[Region/Forecasting Entity],
'Final Table'[Warehouse],
'Final Table'[Product Name]
),
REMOVEFILTERS ( 'Date Table' )
),
-- Summarize columns of 'Date Table'.
-- Since 'Final Table' doesn't filter 'Date Table', we don't need REMOVEFILTERS here.
SUMMARIZE (
'Date Table',
'Date Table'[Month Year],
'Date Table'[Year Month Number]
)
),
-- Evaluate all measures included in the visual.
-- This could be reduced to a minimal set of measures.
-- In sample data model, [R3M Error] could be sufficient.
VAR Measure_Error = [Error]
VAR Measure_Forecast_Requested = [Forecast Requested]
VAR Measure_Requested = [Requested]
VAR Measure_LM_Forecast = [LM Forecast]
VAR Measure_LM_Requested = [LM Requested]
VAR Measure_2PM_Forecast = [2PM Forecast]
VAR Measure_2PM_Requested = [2PM Requested]
VAR Measure_R3M_Forecast = [R3M Forecast]
VAR Measure_R3M_Requested = [R3M Requested]
VAR Measure_R3M_Error = [R3M Error]
VAR Include_Row =
-- Only include rows where at least one measure is nonblank
-- (i.e. where not all measures are blank)
NOT (
ISBLANK ( Measure_Error ) && ISBLANK ( Measure_Forecast_Requested )
&& ISBLANK ( Measure_Requested )
&& ISBLANK ( Measure_LM_Forecast )
&& ISBLANK ( Measure_LM_Requested )
&& ISBLANK ( Measure_2PM_Forecast )
&& ISBLANK ( Measure_2PM_Requested )
&& ISBLANK ( Measure_R3M_Forecast )
&& ISBLANK ( Measure_R3M_Requested )
&& ISBLANK ( Measure_R3M_Error )
)
RETURN
-- Add the [R3M Measure] value to the table, but exclude the row if all measures are blank.
FILTER ( ROW ( "@R3M Error", Measure_R3M_Error ), Include_Row )
),
-- Use ALLSELECTED to ensure RankingTable is constructed in the overall filter context of the visual
ALLSELECTED ()
)
RETURN
-- Finally, return the rank of [R3M Error] in current filter context, compared with all values in RankingTable
RANKX ( RankingTable, [@R3M Error], [R3M Error], DESC, SKIP )``````

Simplified version:

``````Over rank =
VAR RankingTable =
CALCULATETABLE (
GENERATE (
CROSSJOIN (
CALCULATETABLE (
SUMMARIZE (
'Final Table',
'Final Table'[Account Group],
'Final Table'[Salesperson],
'Final Table'[Region/Forecasting Entity],
'Final Table'[Warehouse],
'Final Table'[Product Name]
),
REMOVEFILTERS ( 'Date Table' )
),
SUMMARIZE (
'Date Table',
'Date Table'[Month Year],
'Date Table'[Year Month Number]
)
),
VAR Measure_R3M_Error = [R3M Error]
VAR Include_Row =  NOT ISBLANK ( Measure_R3M_Error )
RETURN
FILTER ( ROW ( "@R3M Error", Measure_R3M_Error ), Include_Row )
),
ALLSELECTED ()
)
RETURN
RANKX ( RankingTable, [@R3M Error], [R3M Error], DESC, SKIP )``````

Kind regards,

Owen

Owen Auger

My Blog Hi Owen, that worked perfectly, appreciate the explanation as well. Thanks so much! Announcements The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform. #### Check it Out!

Mark your calendars and join us on Thursday, June 30 at 11a PDT for a great session with Ted Pattison! #### The Power BI Community Show

Join us on June 20 at 11 am PDT when Kim Manis shares the latest on Azure Synapse analytics, the Microsoft Intelligent Data Platform, and notable Power BI Updates from Microsoft Build 2022. Top Solution Authors
Top Kudoed Authors
Users online (1,648)
{"ft_lang_label":"__label__en","ft_lang_prob":0.8243203,"math_prob":0.8028848,"size":13494,"snap":"2022-27-2022-33","text_gpt3_token_len":3479,"char_repetition_ratio":0.17798369,"word_repetition_ratio":0.6724788,"special_character_ratio":0.2558915,"punctuation_ratio":0.12399284,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96702373,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,1,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-07-03T11:20:55Z\",\"WARC-Record-ID\":\"<urn:uuid:15930779-f77a-4891-ad1b-f54c6e5a3891>\",\"Content-Length\":\"546193\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:8f578e19-2fda-4fc6-95a1-8364e36743ea>\",\"WARC-Concurrent-To\":\"<urn:uuid:cb96587e-534d-420f-b549-e4f56f98a858>\",\"WARC-IP-Address\":\"104.108.117.99\",\"WARC-Target-URI\":\"https://community.powerbi.com/t5/Desktop/Rankx-with-measure-doesn-t-seem-to-work-correctly/m-p/1756228\",\"WARC-Payload-Digest\":\"sha1:ILFKJ2JWHKFLGOTWA4J6RUVGDSRJTPK4\",\"WARC-Block-Digest\":\"sha1:6LJCWXAWWRFTFSUBTQVSSTORPZTEE3KZ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656104240553.67_warc_CC-MAIN-20220703104037-20220703134037-00455.warc.gz\"}"}