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
mikesdunbar
Frequent Visitor

Running total of two columns by date

I'm trying to calculate a running total between two columns, but to change which column to sum based on today's date. Here's my table.

mikesdunbar_0-1673480059583.png

What I'd like to figure out how to calculate would be that "Running Total" column I have there. It cumulatively sums from the "Approved" column up until 1/7/2023, and then continues that cumulative sum, but switched over to the "Estimated" column after 1/7/2023. In case this gets read at a later date, Today = 1/11/2023.

 

Thank you!

1 ACCEPTED SOLUTION
bolfri
Super User
Super User

You can create a new column with CorrectValue and put the logic you want here:

CorrectValue = IF(fact_table[Approved]=0,fact_table[Estimated],fact_table[Approved])
 
What I see in your sample data: show me estimated value if I dont have any approved one.
 
Then a measure for RT is like that:

RT =
var last_date = LASTDATE(fact_table[Date])
return CALCULATE(SUM(fact_table[CorrectValue]),FILTER(ALLSELECTED(fact_table),fact_table[Date]<=last_date))

 

bolfri_0-1673482500422.png

I hope this will help you 🙂





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

5 REPLIES 5
bolfri
Super User
Super User

You can create a new column with CorrectValue and put the logic you want here:

CorrectValue = IF(fact_table[Approved]=0,fact_table[Estimated],fact_table[Approved])
 
What I see in your sample data: show me estimated value if I dont have any approved one.
 
Then a measure for RT is like that:

RT =
var last_date = LASTDATE(fact_table[Date])
return CALCULATE(SUM(fact_table[CorrectValue]),FILTER(ALLSELECTED(fact_table),fact_table[Date]<=last_date))

 

bolfri_0-1673482500422.png

I hope this will help you 🙂





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




That looks like it does it. I was wondering about calculated columns. When I first started learning Power BI, I remember reading somethign that said you should go with a Measure before creating a caluclated column since calculated columns take up more space, or something like that. Is that accurate?

Hmmm not really. I mean, yes and no. It alaways depends on what do you want to achive. In many scenario put whole logic into single column and using it in simple measure has better performance than writing complex measure that takes a lot of time to calculte (or even worse - when you want to filter something and need to wait few seconds to see the results). In this case calculated column is better.





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




That makes sense, thanks for your help!

sevenhills
Super User
Super User

 

Try this measure:

Running Total = 
CALCULATE(
     sum( TableApprovedEstimated[Approved]), 
     filter ( allselected(TableApprovedEstimated), 
            TableApprovedEstimated[Job Code] = Max(TableApprovedEstimated[Job Code]) 
            && TableApprovedEstimated[Date] <= Max(TableApprovedEstimated[Date]) 
    )
)
+ 
CALCULATE(
     sum( TableApprovedEstimated[Estimated]), 
     filter ( allselected(TableApprovedEstimated), 
            TableApprovedEstimated[Job Code] = Max(TableApprovedEstimated[Job Code]) 
            && TableApprovedEstimated[Date] <= Max(TableApprovedEstimated[Date])
             && TableApprovedEstimated[Approved] = 0
    )
)

 

 

Tip: If this is a 1 Million rows, it is better to add as a column "Running Total" either in power query or DAX. This kind of semi-additive calculations are always performance hit for huge set of rows. 

 

 

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.