Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
C4L84
Advocate II
Advocate II

TOTALYTD using variable year end date

Hi

I am using this formula for running total:

C4L84_0-1647623981765.png

No issues with this but I am also using a parameter based on month int:

C4L84_1-1647624079119.png

I need the year end date to change in the formula.

 

I have tried to use the power query editor to create a concatenated value:

[Query="
declare @StartMonthInt int; set @StartMonthInt = '"&Start_Month_Int&"'

declare @CurrentFY int; set @CurrentFY =
(
select
case when [MonthNum] < @StartMonthInt then datepart(yy,dateadd(year, -1, [DateFull]))

else year([DateFull]) end

from [CommRS_Core_Live].[dbo].[RS_Dates]

where
[DateFull] = cast(getdate() as date)
)

declare @FYStartDate date; set @FYStartDate =
(
select
min([DateFull])

from [CommRS_Core_Live].[dbo].[RS_Dates]

where
datepart(yy,[DateFull]) = @CurrentFY

and datepart(mm,[DateFull]) = @StartMonthInt
)

declare @FYEndDate date; set @FYEndDate = (select dateadd(dd, -1, dateadd(year, 1, @FYStartDate)))

--select '" & "' + cast(datepart(dd,@FYEndDate) as varchar(2)) + '/' + cast(datepart(mm,@FYEndDate) as varchar(2)) + '" & """' [FY end dd/mm]

select @FYEndDate [FY end]
"]),
#"Duplicated Column" = Table.DuplicateColumn(Source, "FY end", "FY end - Copy"),
#"Extracted Day" = Table.TransformColumns(#"Duplicated Column",{{"FY end - Copy", Date.Day, Int64.Type}}),
#"Changed Type" = Table.TransformColumnTypes(#"Extracted Day",{{"FY end - Copy", type text}}),
#"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"FY end - Copy", "FY end - Day"}}),
#"Duplicated Column1" = Table.DuplicateColumn(#"Renamed Columns", "FY end", "FY end - Copy"),
#"Extracted Month" = Table.TransformColumns(#"Duplicated Column1",{{"FY end - Copy", Date.Month, Int64.Type}}),
#"Changed Type1" = Table.TransformColumnTypes(#"Extracted Month",{{"FY end - Copy", type text}}),
#"Renamed Columns1" = Table.RenameColumns(#"Changed Type1",{{"FY end - Copy", "FY end - Month"}}),
#"Added Custom" = Table.AddColumn(#"Renamed Columns1", "Custom", each Text.Combine({[#"FY end - Day"],[#"FY end - Month"]},"/")),
#"Renamed Columns2" = Table.RenameColumns(#"Added Custom",{{"Custom", "FY end dd/mm"}})
in
#"Renamed Columns2"

 

I then passed this through to the formula like so:

C4L84_2-1647624231932.png

 

However it looks as though the formula is defaulting to 31/1 rather than 31/3 (in this instance where parameter value is 4, ie April):

C4L84_3-1647624364783.png

 

Do you have any idea how I can "trick" the TOTALYTD formula into accepting the concatenated value as a date value, or is there an alternative?

 

Cheers

 

 

1 ACCEPTED SOLUTION

Hey C4L84,
Can you try this? I used your pbix, and it looks like it works. I added a calculated column in your Calendar table:

 

Calendar Year = YEAR('Calendar'[Date])

 

Tutu_in_YYC_0-1647973543786.png

 

Then this measure:

Running Total Spend - Dynamic = 

VAR _month = FORMAT(MAX('FY end'[Date]), "M")
VAR _day = FORMAT(MAX('FY end'[Date]), "D")
VAR _endofyear = DATE(SELECTEDVALUE('Calendar'[Calendar Year]),_month,_day)
VAR _startofyear = DATE(SELECTEDVALUE('Calendar'[Calendar Year]),_month,_day) +1
VAR _startdatedynamic = IF( MAX('Calendar'[Date]) < _startofyear, EDATE(_startofyear,-12), _startofyear)
VAR _enddatedynamic = IF( MAX('Calendar'[Date]) > _endofyear, EDATE(_endofyear,12), _endofyear)
RETURN

CALCULATE(
SUM(Sheet1[Spend]),
FILTER(
ALL('Calendar'),
'Calendar'[Date] >= _startdatedynamic &&
'Calendar'[Date] <= _enddatedynamic &&
'Calendar'[Date] <= MAX('Calendar'[Date])
)
)


Compared above measure with the time intelligence DAX function, the yield same results

Tutu_in_YYC_1-1647974005489.png

 

Let me know how it goes.

View solution in original post

9 REPLIES 9
Tutu_in_YYC
Resident Rockstar
Resident Rockstar

Thanks for the pbicx, this one is tricky. I tried to broke it down to the bare functions, but still have issues trying to make it sustainable.

v-chenwuz-msft
Community Support
Community Support

Hi @C4L84 ,

 

You missed a argument befor "year_end_date", [filter].

TOTALYTD(<expression>,<dates>[,<filter>][,<year_end_date>])

 

So if you have a calendar table, please try this 

Measure = TOTALYTD(SUM('Table'[Values]),'calendar'[Date],ALL('calendar'),"6/30")
if no calendar table,
Measure 2= TOTALYTD(SUM('Table'[Values]),'Table'[Date],all('Table'),"6/30") 
result: 
 

vchenwuzmsft_1-1647930987432.png

Pbix in the end you can refer.

 

Best Regards

Community Support Team _ chenwu zhu

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

mbers find it more quickly.

I've included the ALL in the filter arguement but I am now getting this error:

"The True/False expression does not specify a column. Each True/False expressions used as a table filter expression must refer to exactly one column."

C4L84_0-1647944158821.png

 

I have a pbix that I could share but I cannot find a way to upload...?

I've uploaded the file to dropbox, here's the link

Hey C4L84,
Can you try this? I used your pbix, and it looks like it works. I added a calculated column in your Calendar table:

 

Calendar Year = YEAR('Calendar'[Date])

 

Tutu_in_YYC_0-1647973543786.png

 

Then this measure:

Running Total Spend - Dynamic = 

VAR _month = FORMAT(MAX('FY end'[Date]), "M")
VAR _day = FORMAT(MAX('FY end'[Date]), "D")
VAR _endofyear = DATE(SELECTEDVALUE('Calendar'[Calendar Year]),_month,_day)
VAR _startofyear = DATE(SELECTEDVALUE('Calendar'[Calendar Year]),_month,_day) +1
VAR _startdatedynamic = IF( MAX('Calendar'[Date]) < _startofyear, EDATE(_startofyear,-12), _startofyear)
VAR _enddatedynamic = IF( MAX('Calendar'[Date]) > _endofyear, EDATE(_endofyear,12), _endofyear)
RETURN

CALCULATE(
SUM(Sheet1[Spend]),
FILTER(
ALL('Calendar'),
'Calendar'[Date] >= _startdatedynamic &&
'Calendar'[Date] <= _enddatedynamic &&
'Calendar'[Date] <= MAX('Calendar'[Date])
)
)


Compared above measure with the time intelligence DAX function, the yield same results

Tutu_in_YYC_1-1647974005489.png

 

Let me know how it goes.

Hi Tutu_in_YYC this is excellent! Thank you very much for your assistance - it's much appreciated! Well done

C4L84
Advocate II
Advocate II

Thanks for the suggestion Tutu_in_YYC, I tried formatting as a variable but got this error:

C4L84_0-1647872942786.png

"The True/False expression does not specify a column. Each True/False expressions used as a table filter expression must refer to exactly one column."

Unofrtunately, I am struggling to find out what this actually means and how to resolve!

Thanks for letting me know. I tried the syntax and you are right, it failed. Somehow it doesnt recognize the data type. I am looking into this.

Tutu_in_YYC
Resident Rockstar
Resident Rockstar

Im curious now, can you try this? Since the end date is of type text i.e "31/01". Use the function FORMAT to convert your date to text.

 

Running Total Spend =
VAR _EndDate = FORMAT( MAX('FY end'[Date]), "DD/MM")
RETURN

TOTALYTD( SUM('Invoiced Sales'[Period]), 'Calendar'[Date], _EndDate)

You may need to tweak this according to the date table you have

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors