Computing a running total in DAX

Computing a running total in DAX

SQLBI

4 года назад

66,509 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Ross Williams
Ross Williams - 11.10.2023 23:23

Why did I have to add the .date to make mine work? Without it, it wouldn't give me a cumulative it would just total the month and start over at the next month.

Ответить
mikeycbd
mikeycbd - 03.10.2023 01:57

I'm a newbie, and my running total is the same... I believe you have a video somewhere that explains the Dax Code please?

Ответить
Carl Sagan
Carl Sagan - 16.06.2023 00:06

Get rid of all the filters on the date? Why would there be filters on the date?

Ответить
Guillaume Trepanier
Guillaume Trepanier - 09.12.2022 00:43

THANK YOU : you have no idea how long I was looking for this explanation

Ответить
Igor Antarov
Igor Antarov - 20.10.2022 16:39

Thank you, this video saved my day! I had to modify the code slightly (by adding SUM()) to calculate form a column and not from measure:
VAR MaxDate = MAX ('Date'[Date])
VAR Result =
CALCULATE(
SUM(Projects[Sales]),
'Date'[Date] <= MaxDate,
ALL ('Date')
)
RETURN Result

Ответить
Gayathri Parameswaran
Gayathri Parameswaran - 16.10.2022 14:07

this is not working when calculating the running total of a measure calculated for % of total hours (tot.Hours/Quantity).
Can we calculate running total for measures ?

Ответить
Wzx
Wzx - 15.09.2022 02:05

I wish i understant it. Even after a dax bible i still dont ubderstand the way calculate and all replaeces filter context and the still works with it

Ответить
william Arthur
william Arthur - 31.08.2022 16:08

Ciao,
I've watched this several times and even though it is how I've always approached RTM's, I wondered if you had any view on feeding a filtered table to SUMX to iterate over;
RTM :=
VAR mdate =
MAX ( Tsales[Date] )
RETURN
SUMX ( FILTER ( ALL ( Tsales[Date] ), Tsales[Date] <= mdate ), [Trevenue] )


I actuallly find this eaier to (sort of ) understand.
BTW, love the face that you forget the RETURN, it's something I do so often.

Ответить
Tom Ludlow
Tom Ludlow - 16.07.2022 03:11

Can someone tell me why ALL ('Date') is included in this Measure? When I have that or don't have that in my measure it seems to make no difference. Thanks

Ответить
Ahmed Khalied
Ahmed Khalied - 03.07.2022 01:37

Thanks allot

Ответить
DevEngiLeo
DevEngiLeo - 24.03.2022 18:37

mind blowing explanation - thank you for the education. You really explained so well diff between YTD and running total.

Ответить
rajeshbusiness
rajeshbusiness - 19.03.2022 09:28

Thanks a lot. I spent hours trying to figure out an issue myself. I was pretty close but couldn't find a solution and it was driving me crazy. Watched your video and I was able to solve in less than 5 mins.. Love it.!!!!

Ответить
Nasir Uddin
Nasir Uddin - 24.02.2022 15:10

Hi, i have a data set of 17mio rows where there is not date column. I've written a DAX measure for running total of the values. Same measure works file with small data set But it takes huge time to execute in this 17mio data set.
DAX is
CALCULATE(
SUM(Table1[Val]),
FILTER(ALL(Table1),Table1[Val]<=MAX(Table1[Val])),
VALUES(Table1[SKU Code]))

Anything important that i'm missing?

Ответить
Marko Capan
Marko Capan - 22.02.2022 18:14

Thank you Alberto, for this explanation. Well this one makes sense since you’re using VAR MaxDate which saves outer value.
But in your book which I purchased (Definite Guide to DAX – 2nd edition), you gave a perplexing alternative example – which works (and can be found on internet too for topic of running total), but I simply can’t grasp how it’s possible. Excerpt: FILTER(ALL(‘Date’[Date]),’Date’[Date] <= MAX(‘Date’[Date])). How is it possible that MAX ignores row context of a FILTER and refers to outer value within FILTER?

Ответить
Antip Ghosh
Antip Ghosh - 22.02.2022 13:48

Sir, How to do summation of first 3 numbers in a series in power bi....?
Series: {5, 7, 2,3,9,10,2}
Desired sum: 5+7+2

Ответить
Vasileios Manasas
Vasileios Manasas - 17.11.2021 11:52

What if we only wanted the running total of visible dates? Wouldn't ALLSELECTED('Dates') be a more useful filter?

Ответить
Saburov Nariman
Saburov Nariman - 08.11.2021 13:40

Thank you Very very much. First Varieblated expression excutes ones and it becames as constant. This part is diffcult to undurstanding to me. How it works.
For exmple I have such Table
Transit / Days / Result(needed column)
1Tranzit 3 0
2Tranzit 5 2
3Tranzit 8 6
It meens=First transit takes 3 days from second Transit. Second transit was planed for 5 days, becouse of first transit the second transit in real remain 2 days not 5days (3-5=2) Then this 2days take from 3Transit 2days and there remains 6day not 8.
Planed days not real days It's just planed days . Fact days is Resalt(Column). I need culculate the risk from bilion rows. I did all my best but could not do it

Ответить
S
S - 29.08.2021 12:18

What if I do not remove the filter on the date table using the ALL function?

Ответить
Kusal Amarasena
Kusal Amarasena - 24.06.2021 11:16

Hi SQLBI,
What is the evaluation context of this Filter part
FILTER (
ALL('Date'),
'Date'[Date] <= 2. MAX('Date'[Date])
))

Is MAX('Date'[Date]) not affected by ALL('Date')? (if it is, then it should give max date of whole date table)

And is Date[‘Date’] column not affected by initial filter context(here Rows of the visual)

Ответить
Nitin Verma
Nitin Verma - 03.06.2021 05:06

Nice one..

Ответить
Drive Trainer
Drive Trainer - 11.05.2021 14:36

Thanks for expanding on non-date RT. Much appreciated

Ответить
Alvaro Rodríguez Lasso
Alvaro Rodríguez Lasso - 23.04.2021 05:05

I need a running total by alfabetical sorting, ¿Can you help me?

Ответить
Tony Spano
Tony Spano - 21.04.2021 21:41

What would be the best way to calculate cumulative and 12 month rolling totals by customer? I need to maintain and then aggregate the by-customer amounts for customer segmentation. It seems that this can't be a measure because the customer-level segmentation needs to be maintained and then aggregated. For example, create "small customer" below a certain sales amount for cumulative or rolling 12 month. Then aggregate total of the small customer sales amounts.

Ответить
nithin kumar
nithin kumar - 14.04.2021 13:27

what if i want to filter it by date using slicer

Ответить
Tatiana Melnikova
Tatiana Melnikova - 07.04.2021 14:14

I like the way you explained it! I did the same in my model but I faced with performance issue which showed slow DAX query (7733 ms). Is it a way how to get the same result of running total but with better Dax query performance?

Ответить
Abhinay Rozer
Abhinay Rozer - 09.03.2021 10:15

How can we do this in calculated column...
Please explain

Ответить
MatBR10
MatBR10 - 27.02.2021 08:16

Hey, this video was useful thanks! But id also like to know how to this exact same thing with for example, total sales per product

Ответить
Ste
Ste - 29.01.2021 00:27

First question should be: What is DAX?

Ответить
Akshay Narwadkar
Akshay Narwadkar - 24.01.2021 14:42

Thanks Alberto.
Quick question.
We have learned CALCULATE function overrides all the existing filters and sets its own filter given in its parameter.
then why are we using ALL function here, whose function is the same to return a complete table removing all the filters
If CALCULATE already doesn't respect any filters, why are we still using the ALL function to remove the existing filter in the Date Table?
Thanks in advance

Ответить
Marilena D'Onofrio
Marilena D'Onofrio - 04.01.2021 14:19

Alberto, sei un mito!

Ответить
NoShadowOfADoubt
NoShadowOfADoubt - 12.12.2020 05:09

It is simple but it does not work in Power pivot...the running total resets.

Ответить
Henrik Vestergaard
Henrik Vestergaard - 05.12.2020 15:50

Is ALL really required in these examples?
In my test without using ALL('Date' / ALL(Customer[Customer Class]), the CALCULATE filters overwrites the incoming Month filter from the rows, and the running total gives the same result as when I use ALL.
But mayby best practice?
Regards, love to learn from your videos, blogs etc. :)

Ответить
Aniket Chodankar
Aniket Chodankar - 29.11.2020 14:51

Marrrrioo... Well explained!!!

Ответить
Jules Diaz
Jules Diaz - 25.11.2020 10:03

Thank you so much!

Ответить
vimukthi amarasena
vimukthi amarasena - 17.11.2020 13:05

Hi can you give me a hint to calculate the Running total with duplicate dates. (example: when there are many orders per one day) Ex:
Date Order Order Qty Expected running Total
11/17/2020 A 5 5
11/17/2020 B 10 15
11/17/2020 C 4 19

Ответить
Raj
Raj - 13.11.2020 10:56

Can you explain YTD calculation, especially date filter condition used inside the filter function --> CummulativeSales = CALCULATE([TotalSales], FILTER(ALLSELECTED(DatesTable),DatesTable[Date] <= MAX(DatesTable[Date])))

Can you please explain how DatesTable[Date] <= MAX(DatesTable[Date] takes all the dates before current row context, shouldn't it take max date that is last date in table because we use ALLSELECTED(DatesTable) meaning row context in the table visual is removed and all the dates are taken. I understand what i think is wrong but can you explain what is happening actually.

Ответить
Dave portland
Dave portland - 09.11.2020 16:39

Hi Alberto great video ! I expected the running total to have the filter function like this :
CALCULATE
[Sales Amount ],
FILTER (
ALL(Date),
'Date[Date] <= MaxDate
)
)
Was FILTER removed for aesthetic and is it the same result ? or omitting filter gives better performance ?

Ответить
kostas
kostas - 21.08.2020 13:27

Thank you! Great video!

Ответить
Abubakr Saad Makyhoun
Abubakr Saad Makyhoun - 25.07.2020 20:21

Simple and focused, thank you

Ответить
Rafael Vargas
Rafael Vargas - 17.07.2020 23:27

I come from the world of SAP BI and now that I'm learning DAX, I miss some features that seemed simple in WebIntelligence and aren't so much in PowerBI. For example, it would be simpler to create a single measure called "Sales RT" and depending on the context in which it is used, it is calculated automatically, without having to create a variable for date, another by category, another by country, etc. and thus prevent the model from filling up with so many DAX variables. It would be great to have a parameter in the CALCULATE(RunningSum([Sales Amount]),CurrentConext) and this internally to do the rest. If a special calculation is desired, then a variable is created with the context to evaluate.

Ответить
Imran Hussain
Imran Hussain - 16.07.2020 12:07

Hi Alberto/ Russo , thanks for uploading valuable video. Please let me know how to get running total for financial year. ( Jul to June)

Thanks
Imran

Ответить
Vuong Luu
Vuong Luu - 15.07.2020 18:59

How to show as % of running total ?

Ответить
Zen Ore
Zen Ore - 15.07.2020 07:16

Useful, and explained so coherently. Thank you!

Ответить
Alfreds Futterkiste
Alfreds Futterkiste - 14.07.2020 21:49

Your videos are great! Currently going through the mastering DAX course.

Ответить
Ali Ramadan
Ali Ramadan - 14.07.2020 19:43

Simple and to the point! Thank you.

Ответить
Analista X
Analista X - 14.07.2020 15:32

Great !! This type of calculation is also very common in financial investments, in which it is necessary to be aware of the amount applied and returned until that last date.

Ответить
Joe Pawlowski
Joe Pawlowski - 14.07.2020 15:09

In the calculate statement, I noticed that sales amount was not aggregated using for example, a sum function. That's not necessary?

Ответить
HM Murdock
HM Murdock - 14.07.2020 14:13

nice video. i learn allot from you.

Ответить
Carlos Castro
Carlos Castro - 14.07.2020 13:34

Thanks Alberto.
Quick question.
I noticed you created a VAR for Result rather than put after RETURN the all function. Why did you do that?
Correct way to write Dax expression using variables? Better performance? VAR trigger the CALCULATE function save the result and then measure just display the outcome rather than trigger the CALCULATE function?
But then, on the second measure CALCULATE function is after result. So, not sure which one is the best way to do it?
Thanks

Ответить