Get values from a temporary table variable in DAX

364 Views Asked by At

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
ABEV3 01/Mar/2021 04/Jun/2021 95 Purchase 77 $12.93 $995.61
ABEV3 04/May/2021 04/Jun/2021 31 Purchase 73 $13.60 $992.80
ABEV3 04/Jun/2021 Sale 150 $17.91
ABEV3 28/Jun/2021 29/Oct/2021 123 Purchase 62 $16.01 $992.62
ABEV3 29/Oct/2021 Sale 62 $15.62
ALSO3 07/Jan/2021 03/Feb/2021 27 Purchase 39 $25.12 $979.68
ALSO3 03/Feb/2021 Sale 39 $27.20
ALSO3 29/Jun/2021 30/Mar/2022 274 Purchase 36 $27.53 $991.08
ALSO3 07/Jan/2022 30/Mar/2022 82 Purchase 56 $17.85 $999.60
ALSO3 30/Mar/2022 Sale 92 $22.61
APER3 18/Nov/2021 22/Dec/2021 34 Purchase 33 $29.88 $986.04
APER3 22/Dec/2021 Sale 33 $33.40
APER3 14/Jun/2022 11/Aug/2022 58 Purchase 34 $29.14 $990.76
APER3 04/Aug/2022 11/Aug/2022 7 Purchase 42 $23.57 $989.94
APER3 11/Aug/2022 Sale 76 $30.33

As you can see for all rows whose [Transaction] column is a "Sale" the [Days] column has empty values.

The reason being that there are cases where a "Sale" transaction relates to more than one previous "Purchase" transaction, therefore a weighted average of [Days] needs to be calculated for those "Sale" rows.

So, my goal is to write up a measure that will provide a weighted average of [Days] for every [Transaction] "Sale" rows that are currently sitting empty, and in order to get there I started with this:

EVALUATE
VAR Supp_Tbl = 
    ADDCOLUMNS( 
        SUMMARIZE( 
            fTrans, 
            fTrans[Ticker], 
            fTrans[Date], 
            fTrans[Sale date related to respective purchase], 
            fTrans[Transaction], 
            "@Days", SUMX( 
                fTrans, 
                IF( 
                    fTrans[Transaction] = "Sale", 
                    BLANK(), 
                    INT( fTrans[Sale date related to respective purchase] - fTrans[Date] ) 
                ) 
            ), 
            "@PurchTotal", [Purchase total] 
        ), 
        "@WghtProdDaysPurch", [@Days] * [@PurchTotal] 
    )
VAR Days_Tbl = 
    ADDCOLUMNS( 
        GROUPBY( 
            Supp_Tbl, 
            fTrans[Ticker], 
            fTrans[Sale date related to respective purchase], 
            "@SumPurchTotal", SUMX( 
                CURRENTGROUP(), 
                [@PurchTotal] 
            ), 
            "@SumWghtProdDaysPurch", SUMX( 
                CURRENTGROUP(), 
                [@WghtProdDaysPurch] 
            ) 
        ), 
        "@WghtdAvgDays", INT( 
                DIVIDE( 
                [@SumWghtProdDaysPurch], 
                [@SumPurchTotal] 
            ) 
        ) 
    )
RETURN
Days_Tbl

And it returns the following temporary table VAR "Days_Tbl" whose highlighted [@WghtdAvgDays] provides said weighted average of [Days] that I need for those "Sale" transaction rows:

Ticker Sale date related to respective purchase @SumPurchTotal @SumWghtProdDaysPurch @WghtdAvgDays
ABEV3
ABEV3 6/4/2021 1988.41 125359.75 63
ABEV3 10/29/2021 992.62 122092.26 123
ALSO3
ALSO3 2/3/2021 979.68 26451.36 27
ALSO3 3/30/2022 1990.68 353523.12 177
APER3
APER3 12/22/2021 986.04 33525.36 34
APER3 8/11/2022 1980.7 64393.66 32

Now all I need is to get those values from [@WghtdAvgDays] under VAR "Days_Tbl" whenever [Sale date related to respective purchase] matches fTrans[Date] under each [Ticker].

And for that I first tried this:

LOOKUPVALUE( 
    [@WghtdAvgDays], 
    fTrans[Sale date related to respective purchase], 
    MAXX( 
        FILTER( 
            fTrans, 
            fTrans[Ticker] = EARLIER( fTrans[Ticker] ) && 
            fTrans[Date] = EARLIER( fTrans[Date] ) && 
            fTrans[Transaction] = "Sale" 
        ), 
        fTrans[Date] 
    ) 
)

But it right off the bat presents two issues:

  1. The column fTransacoes[Data da venda relacionada à respectiva compra] either doesn't exist or doesn't have a relationship to any table available in the current context (message given by PBI when committing the measure code).
  2. [@WghtdAvgDays] can't be used as the first argument of LOOKUPVALUE as it expects a fully qualified column (message provided by DAX Studio while attempting to troubleshoot the code).

How am I supposed to get the values from the [@WghtdAvgDays] column under the temporary table VAR "Days_Tbl" then?

Thanks in advance...

P.S.: I also tried the route of creating a third temp table with SUMMARIZECOLUMNS but it ends up aggregating the overall total of [Days] for all rows:

FILTER( 
    SUMMARIZECOLUMNS( 
        fTrans[Ticker], 
        fTrans[Date], 
        fTrans[Transaction], 
        "@Days", CALCULATE( 
            SUMX( 
                Days_Tbl, 
                [@WghtdAvgDays] 
            ), 
        fTrans[Sale date related to respective purchase] = fTrans[Date] 
        ) 
    ), 
    fTrans[Transaction] = "Sale" 
)
Ticker Date Transaction @Days
ABEV3 6/4/2021 Sale 30080
ABEV3 10/29/2021 Sale 30080
ALSO3 2/3/2021 Sale 30080
ALSO3 3/30/2022 Sale 30080
APER3 12/22/2021 Sale 30080
APER3 8/11/2022 Sale 30080
1

There are 1 best solutions below

0
leolapa On

With an input from an user in another forum and a couple of tweaks from his proposed code I was able to reach a calculated table that did the trick..

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 
            ) 
        )