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
Anonymous
Not applicable

Referring Data from Previous Row in a Query

I have a table with two columns, Date and Average, that shows the the average of scores for each date.

 

Example:

DATE                      AVERAGE

05/26/2020            98.1

06/3/2020              94

 

I want to add another column that gives the % difference between the scores . How do I do this by referring data from the previous row?

2 ACCEPTED SOLUTIONS
AlB
Super User
Super User

Hi @Anonymous 

You might want to tweak the way you calculate the percentage change in the RETURN statement. You can convert the column to %

Calc column =
VAR previousDate_ =
    CALCULATE (
        MAX ( Table1[Date] ),
        ALL ( Table1 ),
        Table1[Date] < EARLIER ( Table1[Date] )
    )
VAR previousDateAvg_ =
    CALCULATE (
        MAX ( Table1[Average] ),
        ALL ( Table1 ),
        Table1[Date] = previousDate_
    )
VAR currentDateAvg_ = Table1[Average]
RETURN
    DIVIDE ( currentDateAvg - previousDateAvg_, previousDateAvg_ )

Please mark the question solved when done and consider giving kudos if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

 

SU18_powerbi_badge

 

View solution in original post

V-lianl-msft
Community Support
Community Support

Hi @Anonymous ,

 

You can also create a measure to get difference.

First, create an index column in the query editor to get the value of the previous "average".

test_index_col.PNG

Then create a measure like this:

Measure =
VAR current_index =
    MAX ( 'Table (3)'[Index] )
VAR last_average =
    CALCULATE (
        FIRSTNONBLANK ( 'Table (3)'[Average], 1 ),
        FILTER ( ALL ( 'Table (3)' ), 'Table (3)'[Index] = current_index - 1 )
    )
RETURN
    DIVIDE ( last_average, MAX ( 'Table (3)'[Average] ) )

test_Referring Data from Previous Row in a Query.PNG

 

Best Regards,
Liang
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

2 REPLIES 2
V-lianl-msft
Community Support
Community Support

Hi @Anonymous ,

 

You can also create a measure to get difference.

First, create an index column in the query editor to get the value of the previous "average".

test_index_col.PNG

Then create a measure like this:

Measure =
VAR current_index =
    MAX ( 'Table (3)'[Index] )
VAR last_average =
    CALCULATE (
        FIRSTNONBLANK ( 'Table (3)'[Average], 1 ),
        FILTER ( ALL ( 'Table (3)' ), 'Table (3)'[Index] = current_index - 1 )
    )
RETURN
    DIVIDE ( last_average, MAX ( 'Table (3)'[Average] ) )

test_Referring Data from Previous Row in a Query.PNG

 

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

AlB
Super User
Super User

Hi @Anonymous 

You might want to tweak the way you calculate the percentage change in the RETURN statement. You can convert the column to %

Calc column =
VAR previousDate_ =
    CALCULATE (
        MAX ( Table1[Date] ),
        ALL ( Table1 ),
        Table1[Date] < EARLIER ( Table1[Date] )
    )
VAR previousDateAvg_ =
    CALCULATE (
        MAX ( Table1[Average] ),
        ALL ( Table1 ),
        Table1[Date] = previousDate_
    )
VAR currentDateAvg_ = Table1[Average]
RETURN
    DIVIDE ( currentDateAvg - previousDateAvg_, previousDateAvg_ )

Please mark the question solved when done and consider giving kudos if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

 

SU18_powerbi_badge

 

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.