Hi, it's a long problem but I hope to explain it well. Let's begin with the raw data:
(Apologies for not being able to format the table properly here.)
|Date||Group||Parent Group||IrrelevantColumn||Return||Return Index||Cumulative Returns Index||Prev_Month_Cumulative_Index|
To begin with, the columns [Cumulative_Returns_Index] and [Prev_Month_Cumulative_Index] are not part of the raw data, but I created them as helper columns in Excel to make it easier to explain what the results should be. If possible, please let me know how to create them either as calculated columns, or have everything done as measures.
I would like to create a measure that will take into account a date range (defined via a slicer), and then calculate the cumulative returns of the group, rebased on the earliest date in the date range. I've broken down the problem as much as I can to hopefully try to make it easier to get some help:
The expected output, together with the other groups present (using just A, B and C to prevent the list from getting too long), should look like this:
If date range of January - March is selected for groups A&B:
If date range of February - March is selected for groups A, B & C:
Things I've tried:
I've created the [Cumulative_Returns_Index] and [Prev_Month_Cumulative_Index] via Python before importing the data to PowerBI (but would like to process the raw data if possible), and have used many hours to come up with the following code that didn't work when more than 1 group was present in the visualisation (table). I suspect it being due to multiple ref_indexes being returned, but PowerBI somehow couldn't identify the correct groups to use it for via the row context provided in the visualization.
The following is the code anyway, if it helps:
Rebal_Cumu_Returns_AllSel = VAR ref_index = CALCULATE( VALUES( table[Prev_Mth_Cumulative_Returns_Index]), FILTER( ALLSELECTED(table), table[Date] = MINX(ALLSELECTED(table), table[Date])) ) RETURN DIVIDE(SELECTEDVALUE(table[Cumulative_Returns_Index]), ref_index)
Solved! Go to Solution.
Hi, thanks for providing an answer. Just wanted to clarify - does Date[Year-Month] indicate that I have to create a table that summarises the list of dates I have in my fact table into the YYYY-MM format?
In addition, in line 40 above, is ALLSELECTED(Dates) supposed to be ALLSELECTED(DateTable), where DateTable refers to the aforementioned table created above in the YYYY-MM format?
To answer your very initial question, yes the hierarchy is a natural one. Does that mean the variable onlyOneParentGroupVisible is no longer required?
Hi just wanted to add - since the fact table by nature has a unique months across all years, it seemed like the date table could be dropped as well. I've amended the formula accordingly, and it seems like it works. Thanks @daxer !
The final formula I used was:
Cumulative_Returns_Index_Rebased = VAR isGroupInScope = ISINSCOPE( table[Group] ) VAR shouldCalculate = True && isGroupInScope VAR Result = IF( shouldCalculate, VAR firstCumulativeReturnsIndex = MAXX( CALCULATETABLE( TOPN( 1, table, table[Date], ASC ), ALLSELECTED( table[Date] ) ), table[Cumulative_Returns_Index] ) VAR currentCumulativeIndex = SELECTEDVALUE( table[Cumulative_Returns_Index] ) VAR relativeCumulativeIndex = DIVIDE( currentCumulativeIndex, firstCumulativeReturnsIndex ) RETURN relativeCumulativeIndex ) RETURN Result
If there are any concerns regarding the changes I've made (to exclude the creation of a date table and the checking of the parent group) that you think could potentially lead to problems down the line, do let me know.
All I can tell you is this. If you have a model where you don't have separate tables for dimensions, especially the date/time dimension... then 100% you're doing it totally wrong. Don't be surprised when you one day you'll discover that your figures not always are correct. But since it's very difficult to immediately spot the problems you'll certainly have one day (because this depends - surprise, surprise! - on the distribution of data in columns), you'll be basing your decisions on wrong figures.
It's your choice not to follow my advice. On www.sqlbi.com there's is an article by Alberto Ferrari that explains why one-table model will in certain circumstances yield wrong figures and you can't do anything about it apart from... building a correct model, which means STAR-SCHEMA.
And yes, if the hierarchy is natural, then you can drop the condition on Parent Group being in scope.
First question. Is the hierarchy Parent Group -> Group a natural one? A natural hierarchy is one that has unique leaves. So, for instance, you can't have any leaf (think: Group) below 2 different parent groups. Is this true? Because if not, then you have to say how you want to calculate when the same group, say A, is visible in the current context but no Parent Group has been selected.
Check out new user group experience and if you are a leader please create your group!
On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks
Mark your calendars and join us for our next Power BI Dev Camp!