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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
dbowman
Frequent Visitor

Need Dax(measure) help. Calculate sum for dynamic YTD and PYTD

Using a filter, users are selecting a Year-Month value in format YYYY-MM.

This filter is coming for a custom column on the CALENDAR table created in PBI.

We have to columns on our report table visual for Clients YTD, and Clients PYTD.

YTD = Jan 1. through the Year-Month selected in the filer.

PYTD = YTD-12 months

 

My data is correct, but i can not comeup with a proper DAX formula to get the end SUM results correct.

Any help is appreciated!

 

Curently i am trying with

 

Clients YTD = CALCULATE(TOTALYTD(SUM(Metrics[Client Count])+0,'Calendar'[Date]))
Clients Prev YTD = TOTALYTD(SUM('Metrics'[Client Count])+0,DATEADD('Calendar'[Year-Month],-12,MONTH))
1 ACCEPTED SOLUTION
bolfri
Solution Sage
Solution Sage

Here is your measures:

Client Count = SUM(Metrics[Client Count])
YTD = TOTALYTD(SUM(Metrics[Client Count]),'Calendar'[Date])
PYTD = CALCULATE([YTD],SAMEPERIODLASTYEAR('Calendar'[Date]))
 
Results:
bolfri_0-1673998979294.png

 





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

Proud to be a Super User!




View solution in original post

4 REPLIES 4
bolfri
Solution Sage
Solution Sage

Here is your measures:

Client Count = SUM(Metrics[Client Count])
YTD = TOTALYTD(SUM(Metrics[Client Count]),'Calendar'[Date])
PYTD = CALCULATE([YTD],SAMEPERIODLASTYEAR('Calendar'[Date]))
 
Results:
bolfri_0-1673998979294.png

 





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

Proud to be a Super User!




SivaMani
Resident Rockstar
Resident Rockstar

@dbowman Try this,

  • YTD
YTD = TOTALYTD(SUM(Metrics[Client Count]),LASTDATE(Calendar[Date])
  • PY
PY = CALCULATE(SUM(Metrics[Client Count]),PREVIOUSYEAR(LASTDATE(Calendar[Date])), ALL(Calendar))

@SivaMani - Thank you for for quick response. Unfortunately, this did not solve it. Although better, the numbers are still off.

 

My results becoming back are:

YTD - 2

PY - 3

 

Data Table

IDYear-MonthClient Count
123452021-12
123452021-21
123452021-32
123452021-91
123452022-25
123452022-32
123452022-71

 

If i select in filter 2022-9

Expected results should be:

YTD - 8

PY - 6

Or selecting 2022-7

YTD - 8

PY - 5

 

Thanks for your help!

Hi ,  @dbowman 

According to your description , you want to "Calculate sum for dynamic YTD and PYTD".

Here are the steps you can refer to :
(1)This is my test data:
vyueyunzhmsft_0-1674010786988.png

(2)We need to click "New Table" to  create a date table as  a slicer:

Date = ADDCOLUMNS( CALENDAR(FIRSTDATE('Table'[Date]) ,DATE(2022,12,31)) , "Year_Month" ,FORMAT([Date],"YYYY-MM"),"Year_Month_num",YEAR([Date])*100+MONTH([Date]))

 

And we need to create a relationship between two tables by [Date].

 

(3)Then we need to create two measures:

PY = var _cur_date = MAX('Date'[Date]) return CALCULATE( SUM('Table'[Value]) , YEAR('Date'[Date])=YEAR(_cur_date)-1 && MONTH('Date'[Date])<= MONTH(_cur_date))

 

YTD = var _cur_year_month = MAX('Date'[Date]) return CALCULATE( SUM('Table'[Value]) , YEAR('Date'[Date])=YEAR(_cur_year_month) && MONTH('Date'[Date])<= MONTH(_cur_year_month))

 

(4)Then we can put the fields we need on the visual and we can meet your need:

vyueyunzhmsft_1-1674010879008.png

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

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

 

 

 

 

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 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.

Top Solution Authors