Forum Discussion

mclaur_and_22's avatar
mclaur_and_22
Frequent Visitor
4 years ago
Solved

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:

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

 

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!! 

 

  • 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.

    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.

    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.

     

1 Reply

  • 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.

    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.

    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.