Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

How to calculate delta (change) between the most recent and previous event(s)

Hi all,

 

In the early stages of building a new data model and report. We will have a table that a Quantity column that report total cumlative values, not incremental values. I would like calculate the change between each "event". There will be a date column, but the events are not at a specific time interval. Furthermore there are categories/classifications of each "event". Here is a simple sample table represenative of the data (the data model that doesn't exsist yet, will be a full star schema):

 

Sample Table

IDOptionCategoryDateQuantityDelta (calculated)
1Option1CatA10/27/20205000
2Option1CatB10/27/202010000
3Option1CatA10/27/20251000500
4Option1CatB10/27/20301500500
5Option1CatA10/27/203545003500
6Option1CatB10/27/20351250-250
7Option1CatA10/27/2050112006700
8Option1CatB10/27/20501700450
9Option2CatA10/27/20205000
10Option2CatB10/27/202010000
11Option2CatA10/27/20251250750
12Option2CatB10/27/203020001000
13Option2CatA10/27/20352000750
14Option2CatB10/27/20352500500
15Option2CatA10/27/205030001000
16Option2CatB10/27/20502100-400

 

The last row illustrates the value(s) I'd like to be able to calculate and chart accordingly or do further analysis. One example I found online suggested using a sequential index column added in Power Query, but I have the Date column, so I'd rather not add the additional index column if I don't need to. While in my sample here the ID column is sequential with date order, that is not garunteed in the production data set.

3 REPLIES 3
AlB
Community Champion
Community Champion

Hi @Anonymous 

Date would be sufficient if there weren't repeated dates per Category/Option.  You need an index. Make sure it establishes the order correctly. The one in your table seems wrong. Try this:

Delta column =
VAR previousIndex_ =
    CALCULATE (
        MAX ( Table1[Date] ),
        Table1[Index] < EARLIER ( Table1[Index] ),
        ALLEXCEPT ( Table1, Table1[Category], Table1[Option] )
    )
VAR previousVal_ =
    CALCULATE (
        DISTINCT ( Table1[Quantity] ),
        Table1[Date] = previousIndex_,
        ALLEXCEPT ( Table1, Table1[Category], Table1[Option] )
    )
VAR currentVal_ = Table1[Quantity]
RETURN
    currentVal_ - previousVal_

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

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

Cheers 

SU18_powerbi_badge

 

Anonymous
Not applicable

Hi @AlB thanks for the response.

 

So you're confirming that there is no good way to filter to only rows pertaining to the context defined by the current Option and Category, and then based on the current date value in the current context, find the next most recent date?

 

You're correct, the ID column is not sequential with the Date values nor would I expect it to be in the data I'll be getting. Adding an index column with the data sorted by date in PowerQuery should not be an issue. I had read that adding index columns can be a bit of a performance hit for large datasets (not that that is an issue for me in this case), but I like to do things the "right" way as much as possible, build good habbits and all that.

 

You're also correct the dates will overlap. The sample data is more granular than what I actually expect. I expect most of my data will be granular to the YYYY (as in either 01/01/YYYY or 12/31/YYYY). There is a possibility that the date date might be granular to the Month, but that is a big maybe at the moment. If that is the case it would all likely look like MM/01/YYYY (since months don't have the same number of days...).

 

On first pass the DAX looks relatively straight-forward, appreciate the assistance.

 

Other comments, thoughts more than welcome.

 

Thank you.

AlB
Community Champion
Community Champion

 


@Anonymous wrote:

 

So you're confirming that there is no good way to filter to only rows pertaining to the context defined by the current Option and Category, and then based on the current date value in the current context, find the next most recent date?

 

@Anonymous 

Not confirming at all. That is pretty much what the code above does. But because you have more than one row per date (in some cases) per Category/Option and you seen to want to have an additional order for those rows with the same date, you need that index. Otherwise those rows will be indistinguishable internally, in terms of order, for the calc we are performing.

 

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

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

Cheers 

 

SU18_powerbi_badge

   

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.