Forum Discussion
DAX running total optimization : Using date column from dimension vs date from fact
Problem : I have a fact table called Productivity. It tracks actual units of work performed and actual hours it took to perform the work. It also includes forecast hours and forecast units. It is connected to three dimensions as shown below. Note that those dimensions are shared between multiple fact tables which are not relevant to this question. The productivity fact table tracks data on daily baisis i.e. data is tracked for each date. I am unable to roll it up because of the business need. I need to calculate divide running total for units by running total for hours.
Total number of rows in fact table : 32931185
I have slicer on each dimension. I have added filters to slicer so that if one slicer is selected, it narrows down values in other slicers.
Here is the measure I am trying to compute:
To Date U/M =
VAR MaxDate = MAX('Date'[Date]) //Maximum visibile date
VAR ActualUnitsToDate = CALCULATE(
SUM(Productivity[Units]) // Sum of units
,ALL('Date'[Date]) // Ignore filters over date
,'Date'[Date]<=MaxDate // All filter on the date
)
VAR ActualHoursToDate = CALCULATE(SUM(Productivity[ActualHours]) //Sum of hours
,ALL('Date'[Date]) // Ignore filters over date
,'Date'[Date]<=MaxDate) // All filter on the date
RETURN DIVIDE(ActualUnitsToDate,ActualHoursToDate,0)
This measure runs forever. So to scale down the problem, I tried calculating only ActualUnitsToDate.
When I use date column from dimension table, ActualUnitsToDate takes forever. When I use date column from productivity fact table, it takes about 25000 ms. Still not really acceptable but atleast it returns the result.
Questions :
- What does date column from fact perform better than the one from date dimension?
- What couldd cause the simple running total calculation so slow. I understand that this is intensive considering the size of the dataset ( 32931185 rows). What can I do to improve the performance?
EDIT : It seems that performance is also impaced by which column I use in visual. Date from fact vs date from dimension.
8 Replies
- Greg_DecklerCommunity Champion
tvaishnav You could try this:
To Date U/M = VAR MaxDate = MAX('Date'[Date]) //Maximum visibile date VAR __Table = FILTER(ALL('Productivity'), [Date] < MaxDate) VAR ActualUnitsToDate = SUMX(__Table, [Units]) // Sum of units VAR ActualHoursToDate = SUMX(__Table, [ActualHours]) //Sum of hours RETURN DIVIDE(ActualUnitsToDate,ActualHoursToDate,0)- tvaishnavHelper IV
Greg_Deckler Your measure returns same value for each date. I can see why. VAR __Table gives a intermediate table. SUMX performs sum over the entire __Table without breaking it by date. Here is how it looks:
I think I need to use calculate function around SUMX. But I am unable to change context on __Table via calculate. Any thoughts on how I can accomplish this?
I do understand what you are trying to do here. It is certainly faster.However, this measure also performs pooly when my date column in the visual comes from date dimension. It returns results instantly if I use date column from fact table in the visual. Why would that be? It is a simple one to many unidirectional relationship.
- Greg_DecklerCommunity Champion
tvaishnav So, probably something I'm not understanding about your data model or where the values in your visual are coming from. If you get your max date in context and then FILTER ALL of your fact table where the date column in that fact table is less than that date, then I don't know why you would get the same value for all dates unless something else is wonky. If you have a 1:* relationship between your date table and fact table and it is single direction, then if you used the date column from the fact table in that visual, then the MAX of 'date'[date] would return the largest date in the date table and that would explain it. So, if that is the case, use MAX('Productivity'[Date]) instead in the measure.