Forum Discussion
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:
| Year | Category | Value |
| 2000 | 1 | 5.65 |
| 2000 | 2 | 3.52 |
| 2001 | 1 | 4.65 |
| 2001 | 2 | 0.84 |
| 2002 | 1 | 1.54 |
| 2002 | 2 | 5.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-msftCommunity 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.