Forum Discussion
Get values from a temporary table variable
- 3 years ago
lbendlin your approach sure provided the stepping stones for helping me get across the finish line.
Thanks very much for your valuable input!
Just a couple of tweaks to your code were needed to reach the goal:
- [Days] and [Purchase total] are measures instead of columns of table 'fTrans', so all it was needed was to remove 'fTrans' from both within SUMX.
- Your calculation provides the equivalent in Excel of a SUMPRODUCT between [Days] and [Purchase total], so all it was needed in order to get a proper weighted average of [Days] was to divide that by the SUM of [Purchase total], and I did that by wrapping it around CALCULATE with the same filters (at least to me it somewhat looked odd a CALCULATE within another CALCULATE, but that's the only way I know how to do it for that particular case - perhaps creating a VAR for that would be more within the best practice guidelines).
So the final code looks as follows:
Table = ADDCOLUMNS( SUMMARIZE( FILTER( fTrans, fTrans[Transaction] = "Sale" ), fTrans[Ticker], fTrans[Date] ), "WavgDays", VAR Ticker_Ref = [Ticker] VAR Date_Ref = [Date] RETURN CALCULATE( SUMX( fTrans, DIVIDE( [Days] * [Purchase total], CALCULATE( [Purchase total], ALL( fTrans ), fTrans[Ticker] = Ticker_Ref, fTrans[Sale date related to respective purchase] = Date_Ref ) ) ), ALL( fTrans ), fTrans[Ticker] = Ticker_Ref, fTrans[Sale date related to respective purchase] = Date_Ref ) )Thanks!
lbendlin that's the thing though, prior to creating that measure I did make sure to connect 'fWavgDays' calculated table to the the data model via a one-to-many relationship coming from the date table 'dDate'[Data] as shown below:
And looking at your originally suggested code, which I repost below, it looks like the date table 'dDate' is not being referenced, which makes sense since you didn't know I had a date table in my model. Do you think that could be the issue? And if so, should the below code be re-written or should I wire 'fTrans' to the calculated table 'fWavgDays' via [Date]? Such connection would have to be a many-to-many relationship so I'm not sure if that could bring up other issues to the model...
Table = ADDCOLUMNS(SUMMARIZE(FILTER(fTrans,[Transaction]="Sale"),[Ticker],[Date]),"WavgDays",var t = [Ticker] var d=[Date] return CALCULATE(sumx(fTrans,fTrans[Days]*fTrans[Purchase total]),all(fTrans),fTrans[Ticker]=t,fTrans[Sale date related to respective purchase]=d))Thanks!
Please provide sample data (with sensitive information removed) that covers your issue or question completely, in a usable format (not as a screenshot). Mention everything that is related to the issue. Leave out anything not related to the issue.
https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-Forum/ba-p/963216
Please show the expected outcome based on the sample data you provided.
https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447523
- leolapa_br3 years agoResolver II
lbendlin please find below the link to the shared .pbix file related to this issue. Please let me know in case it doesn't work.
I tried resetting the connection between 'fWavgsDays' calculated table and the rest of the data model via 'fTrans' with a many-to-many relationship from/to both [Date] columns (also had to get rid of the one-to-many connection with the date table in order to make the newly created connection active).
And it still didn't work as per the following screenshot (in blue are the hardcoded correct results). Now it's apparently summing up [WavgDays] for each [Date] for all [Ticker], so it looks like the correct connection setup is the one-to-many between 'fWavgsDays' and 'dDate' and the calculated table code somehow needs to reference 'dDate'[Data].