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

Learn from the best! Meet the four finalists headed to the FINALS of the Power BI Dataviz World Championships! Register now

Reply
alaynanich
Helper I
Helper I

Help with sales measure between two dates for custom calendar

Hi, I know this is a common ask but I'm stuck on this Dax measure. Basically, I need to calculate the sales variance betwen two dates in my date table for any slice/dimension my user needs. I'm used to using the dateadd or sameperiodlastyear functions for this, but I'm stuck since I'm using custom dates for our new fiscal year calendar.

 

My sales table comes in weekly with a week start column, and a list of stores and products

alaynanich_0-1767985931567.png

For my date table, I added a "this year" and 'last year" column for the comparison dates. Corpweekstart is connected to my date in this table.

alaynanich_1-1767986909472.png

 

I tried the below DAX for YOY sales which works perfeclt in a weekly view. But it appears blank when i look at it in total or as years. From what I've read, it's because of SELECTEDVALUE() and I think i need to use HASONEVALUE() or ISINSCOPE() instead? But I'm not sure if that's correct or what field to use it with?

 

VAR ty = SUM(Sales_Agg[sales])
VAR PrevWeekStart = SELECTEDVALUE ( 'adhoc_ace_date'[ly_week_start] )
VAR ly =
    IF (
        NOT ISBLANK ( PrevWeekStart ),
        CALCULATE ( SUM(Sales_Agg[sales]), TREATAS ( { PrevWeekStart }, 'adhoc_ace_date'[ace_date] ) ),
        BLANK ()
    )
RETURN IF (ISBLANK( ly ), BLANK (), (ty - ly))

 

alaynanich_2-1767987179826.png

It works fine when I look at it weekly, but not yearly (or any other dimension). 

2 ACCEPTED SOLUTIONS
FreemanZ
Community Champion
Community Champion

hi @alaynanich ,

 

try like:

 

YoY=

SUMX(

VALUES('adhoc_ace_date'[ace_date] ),

CALCULATE(

VAR ty = SUM(Sales_Agg[sales])

VAR PrevWeekStart = MAX ( 'adhoc_ace_date'[ly_week_start] )

VAR ly =

    IF (

        NOT ISBLANK ( PrevWeekStart ),

        CALCULATE ( SUM(Sales_Agg[sales]), TREATAS ( { PrevWeekStart }, 'adhoc_ace_date'[ace_date] ) ),

        BLANK ()

    )

RETURN IF (ISBLANK( ly ), BLANK (), (ty - ly))

)

View solution in original post

cengizhanarslan
Super User
Super User

Please try the formulas below:

 

Sales TY =
SUM ( Sales_Agg[sales] )

Sales LY =
CALCULATE(
    [Sales TY],
    TREATAS(
        VALUES ( 'adhoc_ace_date'[ly_week_start] ),
        'adhoc_ace_date'[ace_date]
    )
)

YoY Variance =
VAR ty = [Sales TY]
VAR ly = [Sales LY]
RETURN
IF ( ISBLANK(ly), BLANK(), ty - ly )

 

_________________________________________________________
If this helped, ✓ Mark as Solution | Kudos appreciated
Connect on LinkedIn | Follow on Medium
AI-assisted tools are used solely for wording support. All conclusions are independently reviewed.

View solution in original post

9 REPLIES 9
alaynanich
Helper I
Helper I

Both solutions below worked - thank you!

v-saisrao-msft
Community Support
Community Support

Hi @alaynanich,

Have you had a chance to review the solution we shared by @FBergamaschi @FreemanZ  @cengizhanarslan ? If the issue persists, feel free to reply so we can help further.

 

Thank you.

cengizhanarslan
Super User
Super User

Please try the formulas below:

 

Sales TY =
SUM ( Sales_Agg[sales] )

Sales LY =
CALCULATE(
    [Sales TY],
    TREATAS(
        VALUES ( 'adhoc_ace_date'[ly_week_start] ),
        'adhoc_ace_date'[ace_date]
    )
)

YoY Variance =
VAR ty = [Sales TY]
VAR ly = [Sales LY]
RETURN
IF ( ISBLANK(ly), BLANK(), ty - ly )

 

_________________________________________________________
If this helped, ✓ Mark as Solution | Kudos appreciated
Connect on LinkedIn | Follow on Medium
AI-assisted tools are used solely for wording support. All conclusions are independently reviewed.
FreemanZ
Community Champion
Community Champion

hi @alaynanich ,

 

try like:

 

YoY=

SUMX(

VALUES('adhoc_ace_date'[ace_date] ),

CALCULATE(

VAR ty = SUM(Sales_Agg[sales])

VAR PrevWeekStart = MAX ( 'adhoc_ace_date'[ly_week_start] )

VAR ly =

    IF (

        NOT ISBLANK ( PrevWeekStart ),

        CALCULATE ( SUM(Sales_Agg[sales]), TREATAS ( { PrevWeekStart }, 'adhoc_ace_date'[ace_date] ) ),

        BLANK ()

    )

RETURN IF (ISBLANK( ly ), BLANK (), (ty - ly))

)

GeraldGEmerick
Super User
Super User

@alaynanich SELECTEDVALUE will return blank by default if there are multiple values available (like in a total row). You can try using MAX or MIN instead.

@GeraldGEmerick Oh wow this was so simple to remove the blanks. Thanks!!! But the total row is still blank at the bottom of the tables - is there another simple fix for this?

Hi @alaynanich,

at the total I am not sure to get the meaning of the calculation and a YOY is undefined when there are multiple years.

 

What would be the meaning you want to give to that value at the matruix total? Then we can fix the DAX

 

If this helped, please consider giving kudos and mark as a solution

@me in replies or I'll lose your thread

Want 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

@FBergamaschi Hi, for the total YOY I want it to dynamically sum up whatever context is in the visual if that makes sense?  So on a weekly level, total the YOY for this year's sales vs the associated week from last year. On an annual level, full 2025 sales vs full 2024 sales. 

alaynanich_0-1767990316642.png

 

OK in this case I am not sure shy it still does not work

 

Can you share the pbix via private message so I can check it?

 

If this helped, please consider giving kudos and mark as a solution

@me in replies or I'll lose your thread

Want 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

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

February Power BI Update Carousel

Power BI Monthly Update - February 2026

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