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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
ganiu_s
New Member

Previous year calculation

I have this query that is meant to calculate the data from last year every time i select the current year it should get previous data without having to hard code the year into the dax 

/*
VAR TodayDate = TODAY() - 364
VAR LastSaturday = TodayDate - WEEKDAY(TodayDate, 2) -- Ensures the last Saturday
VAR CurrentFinancialYear = SELECTEDVALUE(trading_data[DepartureFinancialYear])
 
VAR CurrentYearStart = LEFT(CurrentFinancialYear, 4)
VAR CurrentYearEnd = RIGHT(CurrentFinancialYear, 2)
VAR PreviousYearStart = VALUE(CurrentYearStart) - 1
VAR PreviousYearEnd = VALUE(CurrentYearEnd) - 1
VAR PreviousFinancialYear = FORMAT(PreviousYearStart, "0000") & "/" & FORMAT(PreviousYearEnd, "00")

RETURN
    CALCULATE(
        SUM(data[productCount]),
        FILTER(
           data,
            data[HistoryDateFrom] <= LastSaturday &&
            data[HistoryDateTo] >= LastSaturday &&
            data[Status] IN { "ok", "verify" } &&
           data[DepartureFinancialYear] = PreviousFinancialYear
        )
    )

*/
The query above returns blank  but this other query below returns values but when filtered 2024/25 it returns information for2023/24 that is incomplete 

Essentially its not going back to 2023/24 to obtain the records 
VAR TodayDate = TODAY() - 364
VAR LastSaturday = TodayDate - WEEKDAY(TodayDate)
                   -- DATE(2023, 5, 27)
--VAR WeekendDate = LastSaturday - 7
RETURN
    CALCULATE (
        Sum(trading_data[ProductCount]),
        FILTER (
            'data',
            data'[HistoryDateFrom] <= LastSaturday  &&
            'trading_data'[HistoryDateTo] >= LastSaturday
           -- && trading_data[ConfirmedDate]>= WeekStartDate && trading_data[ConfirmedDate]<= LastSaturday
           && 'trading_data'[Status] IN { "ok", "verified" }
           && trading_data[DepartureFinancialYear] = "2023/24" (needs to be dynamic )
        )

)


I have attched output as a further example 

selecting 2024/25 i want to be able to see data for 2023/24  and available data for 2024/25 in 2023/24

ganiu_s_3-1716913342497.png

 




ganiu_s_0-1716913499620.png

 


 



1 REPLY 1
rajendraongole1
Super User
Super User

Hi @ganiu_s  - Create a Date Table (if not already done) using new table Dax.

Mark DateTable as a Date table by going to Table tools >> Mark as Date Table, and select the Date column.

create a new measure

VolumePreviousYear =
VAR SelectedYear = SELECTEDVALUE('DateTable'[Year]) - 365
RETURN
CALCULATE(
COUNT('YourTableName'[ID]),
FILTER(
ALL('DateTable'),
'DateTable'[Year] = SelectedYear
)
)

 

Did I answer your question? Mark my post as a solution! This will help others on the forum!
Appreciate your Kudos!!





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.