Forum Discussion
Measure to include current selection and rows above and below dynamically
- 8 years ago
Hey Mark,
the pbix file attached to this post (I'm sorry for the confusion, but maybe I will stick to onedrive links instead of fileuploads because they do not update automatically :-)) now contains another unrelated table (Years unrelated).
Basically the change was not that big, instead of using the SUM OF ALL YEARS, now I use the AMOUNT OF THE SELECTED YEAR to determine the ranking.
There are two new measures:
- Selected Year
Just contains the value of the selected year - SUM Amount by Selected Year
Calculates the Amount for the selected year
I adjusted the measure "rank Countries", it now uses the measure "SUM Amount by Selected Year", I also added the "old" measure" this is now called "rank Countrires All Years" for comparison reasons, but is seems that the ranking for all years equals the ranking for one paricular year, maybe my calculation is wrong, or it's the data :-)
Please provide Feedback if my adjustments provide the solution you require, if not, can you please provide feedback, how to check this, based on the data you provided.
Regards,
Tom
- Selected Year
Hey,
maybe the attached pbix file provides what you are looking for.
Please be aware that I unpivoted your sample data, this means I transformed from wide (many columns) to long (many rows)
As Greg_Deckler already mentioned an unrelated table is necessary to select the country.
Then then there are some measures
- SELECTED Country (can be hidden)
this measure returns the selected country from the unrelated table - SUM Amount across ALL Years (can be hidden)
this measure sums the value across all the years, this measure is the basis for the ranking - rank Countries (can be hidden)
this measure ranks the measure [SUM Amount across ALL Years] for all the countries - RANK of selected Country (can be hidden)
this measure returns the RANK for the selected country and is used in the final measure to filter the counries (below and above accordingly) - map Amount
this is the final Measure, basicall this measure works like this- create a table that contains the selected country from the unrelated table / slicer, the three countries that are above (a greater rank), and the three countries that are below (a smaller rank),
- use this table to filter the countries
This measure looks looks like this
map Amount =
var countriesAboveAndBelow =
UNION(
FILTER(
FIRSTNONBLANK('Table1'[Country],1)
,'Table1'[Country] = [Selected Country]
)
,
UNION(
SUMMARIZE(
FILTER(
ADDCOLUMNS(
GROUPBY(
'Table1'
,'Table1'[Country]
)
,"theRank", [rank Countries]
)
,[theRank] > [RANK of selected Country] && [theRank] <= [RANK of selected Country] + 3
)
,Table1[Country]
)
,SUMMARIZE(
FILTER(
ADDCOLUMNS(
GROUPBY(
'Table1'
,'Table1'[Country]
)
,"theRank", [rank Countries]
)
,[theRank] < [RANK of selected Country] && [theRank] >= [RANK of selected Country] - 3
)
,Table1[Country]
)
)
)
return
CALCULATE(
SUM(Table1[Amount])
,countriesAboveAndBelow
)Hopefully this is what you are looking for
Regards
Tom
Hi TomMartens,
Thanks for your swift reply, this works perfectly in terms of filtering and selecting the right sets of countries, also the data was originally unpivoted in the same way you have it which makes it easier, I pivoted it thinking that might be the best way.
One detail I forgot to include however (apologies) was that the ranking of the countries needs to be based on a given year, in this case the current year 2018 rather than a ranking based on the SUM across all years. I previously made a step in the query to sort largest to smallest on the '2018' column in the pivoted sample data to achieve this. Do you know of a way to achieve this based on the solution you have provided?
I'm hoping it's only a minor tweak to what you have already provided which is brilliant btw!
Many Thanks,
Mark.
- TomMartens8 years agoSuper User
Hey Mark,
the pbix file attached to this post (I'm sorry for the confusion, but maybe I will stick to onedrive links instead of fileuploads because they do not update automatically :-)) now contains another unrelated table (Years unrelated).
Basically the change was not that big, instead of using the SUM OF ALL YEARS, now I use the AMOUNT OF THE SELECTED YEAR to determine the ranking.
There are two new measures:
- Selected Year
Just contains the value of the selected year - SUM Amount by Selected Year
Calculates the Amount for the selected year
I adjusted the measure "rank Countries", it now uses the measure "SUM Amount by Selected Year", I also added the "old" measure" this is now called "rank Countrires All Years" for comparison reasons, but is seems that the ranking for all years equals the ranking for one paricular year, maybe my calculation is wrong, or it's the data :-)
Please provide Feedback if my adjustments provide the solution you require, if not, can you please provide feedback, how to check this, based on the data you provided.
Regards,
Tom
- TytlerJaxon8 years agoFrequent Visitor
Hey TomMartens,
Apologies, I may be being blind, did you mean to attach the file to your latest reply? I tried downloading the file you sent in the previous post but that doesn't have the changes you have just made for the year selection.
Regards,
Mark.
- TomMartens8 years agoSuper User
Hey Mark,
you are not blind :-) please excuse.
The newer version is now attached to my 2nd answer :-)
Cheers,
Tom
- Selected Year