cancel
Showing results for 
Search instead for 
Did you mean: 
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-kalyj-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-kalyj-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
June 2022 update 768X460.jpg

Check it out!

Click here to read more about the June 2022 updates!

Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Power BI Dev Camp Session 23 768x460.jpg

Check it Out!

Mark your calendars and join us on Thursday, June 30 at 11a PDT for a great session with Ted Pattison!

June 20 episode 7 with aka link 768x460.jpg

The Power BI Community Show

Join us on June 20 at 11 am PDT when Kim Manis shares the latest on Azure Synapse analytics, the Microsoft Intelligent Data Platform, and notable Power BI Updates from Microsoft Build 2022.

Top Solution Authors
Top Kudoed Authors