Forum Discussion
Get values from a temporary table variable
- 2 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!
I created a measure that would allow me to plop that [WavgDays] column from your calculated table in whatever visual I may need (on the case below, a simple table), but I always keep getting the overall total of days:
| Ticker | Date | Days | Transaction | Shares | Price | Purchase total | Days average 1 | Days average 2 |
| ABEV3 | 01/Mar/2021 | 95 | Purchase | 77 | $12.93 | $995.61 | ||
| ABEV3 | 04/May/2021 | 31 | Purchase | 73 | $13.60 | $992.80 | ||
| ABEV3 | 04/Jun/2021 | Sale | 150 | $17.91 | 29,360 | 29,360 | ||
| ABEV3 | 28/Jun/2021 | 123 | Purchase | 62 | $16.01 | $992.62 | ||
| ABEV3 | 29/Oct/2021 | Sale | 62 | $15.62 | 29,360 | 29,360 | ||
| ALSO3 | 07/Jan/2021 | 27 | Purchase | 39 | $25.12 | $979.68 | ||
| ALSO3 | 03/Feb/2021 | Sale | 39 | $27.20 | 29,360 | 29,360 | ||
| ALSO3 | 29/Jun/2021 | 274 | Purchase | 36 | $27.53 | $991.08 | ||
| ALSO3 | 07/Jan/2022 | 82 | Purchase | 56 | $17.85 | $999.60 | ||
| ALSO3 | 30/Mar/2022 | Sale | 92 | $22.61 | 29,360 | 29,360 | ||
| APER3 | 18/Nov/2021 | 34 | Purchase | 33 | $29.88 | $986.04 | ||
| APER3 | 22/Dec/2021 | Sale | 33 | $33.40 | 29,360 | 29,360 | ||
| APER3 | 14/Jun/2022 | 58 | Purchase | 34 | $29.14 | $990.76 | ||
| APER3 | 04/Aug/2022 | 7 | Purchase | 42 | $23.57 | $989.94 | ||
| APER3 | 11/Aug/2022 | Sale | 76 | $30.33 | 29,360 | 29,360 |
I tried both these codes to no success.
Days average 1 =
IF(
VALUES( fTrans[Transaction] ) = "Sale",
SUMX(
fWavgDays,
fWavgDays[WavgDays]
)
)
___________________________________________________________
Days average 2 =
VAR Ticker_Ref = VALUES( fTrans[Ticker] )
VAR Date_Ref = VALUES( fTrans[Date] )
RETURN
CALCULATE(
IF(
VALUES( fTrans[Transaction] ) = "Sale",
SUM( fWavgDays[WavgDays] )
),
fTrans[Ticker] = Ticker_Ref,
fTrans[Date] = Date_Ref
)
What am I doing wrong?
[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.
why?
- leolapa_br2 years agoResolver II
Because both measures are located on another table I've got just to place the measures I create.
- lbendlin2 years agoSuper User
You will need to indicate what these measures do. You cannot convert a calculated column formula into a measure formula like that - you need to think deeply about the very different concepts, and most of the time you need to write a completely new measure from scratch.
- leolapa_br2 years agoResolver II
lbendlinI thought that once I came up with the calculated column I wanted to get, namely [WavgDays], I could create a measure using CALCULATE to iterate row by row through the table visual and pick up the value from [WavgDays] on the calculated table 'fWavgDays' to each "Sale" row on the table visual whose respective [Ticker] and [Date] match those on 'fWavgDays'.
Such measure would be something similar to a RELATED type process. Or talking in Excel terms, an INDEX/MATCH type deal, where the visual table is the destination and the source table on this case is 'fWavgDays'.