Forum Discussion
Rustin788
2 years agoFrequent Visitor
YTD Calculation without Date Field
Hey, I'm trying to figure out the best way to have YTD calculations in data that is using a fiscal calendar that puts days in different months. My dataset (SalesData) has a Fiscal-Month field as wel...
gmsamborn
2 years agoSuper User
Hi Rustin788
I created the below Calendar table and a column to add to your Sales table (in Power Query). This is for a date field to be used in a relationship with the Calendar table.
Power Query - Add this column to your Sales table.
= Text.From([FISCAL_YEAR]) &
"-" &
[FISCAL_MONTH] &
"-15"
DAX Calendar table
/* Table */
Calendar = CALENDAR( DATE( 2022, 1, 1 ), DATE( 2024, 12, 31)
/* Calculated Columns */
Year = YEAR( [Date] )
Week =
VAR _Start =
CALCULATE(
MIN( 'Calendar'[Date] ),
ALL( 'Calendar' )
)
RETURN
INT( ( [Date] - _Start ) / 7 ) + 1
Week of Year = MOD( [Week], 52.0001 )
Quarter = INT( [Week of Year] / 13.0001 ) + 1
Week of Quarter = MOD( [Week of Year], 13.0001 )
Period of Quarter =
SWITCH(
TRUE(),
[Week of Quarter] <= 5, 1,
[Week of Quarter] <= 9, 2,
3
)
Period No =
SWITCH(
TRUE(),
[Quarter] = 1 && [Period of Quarter] = 1, 1,
[Quarter] = 1 && [Period of Quarter] = 2, 2,
[Quarter] = 1 && [Period of Quarter] = 3, 3,
[Quarter] = 2 && [Period of Quarter] = 1, 4,
[Quarter] = 2 && [Period of Quarter] = 2, 5,
[Quarter] = 2 && [Period of Quarter] = 3, 6,
[Quarter] = 3 && [Period of Quarter] = 1, 7,
[Quarter] = 3 && [Period of Quarter] = 2, 8,
[Quarter] = 3 && [Period of Quarter] = 3, 9,
[Quarter] = 4 && [Period of Quarter] = 1, 10,
[Quarter] = 4 && [Period of Quarter] = 2, 11,
[Quarter] = 4 && [Period of Quarter] = 3, 12,
0
)
Period =
SWITCH(
[Period No],
1, "JAN",
2, "FEB",
3, "MAR",
4, "APR",
5, "MAY",
6, "JUN",
7, "JUL",
8, "AUG",
9, "SEP",
10, "OCT",
11, "NOV",
12, "DEC",
"Error"
)
Fiscal Year =
IF(
MONTH( [Date] ) = 12 && [Period] = "JAN",
YEAR( [Date] ) + 1,
YEAR( [Date] )
)
YTD Sales =
VAR _StartofYear =
CALCULATE(
MIN( 'Calendar'[Date] ),
ALL( 'Calendar'[Date] ),
'Calendar'[Fiscal Year] = MAX( 'Calendar'[Fiscal Year] )
)
VAR _YTD =
CALCULATE(
[Sales],
'Calendar'[Date] >= _StartofYear
&& 'Calendar'[Date] <= SELECTEDVALUE( 'Calendar'[Date] )
)
RETURN
_YTD
- Rustin7882 years agoFrequent Visitor
Hey, I get a big error message that starts with "The syntax for 'Year' is incorrect.". Would it be easier if I didn't need a true YTD calendar but rather something that would go through the previous period? So even if I am running it on 12/20, I just need the YTD data to be Jan-November.
- gmsamborn2 years agoSuper User
Which calculated column were you creating when you got the error message?