Forum Discussion

BeginnerBob's avatar
BeginnerBob
Frequent Visitor
9 years ago
Solved

Average for populated columns

Hi,   I have 2 tables: factPosition and dimDate. What I'm trying to do is calculate the Average for the closing price, for the last 5 days for each symbol:   TestSymbolAverage5Day:= AVERAGEX ( ...
  • OwenAuger's avatar
    OwenAuger
    9 years ago

    Make these two changes to your code:

     

    TestAverage5days :=
    AVERAGEX (
        TOPN (
            5,
            CALCULATETABLE (
                SUMMARIZE ( factEODPosition, dimDate[DateValue] ),
                DATESBETWEEN ( dimDate[DateValue], BLANK (), MAX ( dimDate[DateValue] ) )
            ),
            dimDate[DateValue]
        ),
        CALCULATE ( SUM ( [ClosePosition] ) )
    )
    • BLANK() is used so that DATESBETWEEN has now lower bound, and that the top 5 dates are taken from all dates so far.
    • CALCULATE is needed in the 2nd argument of AVERAGEX so that context transition occurs, converting the date row context into filter context (in my example I used a measure which is automatically wrapped in CALCULATE).
    • Not sure why dimDate[DateValue] was highlighted red - just confirming it's definitely a column of type Date?