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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
AgataJ
Helper II
Helper II

Subtracting Running Total from first available value in a table

Good morning,

I am working on such calculations:

I have a column with Hours and Hours Used. For Hours used I need to calculate Running Total, what I have a DAX for it no problem.

My problem starts with calculating a Balance.

I need to subtract Hours Used Running Total always from the first available date for Hours (01/01/2025 – 100 in my example table)

In Excel it would be like Balance = $B$3 – E3, next row $B$3 – E4 etc., but I cannot find a solution how to do this in DAX.

Would appreciate help with this.

Kind regards,

Agata

 

DateHoursHours usedHours Used Running TotalBalance
01/01/2025100101010
02/01/2025200607030
03/01/2025300108020
04/01/2025100201000
05/01/202550020120-20
06/01/202530030150-50
07/01/202540040190-90
08/01/202540030210-110
09/01/202520050260-160
10/01/202510010270-170
1 ACCEPTED SOLUTION
speedramps
Super User
Super User

Try this

 

Answer = 

// get first date
var myfirstdate = 
CALCULATE(
    MIN(yourdata[Date]),
    ALl(yourdata)
)

// get hours for first date
var myfirstvalue =
CALCULATE(
SUM(yourdata[Hours]),
ALL(yourdata),
yourdata[Date] <= myfirstdate)

// get date for current row in visual
var mydate = SELECTEDVALUE(yourdata[Date])

// get running total for row
var runningtotal =
CALCULATE(
SUM(yourdata[Hours used]),
ALL(yourdata),
yourdata[Date] <= mydate)

RETURN
// deduct running toral from first value
myfirstvalue - runningtotal

 

speedramps_1-1736856237801.png

 

Please click the [accept solution] and thumbs up buttons. Thank you

 

 

 

View solution in original post

5 REPLIES 5
speedramps
Super User
Super User

Thanks @AgataJ 

 

Your new issue is a bit vague and a new problem that will require a new solution.

 

Please raise a new ticket rather than append to this one, otherwise uit gets confussing which is which.

 

If you quote @speedramops then I wil receive an automatic notification.

 

In you new ticket please provide:-

  • input data (as a table not picture). 
  • desired output
  • step by step description
  • use meaningful column names
  • keep it simple and omit anything not related to the problem

 

Take care to use the same column names in the input, output and description.

 

Use Power Bi terminnology.  If you mean Slicer then say slicer and not Filter.

 

You will get quicker and better replies if you take the time and effort to ask clear and comprehensive questions with example data.

 

Thank you

speedramps
Super User
Super User

@AgataJ  please review my solution and either click [accept solution] and the thumbs up button or provide more details, thank you 😀

v-jtian-msft
Community Support
Community Support

Hi,speedramps ,thanks for your concern about this issue.

Your answer is excellent!
And I would like to share some additional solutions below.
Hello,@AgataJ .I am glad to help you.

After my test, the test data provided by speedramps  is correct. In addition, I noticed that there is an error in the test data provided by you, which may cause misunderstanding

vjtianmsft_0-1737103912393.png

vjtianmsft_1-1737103921191.png

I hope my suggestions give you good ideas, if you have any more questions, please clarify in a follow-up reply.
Best Regards,
Carson Jian

speedramps
Super User
Super User

Try this

 

Answer = 

// get first date
var myfirstdate = 
CALCULATE(
    MIN(yourdata[Date]),
    ALl(yourdata)
)

// get hours for first date
var myfirstvalue =
CALCULATE(
SUM(yourdata[Hours]),
ALL(yourdata),
yourdata[Date] <= myfirstdate)

// get date for current row in visual
var mydate = SELECTEDVALUE(yourdata[Date])

// get running total for row
var runningtotal =
CALCULATE(
SUM(yourdata[Hours used]),
ALL(yourdata),
yourdata[Date] <= mydate)

RETURN
// deduct running toral from first value
myfirstvalue - runningtotal

 

speedramps_1-1736856237801.png

 

Please click the [accept solution] and thumbs up buttons. Thank you

 

 

 

Hi @speedramps ,

My bad, I focused on the project and forgot to asnwer. Your code works well, thanks a million!

I had to filter it to start the calculation from the first date when Cum_Complete is not blank. I did it using this code: 

Answer2 filtered = CALCULATE([Answer 2], 'MRP History'[Complete]<>0). It works properly however still shows the first line when cum_complete is empty, would you know maybe how to show the first line with cum_complete not blank (in the example case it'd be 02-Jan_2025)? I cannot filter it by cum_complete >0 as I would like to see other empty lines , like here:  AgataJ_0-1737366130341.png

Thanks in advance,

Agata

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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