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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
Pasq_italy
Regular Visitor

Progressive Difference Calculation

Hello everyone, I would appreciate your assistance in understanding how to set up a measure that allows calculating the revenue difference per customer and order date for each recorded date in Power BI.

 

To explain further, if we divide customers by invoice date and track their status at precise times, how can we calculate the difference between one observation and the next?

 

Thank you all for your support! 😊

 

Pasq_italy_0-1711731701287.png

 

1 ACCEPTED SOLUTION
Zang_Mi
Resolver II
Resolver II

Hello, I've got the expected result in a matrix using one measure. In that matrix, customer, date, and hour are used as row hierarchy, and in the column of total income we can see the income value at each hour.

Zang_Mi_0-1711793646458.png

M query used to build this sample data:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wci4tLsnPTS1SMFTSUTKy1Dcw1jcyMDIBcgzMrAwMgLShAZCK1cGn1NAEotTSlKBKC6ihRtiUGhjqG5ig22+CzXoUlTDrTQmrhFpvDlIZCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Customer = _t, #"Delivery Date" = _t, Rel.Date = _t, #"Total Income" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer", type text}, {"Delivery Date", type text}, {"Rel.Date", type time}, {"Total Income", Int64.Type}})
in
    #"Changed Type"


Measure:

Difference = 
VAR __CURRENT_ROW_DELIVERY_HOUR = SELECTEDVALUE('Table'[Rel.Date])
VAR __PREVIOUS_HOUR = CALCULATE(MAX('Table'[Rel.Date]), ALLEXCEPT('Table', 'Table'[Customer], 'Table'[Delivery Date]), 'Table'[Rel.Date] < __CURRENT_ROW_DELIVERY_HOUR)
VAR __CURRENT_ROW_INCOME = SUM('Table'[Total Income])
VAR __PREVIOUS_ROW_INCOME = CALCULATE(SUM('Table'[Total Income]), 'Table'[Rel.Date] = __PREVIOUS_HOUR)
VAR DIFFERENCE = IF(ISBLANK(__PREVIOUS_ROW_INCOME), BLANK(), __CURRENT_ROW_INCOME - __PREVIOUS_ROW_INCOME)
RETURN DIFFERENCE

In summary:

  1. Obtain the hour of the current row (for current customer and delivery date as filtered by the row)
  2. Obtain the hour of the previous row, which should be the maximum hour value that is smaller than the current hour, for the same customer and delivery date.
  3. Calculate the income for the current row
  4. Calculate the income for the previous row (filtered in the current row by the same customer and delivery date, and we also add an additional filter to take exactly the income value of the previous hour)
  5. Calculate the difference (current row income - previous row income) under the condition that, if there is not a previous hour, just return a blank value and no calculation has to be done.

 

If this answer helps you, please give a kudo and mark it as solution 🙂.

View solution in original post

1 REPLY 1
Zang_Mi
Resolver II
Resolver II

Hello, I've got the expected result in a matrix using one measure. In that matrix, customer, date, and hour are used as row hierarchy, and in the column of total income we can see the income value at each hour.

Zang_Mi_0-1711793646458.png

M query used to build this sample data:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wci4tLsnPTS1SMFTSUTKy1Dcw1jcyMDIBcgzMrAwMgLShAZCK1cGn1NAEotTSlKBKC6ihRtiUGhjqG5ig22+CzXoUlTDrTQmrhFpvDlIZCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Customer = _t, #"Delivery Date" = _t, Rel.Date = _t, #"Total Income" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer", type text}, {"Delivery Date", type text}, {"Rel.Date", type time}, {"Total Income", Int64.Type}})
in
    #"Changed Type"


Measure:

Difference = 
VAR __CURRENT_ROW_DELIVERY_HOUR = SELECTEDVALUE('Table'[Rel.Date])
VAR __PREVIOUS_HOUR = CALCULATE(MAX('Table'[Rel.Date]), ALLEXCEPT('Table', 'Table'[Customer], 'Table'[Delivery Date]), 'Table'[Rel.Date] < __CURRENT_ROW_DELIVERY_HOUR)
VAR __CURRENT_ROW_INCOME = SUM('Table'[Total Income])
VAR __PREVIOUS_ROW_INCOME = CALCULATE(SUM('Table'[Total Income]), 'Table'[Rel.Date] = __PREVIOUS_HOUR)
VAR DIFFERENCE = IF(ISBLANK(__PREVIOUS_ROW_INCOME), BLANK(), __CURRENT_ROW_INCOME - __PREVIOUS_ROW_INCOME)
RETURN DIFFERENCE

In summary:

  1. Obtain the hour of the current row (for current customer and delivery date as filtered by the row)
  2. Obtain the hour of the previous row, which should be the maximum hour value that is smaller than the current hour, for the same customer and delivery date.
  3. Calculate the income for the current row
  4. Calculate the income for the previous row (filtered in the current row by the same customer and delivery date, and we also add an additional filter to take exactly the income value of the previous hour)
  5. Calculate the difference (current row income - previous row income) under the condition that, if there is not a previous hour, just return a blank value and no calculation has to be done.

 

If this answer helps you, please give a kudo and mark it as solution 🙂.

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

July Power BI Update Carousel

Power BI Monthly Update - July 2026

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

60 days of Data Days Carousel

Data Days 2026

Join Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Top Solution Authors