Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Percent change by years in Direct Query

Hello, new to Power BI here.

I'm trying to obtain the percent changes of my data, by Years. The main issue I'm facing is that I'm using DirectQuery, and the DAX functions SAMEPERIODLASTYEAR and PREVIOUSYEAR are not available using DirectQuery, as per the documentation.

 

Here's a sample of how my data looks:

 

YearCategoryValue
200015.65
200023.52
200114.65
200120.84
200211.54
200225.59

 

 

Ideally, I'd have a line chart with the Years in the X axis, and the values in the Y axis. I'd use the Legend option to put the Category fields.

 

Any ideas on how to proceed?

  • Hi Anonymous ,

     

    The major issue was the "Year" Column which was not really a date.

    1. You should create a calculated column to replace "Year": 

     

    T_YEAR = DATE(Table[Year],1,1) 

     

    2. Create measure to get "value" of this year and last year:

     

    THIS YEAR = SUMX(Table, Table[Value])
    LAST_YEAR = CALCULATE(SUMX(Table,Table[Value]),PREVIOUSYEAR(Table[T_YEAR]))

     

    3. Use "DIVIDE" function to calculate percentage change: 

     

    Percent_Change = DIVIDE([THIS YEAR]-[LAST_YEAR],[LAST_YEAR],0)

     

     

    Best Regards,

    Liang

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

1 Reply

  • V-lianl-msft's avatar
    V-lianl-msft
    Community Support

    Hi Anonymous ,

     

    The major issue was the "Year" Column which was not really a date.

    1. You should create a calculated column to replace "Year": 

     

    T_YEAR = DATE(Table[Year],1,1) 

     

    2. Create measure to get "value" of this year and last year:

     

    THIS YEAR = SUMX(Table, Table[Value])
    LAST_YEAR = CALCULATE(SUMX(Table,Table[Value]),PREVIOUSYEAR(Table[T_YEAR]))

     

    3. Use "DIVIDE" function to calculate percentage change: 

     

    Percent_Change = DIVIDE([THIS YEAR]-[LAST_YEAR],[LAST_YEAR],0)

     

     

    Best Regards,

    Liang

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