Forum Discussion
DAX Running total by another column
- 10 years ago
Hello nhanser,
for me it looks like you want to add a calculated column, but the DAX code you use is for beeing used in a pivot table.
I copied your raw data, but used different dates.
The pivot I created looks like this and I hope this is what you want:
The code I used is close to the one you used, but slightly different:
RunningTotal :=
CALCULATE (
SUM ( Tabelle1[QTY] );
FILTER ( ALL ( Tabelle1 ); Tabelle1[Date] <= MAX ( Tabelle1[Date] ) );
VALUES ( Tabelle1[Item] )
)The key here is the VALUES() which puts the Items into the filter context of the CALCULATE()-statement. Otherwise you would have running totals on the dates, but it would be the same for all your Items.
Hope that helps a bit.
Greets,
Lars
Hello nhanser,
for me it looks like you want to add a calculated column, but the DAX code you use is for beeing used in a pivot table.
I copied your raw data, but used different dates.
The pivot I created looks like this and I hope this is what you want:
The code I used is close to the one you used, but slightly different:
RunningTotal :=
CALCULATE (
SUM ( Tabelle1[QTY] );
FILTER ( ALL ( Tabelle1 ); Tabelle1[Date] <= MAX ( Tabelle1[Date] ) );
VALUES ( Tabelle1[Item] )
)
The key here is the VALUES() which puts the Items into the filter context of the CALCULATE()-statement. Otherwise you would have running totals on the dates, but it would be the same for all your Items.
Hope that helps a bit.
Greets,
Lars
- robbie33710 years agoAdvocate I
This is a great solution, thank you. Worth noting you can stack multiple variables in the value cluase to allow aggregation across multiple / different dimensions, (similar to partition by in sql I guess).
However, I have run into a slight issue. If you have a value or combination of values that are empty i.e. there is no row in your dataset for that value or combination of values, the running total becomes incorrect until the next time a row matching those criteria exists, at which point it 'catches up'.
For example, if you are creating a running total of sales by month and team across a year, using a data set like:
Month Team Sales
Jan A 100
Jan B 200
Feb A 150
Feb B 210
Mar A 300
Apr A 120
Apr B 400
In March, if Team B has a month where they have no sales (and therefore no row in the dataset), your running total for the business as a whole will ignore all Team B's previous data in the calculation of a running total, returning 550 as the total, rather than 690.
I'm yet to find a fix for this (despite habving tried every combination of isempty i can think of). If anyone has any ideas I'd love to hear them
RobbieThe following month if Team B has
- Mardin9 years agoAdvocate IV
Hi robbie337 did you find a solution to your problem?
I have a similar problem, since I want to display my Running Total Measure in a Line Chart and I need to to filter on different categories in a slicer.
If I include my VALUES[Category column] in the Running Total Measure all is fine as long as I ALWAYS have one of the Categories selected in my slicer. If I deselect all in the Category Slicer the Line Chart will jump up and down and not continue to increase as intended.
To fix this I can just leave out the VALUES([Category column] of my Running Total Measure, but then my Running Total would just show an aggregation across all categories and not calculate PER category as intended.
...This is such an easy task in Excel Pivot Tables where Running Total Calculation is a standard option - why is this still missing in PowerBI.
- jmalone10 years agoResolver III
LarsSchreiber - I was looking for a similar solution as nhanser. Your solution worked perfectly!
The key, as you mentioned, is the VALUES() function to put the running total calculation in context of the field needing to be grouped.
Thank you for the very useful response!
- LarsSchreiber10 years agoResponsive Resident
- PeterBI10 years agoHelper I
- FawadRehman10 years agoHelper I
LarsSchreiber I need your help. I am looking for same solution but somehow its not working for me.
RunningTotal =
CALCULATE (
SUM ( TabelForecast[Forcast] );
FILTER ( ALL ( TabelForecast ); TabelForecast[MonthYear] <= MAX ( TabelForecast[MonthYear] ) );
VALUES ( TabelForecast[M#] )
)I have tried your formula in power BI but its not giving me running total but total for all the rows :( Any suggestions?
- LarsSchreiber10 years agoResponsive Resident
Hi FawadRehman,
sorry, I haven't been here for a while. Could you solve your problem already?!
regards,
Lars
- FawadRehman10 years agoHelper I
- Anonymous10 years agoNot applicable
Hi LarsSchreiber,
your solution didn't workout in my scenario..!!
DimItem(ItemKey,ItemNumber,CompanyKey) (One ItemNumber can have Multiple ItemKey because of CompanyKey)
FactRevenue(ItemKey,CompanyKey,CustomerKey,BrandKey,AccountingDate,Revenue) (Fact is day level)
Fact and Dim Item Joined on ItemKey.
when i try to put ItemNumber from DimItem and Revenue from FactRevenue in a table visual with following Measures.
TotalRevenue = CALCULATE(SUM(FactRevenue[Revenue]),ALL(DimItem[ItemNumber]))
RContr = DIVIDE(SUM(FactRevenue[Revenue]),[TotalRevenue],0)
RT = CALCULATE([RContr],FILTER(ALL(FactRevenue),FactRevenue[AccountingDate] <= MAX(FactRevenue[AccountingDate])),VALUES(DimItem[ItemNumber]))
I am not getting good results. Please consider the output:
5.99+5.04 = 11.03 or something.
- zaidmasad8 years agoHelper III
LarsSchreiber Is it possible to create the 70th percentile on the results per each Item?
Thanks,
Zaid