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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
mclaur_and_22
Frequent Visitor

DAX for YoY rate calculation

Hi everybody, 

 

I currently have a table where I want to see the YoY growth rate for different locations. The date I have been using is not continuous or has an specific pattern, so when I tried doing YoY calculation I always received the following error: "Function DATEADD expects a contiguous selection when the date column is not unique, has gaps...". Is there perhaps a way to calculate YoY rate for no continous dates?? 

The table I'm working with looks like this, where Nod is a location and the amount per year describes the sales:

dax1.png

What I would like to have is the percent difference table calculation that you can find in Tableau: 

dax2.jpg

 

The measures I used were:

 

YOY diff = 
VAR previousDateWithPrice =
    CALCULATE (
        MAX ( 'capa'[Dates]),
        FILTER ( ALL ( 'capa' ), 'capa'[Sales] > 0 && 'capa'[Dates] < MAX ( 'capa'[Dates] ) )
    )
VAR previousPrice =
    CALCULATE (
        SUM ('capa'[Dates] ),
        FILTER ( ALL ( 'capa' ), 'capa'[Dates] = previousDateWithPrice )
    )
RETURN
    DIVIDE ( SUM ( 'capa'[Sales] ) - previousPrice, previousPrice )

 

Thank you in advance!! 

 

1 ACCEPTED SOLUTION
v-yanjiang-msft
Community Support
Community Support

Hi @mclaur_and_22 ,

According to your description, if you just want to show the annual growth rate, here's my solution.

This is my sample data.

vkalyjmsft_0-1643946573487.png

I modify your formula like this:

YOY diff =
VAR previousDateWithPrice =
    CALCULATE (
        MAX ( 'capa'[Dates].[Year] ),
        FILTER (
            ALL ( 'capa' ),
            'capa'[Sales] > 0
                && 'capa'[Dates].[Year] < MAX ( 'capa'[Dates].[Year] )
        )
    )
VAR previousPrice =
    CALCULATE (
        SUM ( 'capa'[Sales] ),
        FILTER (
            ALL ( 'capa' ),
            'capa'[Nod] = MAX ( 'capa'[Nod] )
                && 'capa'[Dates].[Year] = previousDateWithPrice
        )
    )
RETURN
    DIVIDE ( SUM ( 'capa'[Sales] ) - previousPrice, previousPrice )

Get the correct result.

vkalyjmsft_1-1643946735645.png

I attach my sample below for reference.

 

Best Regards,
Community Support Team _ kalyj

 

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
v-yanjiang-msft
Community Support
Community Support

Hi @mclaur_and_22 ,

According to your description, if you just want to show the annual growth rate, here's my solution.

This is my sample data.

vkalyjmsft_0-1643946573487.png

I modify your formula like this:

YOY diff =
VAR previousDateWithPrice =
    CALCULATE (
        MAX ( 'capa'[Dates].[Year] ),
        FILTER (
            ALL ( 'capa' ),
            'capa'[Sales] > 0
                && 'capa'[Dates].[Year] < MAX ( 'capa'[Dates].[Year] )
        )
    )
VAR previousPrice =
    CALCULATE (
        SUM ( 'capa'[Sales] ),
        FILTER (
            ALL ( 'capa' ),
            'capa'[Nod] = MAX ( 'capa'[Nod] )
                && 'capa'[Dates].[Year] = previousDateWithPrice
        )
    )
RETURN
    DIVIDE ( SUM ( 'capa'[Sales] ) - previousPrice, previousPrice )

Get the correct result.

vkalyjmsft_1-1643946735645.png

I attach my sample below for reference.

 

Best Regards,
Community Support Team _ kalyj

 

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

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.