Dsum formula results for quarters in previous year (1 Viewer)

UltimateNeo

New member
Local time
Today, 17:19
Joined
Apr 29, 2024
Messages
15
Hello, I am having trouble with this formula not giving me the q2 results of last year. It still give me the current year results

=DSum("[QTY]","[InspectionsT]","[TaskID]=3 And Datepart('q',Dateadd('yyyy', -1, [DateCompleted]))=2")

Any help would be greatly appreciated
 

UltimateNeo

New member
Local time
Today, 17:19
Joined
Apr 29, 2024
Messages
15
The part that is not working is this part =DateAdd('yyyy',-1,[DateCompleted]) i get #name?
 

Gasman

Enthusiastic Amateur
Local time
Today, 17:19
Joined
Sep 21, 2011
Messages
14,630
So did you look up the syntax?
 

UltimateNeo

New member
Local time
Today, 17:19
Joined
Apr 29, 2024
Messages
15
yeah it looked ok and the rest of the formula has been working with datecompleted field.
 

Gasman

Enthusiastic Amateur
Local time
Today, 17:19
Joined
Sep 21, 2011
Messages
14,630
It did not look OK to me. What country are you from?
Perhaps follow the examples?
 

UltimateNeo

New member
Local time
Today, 17:19
Joined
Apr 29, 2024
Messages
15
I have tried examples for example =DateAdd("yyyy",-1,[DateCompleted]) still does not like it. I have not seen any other examples with different syntax.
 

Gasman

Enthusiastic Amateur
Local time
Today, 17:19
Joined
Sep 21, 2011
Messages
14,630
So in the immediate window what does
? =DateAdd("yyyy",-1,[DateCompleted])
show you?
Does datecompleted always have a value?
 

UltimateNeo

New member
Local time
Today, 17:19
Joined
Apr 29, 2024
Messages
15
I solved it wit this =DSum("[QTY]","[InspectionsT]","[TaskID] = 3 And Datepart('q',[DateCompleted]) = 2 and Datepart('yyyy',[DateCompleted]) = Year(Date()) - 1") splitting the year and quarter
 

Gasman

Enthusiastic Amateur
Local time
Today, 17:19
Joined
Sep 21, 2011
Messages
14,630
What are you going to do when you want the next quarter?
 
Last edited:

UltimateNeo

New member
Local time
Today, 17:19
Joined
Apr 29, 2024
Messages
15
I have 8 textboxes each with different quarter in them. 4 for last year and 4 for this year on a form. As they are just calculated fields from the data in the table. I couldn't think of any other way to do it
 

Gasman

Enthusiastic Amateur
Local time
Today, 17:19
Joined
Sep 21, 2011
Messages
14,630
If that is what you need to see each time, then that will work.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 12:19
Joined
Feb 19, 2002
Messages
43,776
I have 8 textboxes each with different quarter in them. 4 for last year and 4 for this year on a form. As they are just calculated fields from the data in the table. I couldn't think of any other way to do it
There are solutions that won't require code changes every year if you are interested.
 

ebs17

Well-known member
Local time
Today, 18:19
Joined
Feb 7, 2020
Messages
2,031
Instead of creating a fireworks display with DSum, it is better to calculate everything in a database in one query.
Approach:
SQL:
SELECT
   Year(DateCompleted) AS QYear,
   DatePart('q', DateCompleted) AS Quarter,
   SUM(QTY) AS SumQty
FROM
   InspectionsT
WHERE
   DateCompleted BETWEEN DateSerial(Year(Date()) - 1, 1, 1)
      AND
   DateSerial(Year(Date()), 12, 31)
GROUP BY
   Year(DateCompleted),
   DatePart('q', DateCompleted)
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 02:19
Joined
Jan 20, 2009
Messages
12,866
Instead of creating a fireworks display with DSum, it is better to calculate everything in a database in one query.
Take notice of this solution. It not only does the whole job in a single query, it also allows an index on DateCompleted to be used. This will be massively better performing than any query (including a DSum) that has to apply functions to every record in the table before selecting.


It is important to understand and apply the principles of SARGable queries.
 

Users who are viewing this thread

Top Bottom