Forum Discussion
leolapa_br
2 years agoResolver II
Get values from a temporary table variable
Below is a piece of the fact table 'fTrans' that provides context to my question. Ticker Date Sale date related to respective purchase Days Transaction Shares Price Purchase total A...
- 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!
leolapa_br
2 years agoResolver II
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!