Forum Discussion
Summarize virtual table
- 1 year ago
Hi FotFly - your DAX expression to ensure the grouping within the SUMMARIZE is scoped correctly
Modified the dax as below:
VAR Trans =
ADDCOLUMNS(
Transactions,
"Weight",
DIVIDE(
DATEDIFF(Transactions[EffectiveDate], Transactions[AsOfDate], DAY),
DATEDIFF(EOMONTH(Transactions[AsOfDate], -3), Transactions[AsOfDate], DAY)
),
"WeightedTrans",
DIVIDE(
DATEDIFF(Transactions[EffectiveDate], Transactions[AsOfDate], DAY),
DATEDIFF(EOMONTH(Transactions[AsOfDate], -3), Transactions[AsOfDate], DAY)
) * Transactions[CashAmount $]
)VAR SumWeightTrans =
SUMMARIZE(
Trans,
Trans[AsOfDate],
"SumWeightedTrans",
SUMX(
FILTER(Trans, Trans[AsOfDate] = EARLIER(Trans[AsOfDate])),
[WeightedTrans]
)
)RETURN
SumWeightTransI hope this result in the correct sum of WeightedTrans grouped by AsOfDate.
- 1 year ago
Try
SummaryTable = VAR Trans = ADDCOLUMNS ( SUMMARIZE ( Transactions, Transactions[EffectiveDate], Transactions[AsOfDate] ), "@Cash Amount $", CALCULATE ( SUM ( Transactions[CashAmount $] ) ), "@WeightedTrans", DIVIDE ( DATEDIFF ( Transactions[EffectiveDate], Transactions[AsOfDate], DAY ), DATEDIFF ( EOMONTH ( Transactions[AsOfDate], -3 ), Transactions[AsOfDate], DAY ) ) * [@CashAmount $] ) VAR SumWeightTrans = GROUPBY ( Trans, [AsOfDate], "@SumWeightedTrans", SUMX ( CURRENTGROUP (), [@WeightedTrans] ) ) RETURN SumWeightReturnsThe main point is to use GROUPBY rather than the second SUMMARIZE, but I've also tweaked the code a bit.
You should never use SUMMARIZE to add calculated columns, just use that for grouping and use ADDCOLUMNS to add the new columns you need.
I've also removed the Weight column as it wasn't being used, so there's no point calculating it.
Finally, I use @ in column names in temporary tables, so that they are easily distinguishable from columns or measures in the model.
Try
SummaryTable =
VAR Trans =
ADDCOLUMNS (
SUMMARIZE ( Transactions, Transactions[EffectiveDate], Transactions[AsOfDate] ),
"@Cash Amount $", CALCULATE ( SUM ( Transactions[CashAmount $] ) ),
"@WeightedTrans",
DIVIDE (
DATEDIFF ( Transactions[EffectiveDate], Transactions[AsOfDate], DAY ),
DATEDIFF ( EOMONTH ( Transactions[AsOfDate], -3 ), Transactions[AsOfDate], DAY )
) * [@CashAmount $]
)
VAR SumWeightTrans =
GROUPBY (
Trans,
[AsOfDate],
"@SumWeightedTrans", SUMX ( CURRENTGROUP (), [@WeightedTrans] )
)
RETURN
SumWeightReturns
The main point is to use GROUPBY rather than the second SUMMARIZE, but I've also tweaked the code a bit.
You should never use SUMMARIZE to add calculated columns, just use that for grouping and use ADDCOLUMNS to add the new columns you need.
I've also removed the Weight column as it wasn't being used, so there's no point calculating it.
Finally, I use @ in column names in temporary tables, so that they are easily distinguishable from columns or measures in the model.
Thank you very much! I will test is out. This is a different approach that I havent thought.