Don't miss your chance to take the Fabric Data Engineer (DP-700) exam on us!
Learn moreWe've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now
Hi,
I am trying to find a substitute for SAMEPERIODLASTYEAR() to calculate Total Sales YTD PY, which will be working also for non contiguous selections (to avoid Error fetching data for this visual).
I have found a solution (Total Sales YTD PY (2)), but it seems to be quite harsh. Can you help me optimize Total Sales YTD PY (2)?
FROM (not working for non contiguos selectios):
IDEA (working for non contiguos selectios):
Solved! Go to Solution.
Sure, please find my changes implemented to Total Sales YTD PY (2) thanks to the comments in this thread.
Total Sales YTD PY (2)=
[...]VAR PreviusYearMaxSelectedDateincludingLunarYear = IF( PreviousYearMaxSelectedDate = DATE(PreviousYearMaxSelected,2,28), DATE(PreviousYearMaxSelected,2,29), PreviousYearMaxSelectedDate )
VAR PreviusYearMaxSelectedDate_IncludingLunarYear =
IF(
MaxSelectedDate = DATE(YEAR(MaxSelectedDate),2,28),
EDATE(PreviousYearMaxSelectedDate,0),
PreviousYearMaxSelectedDate
)
[...]VAR TotalSalesYTDPY = TOTALYTD( [Total Sales], DATESBETWEEN( Calendar[Calendar Date], StartDateOfPreviousYear, PreviusYearMaxSelectedDateincludingLunarYear))
VAR TotalSalesYTDPY =
CALCULATE(
[Total Shipments],
DATESBETWEEN(
Calendar[Calendar Date],
StartDateOfPreviousYear,
PreviusYearMaxSelectedDate_IncludingLunarYear))
[...]
Thank you ALL for your inputs! Thanks to your comments I was able to find a way to re-write my measue.
Hi @karo ,
We really appreciate your efforts and for letting us know the update on the issue.
If a solution has been found, it would be greatly appreciated if you could share your insights with the community. This would be helpful for other members who may encounter similar issues.
Thank you for your understanding and assistance.
Sure, please find my changes implemented to Total Sales YTD PY (2) thanks to the comments in this thread.
Total Sales YTD PY (2)=
[...]VAR PreviusYearMaxSelectedDateincludingLunarYear = IF( PreviousYearMaxSelectedDate = DATE(PreviousYearMaxSelected,2,28), DATE(PreviousYearMaxSelected,2,29), PreviousYearMaxSelectedDate )
VAR PreviusYearMaxSelectedDate_IncludingLunarYear =
IF(
MaxSelectedDate = DATE(YEAR(MaxSelectedDate),2,28),
EDATE(PreviousYearMaxSelectedDate,0),
PreviousYearMaxSelectedDate
)
[...]VAR TotalSalesYTDPY = TOTALYTD( [Total Sales], DATESBETWEEN( Calendar[Calendar Date], StartDateOfPreviousYear, PreviusYearMaxSelectedDateincludingLunarYear))
VAR TotalSalesYTDPY =
CALCULATE(
[Total Shipments],
DATESBETWEEN(
Calendar[Calendar Date],
StartDateOfPreviousYear,
PreviusYearMaxSelectedDate_IncludingLunarYear))
[...]
Hi @karo ,
Glad you were able to resolve your issue.
Please continue using fabric community forum for your further assistance.
Thank you
Hi @karo,
what are you looking for? Performance improvement? Shorter code? Bug fixes?
Without this information, I am not sure how to help you.
Surely, instead of writing this
If this helped, please consider giving kudos and mark as a solution
@me in replies or I'll lose your threadWant to check your DAX skills? Answer my biweekly DAX challenges on the kubisco Linkedin page
Consider voting this Power BI idea
Francesco Bergamaschi
MBA, M.Eng, M.Econ, Professor of BI
I refined your code with AI assistance (so please check carefully). I am pretty sure you could use EOMONTH() instead of your lunar year calculations. Let me know if this throws up errors.
Total Sales YTD PY =
VAR MaxSelectedDate =
MAXX(
ALLSELECTED('Calendar'),
Calendar[Calendar Date]
)
VAR PreviousYear = YEAR(MaxSelectedDate) - 1
VAR YTDEndDatePY =
DATE(
PreviousYear,
MONTH(MaxSelectedDate),
DAY(
EOMONTH(
DATE(PreviousYear, MONTH(MaxSelectedDate), 1),
0
)
)
)
VAR TotalSalesYTDPY =
TOTALYTD(
[Total Sales],
DATESBETWEEN(
Calendar[Calendar Date],
DATE(PreviousYear, 1, 1),
YTDEndDatePY
)
)
RETURN
IF(
ISBLANK(TotalSalesYTDPY),
"N/A for PY",
TotalSalesYTDPY
)
hi @karo ,
try like:
Total Sales YTD PY (2)=
VAR MaxSelectedDate =
MAXX(
ALLSELECTED('Calendar'),
Calendar[Calendar Date]
)
VAR PreviousYearMaxSelected = YEAR(MaxSelectedDate) - 1
VAR PreviousYearMaxSelectedDate = DATE(PreviousYearMaxSelected, MONTH(MaxSelectedDate), DAY(MaxSelectedDate))
VAR PreviusYearMaxSelectedDateincludingLunarYear =
IF(
PreviousYearMaxSelectedDate = DATE(PreviousYearMaxSelected,2,28),
DATE(PreviousYearMaxSelected,2,29),
PreviousYearMaxSelectedDate
)
VAR StartDateOfPreviousYear = DATE(PreviousYearMaxSelected,1,1)
VAR TotalSalesYTDPY =
CALCULATE(
[Total Sales],
FILTER(
ALLSELECTED(Calendar[Calendar Date]),
Calendar[Calendar Date]>=StartDateOfPreviousYear
&& Calendar[Calendar Date]<= PreviusYearMaxSelectedDateincludingLunarYear)
)
RETURN
IF(
ISBLANK(TotalSalesYTDPY),
"N/A for PY",
TotalSalesYTDPY
)
This builds PY YTD up to the same day as your current selection’s max date doesn’t require a contiguous date range.
Why this works:
If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.
A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.
Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.
| User | Count |
|---|---|
| 55 | |
| 34 | |
| 32 | |
| 19 | |
| 17 |
| User | Count |
|---|---|
| 75 | |
| 72 | |
| 38 | |
| 35 | |
| 25 |