Forum Discussion

yuvan29's avatar
yuvan29
Regular Visitor
1 year ago
Solved

Previous 3 day average line graph

Hello, I need some help in creating a measure that caluclates the average value at a certain time for the past 3 days. In my current line graph, I've got different times of the day on the x-axis and...
  • Anonymous's avatar
    Anonymous
    1 year ago

    Hi yuvan29 

     

    Measure can only return one value at a date point, so I create 4 measures.

    Here's the sample:

    Table:

    Last day = 
    	VAR _currentTime = MAX('Table 2'[Time])
    	VAR _today = DATE(YEAR(MAX('Table'[Date\Time])), MONTH(MAX('Table'[Date\Time])), DAY(MAX('Table'[Date\Time])))
    	VAR _previousday = CALCULATE(
    		MAX('Table'[Date\Time].[Date]),
    		FILTER(
    			ALL('Table'),
    			DATE(YEAR('Table'[Date\Time]), MONTH('Table'[Date\Time]), DAY('Table'[Date\Time])) < _today
    		)
    	)
    	RETURN
    		CALCULATE(
    			SUM('Table'[Values]),
    			FILTER(
    				ALL('Table'),
    				TIME(
    					HOUR('Table'[Date\Time]),
    					MINUTE('Table'[Date\Time]),
    					SECOND('Table'[Date\Time])
    				) = _currentTime && DATE(YEAR('Table'[Date\Time]), MONTH('Table'[Date\Time]), DAY('Table'[Date\Time])) = _previousday
    			)
    		)
    Last 2 day = 
    	VAR _currentTime = MAX('Table 2'[Time])
    	VAR _today = DATE(YEAR(MAX('Table'[Date\Time])), MONTH(MAX('Table'[Date\Time])), DAY(MAX('Table'[Date\Time])))
    	VAR _previousday = CALCULATE(
    		MAX('Table'[Date\Time].[Date]),
    		FILTER(
    			ALL('Table'),
    			DATE(YEAR('Table'[Date\Time]), MONTH('Table'[Date\Time]), DAY('Table'[Date\Time])) < _today
    		)
    	)
    	VAR _last2day = CALCULATE(
    		MAX('Table'[Date\Time].[Date]),
    		FILTER(
    			ALL('Table'),
    			DATE(YEAR('Table'[Date\Time]), MONTH('Table'[Date\Time]), DAY('Table'[Date\Time])) < _previousday
    		)
    	)
    	RETURN
    		CALCULATE(
    			SUM('Table'[Values]),
    			FILTER(
    				ALL('Table'),
    				TIME(
    					HOUR('Table'[Date\Time]),
    					MINUTE('Table'[Date\Time]),
    					SECOND('Table'[Date\Time])
    				) = _currentTime && DATE(YEAR('Table'[Date\Time]), MONTH('Table'[Date\Time]), DAY('Table'[Date\Time])) = _last2day
    			)
    		)
    Last 3 day = 
    	VAR _currentTime = MAX('Table 2'[Time])
    	VAR _today = DATE(YEAR(MAX('Table'[Date\Time])), MONTH(MAX('Table'[Date\Time])), DAY(MAX('Table'[Date\Time])))
    	VAR _previousday = CALCULATE(
    		MAX('Table'[Date\Time].[Date]),
    		FILTER(
    			ALL('Table'),
    			DATE(YEAR('Table'[Date\Time]), MONTH('Table'[Date\Time]), DAY('Table'[Date\Time])) < _today
    		)
    	)
    	VAR _last2day = CALCULATE(
    		MAX('Table'[Date\Time].[Date]),
    		FILTER(
    			ALL('Table'),
    			DATE(YEAR('Table'[Date\Time]), MONTH('Table'[Date\Time]), DAY('Table'[Date\Time])) < _previousday
    		)
    	)
        VAR _last3day = CALCULATE(
    		MAX('Table'[Date\Time].[Date]),
    		FILTER(
    			ALL('Table'),
    			DATE(YEAR('Table'[Date\Time]), MONTH('Table'[Date\Time]), DAY('Table'[Date\Time])) < _last2day
    		)
    	)
    	RETURN
    		CALCULATE(
    			SUM('Table'[Values]),
    			FILTER(
    				ALL('Table'),
    				TIME(
    					HOUR('Table'[Date\Time]),
    					MINUTE('Table'[Date\Time]),
    					SECOND('Table'[Date\Time])
    				) = _currentTime && DATE(YEAR('Table'[Date\Time]), MONTH('Table'[Date\Time]), DAY('Table'[Date\Time])) = _last3day
    			)
    		)
    AVG = 
    	VAR _Today = IF(
    		'Table'[Today] <> BLANK(),
    		1,
    		0
    	)
    	VAR _Previousday = IF(
    		'Table'[Last day] <> BLANK(),
    		1,
    		0
    	)
    	VAR _Last2day = IF(
    		'Table'[Last 2 day] <> BLANK(),
    		1,
    		0
    	)
    	RETURN
    		([Last 2 day] + [Last day] + [Today]) / (_Last2day + _Previousday + _Today)

    The result is as follow:

     

     

    Best Regards

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

  • FreemanZ's avatar
    1 year ago

    Hi yuvan29 ,

     

    Supposing you have a data table like this:

     

    you can try to plot a visual with time column and a measure like this:

    Rolling 3d Avg = 
    VAR _date = SELECTEDVALUE(data[date])
    VAR _last3d = 
    TOPN(
        3,
        FILTER(ALL(data[Date]), data[date]<_date),
        data[date]
    )
    VAR _3dtotal =
    CALCULATE(
        SUM(data[value]),
        _last3d
    )
    VAR _result = DIVIDE(_3dtotal, COUNTROWS(_last3d))
    RETURN _result

     

    it worked like this:

     

    Please find more info in the attached file.

     

    p.s.:

    1. If more info could be provided, the code could be further simplified, like in the attachment. 

    2. With dedicated calendar tables, you will also find Time Intelligence functions like DATESINPERIOD very handy for such cases.