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
LasseLJørgensen
Regular Visitor

Running total using datediff in a measure

I need a dynamic measure that can make a running total  summing the amount of days between two dates per employee. I want the measure to be able to run in 12 months 'windows'. Please regard I am not able to use window functions since i use analysis services which doesn't include window functions.

This is the data:

LasseLJrgensen_0-1704489994586.png

 

Among other attempts I have tried:

Running total =
CALCULATE(
    sumx(
       SUMMARIZE('Data', "result",
      DATEDIFF(MAX('Data'[Date incurred]), MIN('Data'[Date paid]), DAY ))
    , [result])
    , DATESINPERIOD('Data'[Date paid], MAX('Data'[Date paid]), -1, YEAR))

Anybody know of a solution where one can make running totals like that without using window functions?

 

1 ACCEPTED SOLUTION
LasseLJørgensen
Regular Visitor

@Anonymous 

Hi, thx for the advice, but I couldn't get your idea to work. 

I've worked on it some more and got it to work with this:

Measure =

VAR max_dato = MAX('Data'[Date paid])
VAR min_dato = FIRSTDATE(SAMEPERIODLASTYEAR('Data'[Date paid]))
RETURN
CALCULATE(SUM('Data'[DIFF]),

FILTER(ALL('Data'),
        'Data'[Date paid] <= max_dato
        && 'Data'[Date incurred] >= min_dato)
)



View solution in original post

4 REPLIES 4
LasseLJørgensen
Regular Visitor

@Anonymous 

Hi, thx for the advice, but I couldn't get your idea to work. 

I've worked on it some more and got it to work with this:

Measure =

VAR max_dato = MAX('Data'[Date paid])
VAR min_dato = FIRSTDATE(SAMEPERIODLASTYEAR('Data'[Date paid]))
RETURN
CALCULATE(SUM('Data'[DIFF]),

FILTER(ALL('Data'),
        'Data'[Date paid] <= max_dato
        && 'Data'[Date incurred] >= min_dato)
)



Anonymous
Not applicable

Hi @LasseLJørgensen ,

 

According to your description, here are my steps you can follow as a solution.

(1) My test data is the same as yours.

(2) We can create measures. 

Measure = DATEDIFF(MAX('Table'[Date incurred]),MAX('Table'[Date paid]),DAY)
Measure 2 = 
var _a=SUMX(
    SUMMARIZE(
      FILTER(ALL( 'Table'),'Table'[Date paid]<=MAX('Table'[Date paid])),
        'Table'[Employee number],
        'Table'[Date incurred],'Table'[Date paid],
        "total",[Measure]
    ),
    [total]
)
var _b=IF([Measure]=0,0,_a)
return CALCULATE(_b,DATESINPERIOD('Table'[Date paid], MAX('Table'[Date paid]), -1, YEAR))

(3) Then the result is as follows.

vtangjiemsft_0-1704683106933.png

 

If the above one can't help you get the desired result, please provide some sample data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples. It is better if you can share a simplified pbix file. Thank you.

 

Best Regards,

Neeko Tang

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

LasseLJørgensen
Regular Visitor

Hi @Dangar332 

That gives the same result. What I'm expectning is:
26-01-2014 = 5
14-02-2014 = 15
16-04-2014 = 33

and so on. 

Dangar332
Super User
Super User

hi,  @LasseLJørgensen 

try below

Running total =
var a = edate(min('Data'[Date paid]),-12)
var b = min('Data'[employee number])
return
var c  =sumx(
          filter( 
             'Data', 
             'Data'[employee number] = b &&
             'Data'[Date paid]<= min('Data'[Date paid]) && 'Data'[Date paid]>=a
          ), 
          DATEDIFF(MIN('Data'[Date incurred]), MIN('Data'[Date paid]), DAY )
        )

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.