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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
jackalmf
New Member

Issues with TOTALYTD & previous year calculations

Hi Power BI community,

 

I wrote some queries which I believe were working correctly last year but in the new year these do not seem to function as intended. There is a calendar table I created over the course of last year as well as using the DocDate from tables.

 

To explain the premise I am wanting to show total sales year to date (YTD)  minus any credits year to date.

I am then using 2 additional queries to calculate the same for the previous year to date and then comparing the 2 to see % increase/decrease.

 

These queries seemed to be working okay last year but after rolling over into the new year I am now not so sure.

 

My queries as follows:

 

TotalYTD-Credits = [TotalYTDexcVAT] - [TotalCreditsYTDExcVAT]

TotalYTDexcVAT = TOTALYTD(CALCULATE(SUM(OINV[DocTotal]) - SUM(OINV[VatSum])), OINV[DocDate])
TotalCreditsYTDExcVAT = TOTALYTD(
    CALCULATE(SUM(ORIN[DocTotal]) - SUM(ORIN[VatSum])),
    ORIN[DocDate]
    )
 
TotalPYTD-Credits = [TotalPYTDexcVAT] - [TotalCreditsPYTDexcVAT]
TotalPYTDexcVAT = TOTALYTD(
    CALCULATE(SUM(OINV[DocTotal]) - SUM(OINV[VatSum])),
    SAMEPERIODLASTYEAR('Calendar Table'[Date])
)
TotalCreditsPYTDexcVAT = TOTALYTD(
    CALCULATE(SUM(ORIN[DocTotal]) - SUM(ORIN[VatSum])),
    SAMEPERIODLASTYEAR(ORIN[DocDate])
)
 
To calculate the difference my query is:
 
TotalPrYR%-Credits = ([TotalYTD-Credits] - [TotalPYTD-Credits]) / [TotalPYTD-Credits]
 
The calendar table creation code as follows:
 
Calendar Table =
    VAR MinDate = YEAR ( MIN ( OINV[DocDate]) )
    VAR MaxDate = YEAR ( MAX ( OINV[DocDate]) )
    RETURN
    ADDCOLUMNS(
        FILTER (
   
        CALENDARAUTO(),
        YEAR ([Date] ) >= MinDate &&
        year ([Date] ) <= MaxDate
           ),

    "Year", YEAR([Date]),
    "Quarter", FORMAT ([Date], "\QQ" ),
    "Month", FORMAT ( [Date], "mmmm" ),
    "Month Number", MONTH ([Date])
)
 
Any help appreciated, perhaps there is something I am missing in my understanding here! 🙂 
 
Many thanks
1 REPLY 1
Anonymous
Not applicable

Hi @jackalmf ,

Could you please give me the sample data and the expected results, thank you very much.

Best Regards,

Xianda Tang

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

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.