Forum Discussion
DAX Calculate context - creating own YTD
- 8 years ago
Your problem is the way in which you used STARTOFYEAR. It is a table function which triggers context transition of the row context created by the FILTER function. Your "Sales YTD Own2 wrong" code is:
FILTER ( ALL ( 'Calendar' ), 'Calendar'[Date] >= STARTOFYEAR ( 'Calendar'[Date] ) && 'Calendar'[Date] <= MAX ( 'Calendar'[Date] ) )and in it, STARTOFYEAR will always evaluate to first day of the year based on the currently iterated row of 'Calendar', so this part of the boolean clause will always be true for every row in 'Calendar' table (so in essense, this part of clause does nothing). Which means FILTER function only affected by the " 'Calendar'[Date] <= MAX ( 'Calendar'[Date] ) " . MAX is a scalar function and not a table function so it does not trigger context transition so it retrieves max date in current filter context based on what you have on rows of Matrix.
The "correct" longhand way of writing YTD formula is:
Sales YTD = CALCULATE ( [Total Sales], FILTER ( ALL ( 'Calendar' ), 'Calendar'[CalendarYear] = MAX ( 'Calendar'[CalendarYear] ) && 'Calendar'[Date] <= MAX ( 'Calendar'[Date] ) ) )Make sense?
I used the following formula, But it is showing some wrong data in opening. But the wrong value is not refkecting column total.
OE YTD = CALCULATE(
[OE],
FILTER (
ALL ( 'Central' ),
Central[Year] = MAX ( Central[Year] )
&& Central[Document Date] <= MAX ( Central[Document Date] )
)
)
NS YTD = CALCULATE(
[NS],
FILTER (
ALL ( 'Central' ),
Central[Year] = MAX ( Central[Year] )
&& Central[Document Date] <= MAX ( Central[Document Date] )
)
)
OH YTD = CALCULATE(
[OE] - [NS],
FILTER (
ALL ( 'Central' ),
Central[Year] = MAX ( Central[Year] )
&& Central[Document Date] <= MAX ( Central[Document Date] )
)
)
The table showing wrong data for OE in opening.
It is not reflecting in total.
Can you please help on this?
Anonymous You should provide more details - the definition of measures used here, and also showing the data model would help so we see the tables / relationships.
Is there a reason you wouldn't use the YTD functions in DAX? Is the result the same if you do?
And what is "Opening"? That isn't a conventional month/year so may well be the cause.
- Anonymous7 years agoNot applicable
This is my MTD data.
I want to create YTD of the same data below.
I have one extra data for January. Opening value is avilable for January. One column namely Document Month Name is avilable to differentiate the opening value. Please help on this.
If I use YTD function with Document Month Name, It will show MTD data only.
If I use YTD function with Calendar , it will show YTD data.
- Anonymous7 years agoNot applicable
Please go through my pbix file attached
- Anonymous7 years agoNot applicable
https://community.powerbi.com/t5/Quick-Measures-Gallery/Time-Intelligence-quot-The-Hard-Way-quot-TITHW/m-p/782559#M386https://community.powerbi.com/t5/Quick-Measures-Gallery/Time-Intelligence-quot-The-Hard-Way-quot-TITHW/m-p/782559#M386
Please go throgh the pbix file bro.