Forum Discussion
Plot zero when there is no data in line chart
- 8 years ago
And same for the MovingAverage
MovingAverage = CALCULATE ( SUM ( 'Shiptracking history'[Production] ), DATESINPERIOD ( 'Shiptracking history'[Date], LASTDATE ( 'Shiptracking history'[Date] ), -10, DAY ) ) / 10 + 0Hope this helps! :smileyhappy:
Works with the sample data!
- 8 years ago
Okay hopefully this will solve it!
First things first go back to the Query Editor
1) Select the Date Column - Add Column tab - Time dropdown/button - select Time Only
2) with Date Column still selected - Date dropdown/button - select Date Only
3) Rename the original Date Column - Time and Date (or as you wish) and Date.1 rename just Date
4) Home tab - Close and Apply
Then
5) Create a Calendar Table - Modeling tab - click New Table
Calendar Table = CALENDAR ( MIN('Table'[Date]), MAX('Table'[Date]) )6) Set up the Relationship between the 2 tables based on the 2 date columns
7) And this is your New Moving Average Measure
MovingAverage Measure Calendar =
CALCULATE (
SUM ( 'Table'[Production] ),
DATESINPERIOD (
'Calendar Table'[Date],
LASTDATE ( 'Calendar Table'[Date] ),
-10,
DAY
)
)
/ 10
+ 08) You can add a "Between" Date Slicer if you wish just make sure you use the Date from the Calendar
9) Create a Line Chart - add the Date from the Calendar to the axis and deselect the Date Hierarchy if it defaults to it
10) Add the New Moving Average to the Values
Tell me this works! :smileyhappy:
- 7 years ago
Wrap the calculation in CALCULATE to have Row Context
Production Measure New = SUMX ( FILTER ( 'Calendar Table', 'Calendar Table'[Date] <= TODAY () ), CALCULATE ( SUM ( 'Table'[Production] ) + 0 ) )
Anonymous
That should me a MEASURE not a COLUMN
Production Measure = SUM ( 'Table'[Production] ) + 0
Then place the Measure in the Values area of your Line Chart.
That should do it! :smileyhappy:
And same for the MovingAverage
MovingAverage =
CALCULATE (
SUM ( 'Shiptracking history'[Production] ),
DATESINPERIOD (
'Shiptracking history'[Date],
LASTDATE ( 'Shiptracking history'[Date] ),
-10,
DAY
)
)
/ 10
+ 0Hope this helps! :smileyhappy:
Works with the sample data!
- Anonymous8 years agoNot applicable
Sean Thank you. The Moving Average does not work for me... I created a measure copy-pasting your code. Power BI is still interpolating when data has gaps. In March there was no production so the 10-day moving average should be zero. Does it work for you? What am I doing wrong here?
There is no production in March so moving average should be zero. Power BI is still interpolating
- Sean8 years agoCommunity Champion
Yes works with the sample data you provided!
EDIT: I switched to Categorical Axis only for the picture above - to show March 2018 specifically! :smileyhappy:
- Anonymous8 years agoNot applicable
Sean thanks so much for your help and patience. I am doing something wrong. Maybe the problem is with my axis. Check the picture below, it does not work for me. The title says "by Date" whereas yours says "by Year and Month". I wonder if that may be the reason why it does not work for me? It is odd that it works for you and not for me, surely I am making a mistake somewhere.
Categorical axis only to show you the detail in March 2018, which does not appear. I have enabled "show items with no data"