Forum Discussion
Window Function with Month Number and Month Name
- 2 years ago
You're using ALLSELECTED inside of an iterator. Understanding what happens in that situation is highly complex so I recommend avoiding that if possible (by defining it as a variable outside of the iterator, for example).
Ignoring that, I think the core problem here is that the table you define inside WINDOW doesn't have any corresponding outer value to define the current row in your first example. That is, your WINDOW table is trying to sort on [Month Number Of Year] but that column doesn't exist in your table.
I've not tested this but it might work better:
SMA 3 Months = VAR _Months_ = SUMMARIZE ( ALLSELECTED ( 'Date' ), 'Date'[Month Number Of Year], 'Date'[English Month Name] ) RETURN AVERAGEX ( WINDOW ( -2, REL, 0, REL, _Months_, ORDERBY ( 'Date'[Month Number Of Year], ASC ) ), [Sum Sales Amount] )I've made three main changes.
- Moved ALLSELECTED outside of AVERAGEX.
- Included [English Month Name] in the table to use in the window function.
- Added and explicit ORDERBY argument.
You're using ALLSELECTED inside of an iterator. Understanding what happens in that situation is highly complex so I recommend avoiding that if possible (by defining it as a variable outside of the iterator, for example).
Ignoring that, I think the core problem here is that the table you define inside WINDOW doesn't have any corresponding outer value to define the current row in your first example. That is, your WINDOW table is trying to sort on [Month Number Of Year] but that column doesn't exist in your table.
I've not tested this but it might work better:
SMA 3 Months =
VAR _Months_ =
SUMMARIZE (
ALLSELECTED ( 'Date' ),
'Date'[Month Number Of Year],
'Date'[English Month Name]
)
RETURN
AVERAGEX (
WINDOW (
-2, REL,
0, REL,
_Months_,
ORDERBY ( 'Date'[Month Number Of Year], ASC )
),
[Sum Sales Amount]
)
I've made three main changes.
- Moved ALLSELECTED outside of AVERAGEX.
- Included [English Month Name] in the table to use in the window function.
- Added and explicit ORDERBY argument.