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
Anonymous
Not applicable

Time functions

Hi,

 

I have data Amount and Date in a table and wanted to calculate some time functions. I dont want to use any calendar table here. I derived Year and Month from the date field and using it for filters.

 

DateAmount
01/01/2021500
01/02/2021573
01/03/20214723
01/04/2021473
01/05/202157
01/06/2021869
01/07/2021472
01/08/2021887
01/09/2021739
01/10/2021273
01/11/20212087
01/12/2021738
01/01/2022830
01/02/2022245
01/03/20223267
01/04/2022734
01/05/2022265
01/06/2022257
01/07/20225489
01/08/2022378
01/09/20225478
01/10/20223267
01/11/2022439
01/12/20223278
01/01/20233289
01/02/2023389
01/03/20234590

 

I do have filters like Year and Month. When I select Year 2022 and Month APRIL

 

1) Selected Month and Year: I just used SUM(Amount) -- I am getting correct results.

 

2) Same selected Month and Year but Last Year:

CALCULATE(SUM(Amount),SAMEPERIODLASTYEAR(Date))  --- not sure why this is not giving correct results. I suppose to get 2021 APRIL data but it is showing BLANK.
 
3) YTD: My financial year starts from APRIL to MARCH when I selected 2022 and APRIL. I suppose to see 2022 APRIL to 2023 March.  not sure how to get this done.
 
Last Year YTD: When I select 2022 APRIL then I supposed to see 2021 APRIL to 22 MARCH. not sure how to get this done.
 
Appreciated your support.
 
Thanks,
1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Anonymous ,

Please refer to my pbix file to see if it helps you.

Create a measure.

Same selected Month and Year but Last Year =
VAR _date =
    SELECTEDVALUE ( 'Table'[Date] )
VAR _1yearbefore =
    EDATE ( _date, -12 )
RETURN
    CALCULATE (
        SUM ( 'Table'[Amount] ),
        FILTER ( ALL ( 'Table' ), 'Table'[Date] = _1yearbefore )
    )

Create a column and a measure about YTD.

SortCol_COLUMN =
VAR _year =
    YEAR ( 'Table'[Date] )
VAR _month =
    MONTH ( 'Table'[Date] )
VAR _RESULT =
    CONCATENATE (
        IF ( [Date].[MonthNo] > 3, _year, _year - 1 ),
        FORMAT ( IF ( _month > 3, _month - 3, _month + 9 ), "00" )
    )
RETURN
    LEFT ( _RESULT, 4 )
YTD =
CALCULATE (
    SUM ( 'Table'[Amount] ),
    FILTER (
        ALL ( 'Table' ),
        'Table'[SortCol_COLUMN] = SELECTEDVALUE ( 'Table'[SortCol_COLUMN] )
    )
)

vpollymsft_0-1655446914887.png

Best Regards

Community Support Team _ Polly

 

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

 

 

View solution in original post

1 REPLY 1
Anonymous
Not applicable

Hi @Anonymous ,

Please refer to my pbix file to see if it helps you.

Create a measure.

Same selected Month and Year but Last Year =
VAR _date =
    SELECTEDVALUE ( 'Table'[Date] )
VAR _1yearbefore =
    EDATE ( _date, -12 )
RETURN
    CALCULATE (
        SUM ( 'Table'[Amount] ),
        FILTER ( ALL ( 'Table' ), 'Table'[Date] = _1yearbefore )
    )

Create a column and a measure about YTD.

SortCol_COLUMN =
VAR _year =
    YEAR ( 'Table'[Date] )
VAR _month =
    MONTH ( 'Table'[Date] )
VAR _RESULT =
    CONCATENATE (
        IF ( [Date].[MonthNo] > 3, _year, _year - 1 ),
        FORMAT ( IF ( _month > 3, _month - 3, _month + 9 ), "00" )
    )
RETURN
    LEFT ( _RESULT, 4 )
YTD =
CALCULATE (
    SUM ( 'Table'[Amount] ),
    FILTER (
        ALL ( 'Table' ),
        'Table'[SortCol_COLUMN] = SELECTEDVALUE ( 'Table'[SortCol_COLUMN] )
    )
)

vpollymsft_0-1655446914887.png

Best Regards

Community Support Team _ Polly

 

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

 

 

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!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.