Forum Discussion

Enrique_Pi1000's avatar
Enrique_Pi1000
Regular Visitor
2 years ago

Promedio movil

Necesito resolver un tema

Evaluando precios de cripto etherum he constuido una medida que mide el rendimientoen el tiempo. la he construido ocn la posibilidad de aplicar un slicer y asi ver por ej cada año. La medida es asi:

RendimientoEtherum=

VAR Fechaini =

IF (

[Cot_Ethereum] > 0;

CALCULATE ( FIRSTDATE ( CRIPTOCOTIZ[FECHA] ); ALLSELECTED ( 'Calendario'[Date] ) );

BLANK ()

)

VAR Cotini =

CALCULATE (

[Cot_Ethereum];

Calendario[Date]= Fechaini;

ALLSELECTED ( 'Calendario'[Date])

)

RETURN

IF ( [Cot_Ethereum] > 0; DIVIDE ( [Cot_Ethereum]; Cotini; 0 ) - 1; BLANK () )

 

esto resulta ok. pero al hacer una medida que calcule un promedio movil como la siguiente:

%Promedio Movil Etherum 30 d=

IF([Cot_Ethereum]>0;

CALCULATE(

AVERAGEX ( VALUES(Calendario[Date] ); [RendimientoEtherum] );ALLSELECTED(Calendario[Date]);

DATESINPERIOD(Calendario[Date];MAX(Calendario[Date]);-30;DAYS));

BLANK())

esta medida responde si no hay slicers de año por ej. Pero si en una serie elijo un año esta medida empieza a calcular el primer dia del año con los ultimos 30 dias del anterior en lugar de 0 ya que el rendimiento del proer dato debiera ser cero y esa grafica no sale como la esperada.

Me pueden ayudar?

empieza en menos 50% EN LUGAR DE EMPEZAR EN 0

1 Reply

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Enrique_Pi1000 ,

    According to your description, it seems that you want to get moving average. You can refer the following links to get it:

    How to Calculate Rolling Average in Power BI

    Moving Average (AVERAGEX) =
    VAR LastTransactionDate =
        MAX ( 'Dates'[Transaction_Date] )
    VAR AverageDay = 30
    VAR PeriodInVisual =
        FILTER (
            ALL ( 'Dates'[Transaction_Date] ),
            AND (
                'Dates'[Transaction_Date] > LastTransactionDate - AverageDay,
                'Dates'[Transaction_Date] <= LastTransactionDate
            )
        )
    VAR OutPut =
        CALCULATE ( AVERAGEX ( 'Dates', [Total Sales] ), PeriodInVisual )
    RETURN
        OutPut

    How to calculate moving average or sum in Power BI

    _30d moving sum = 
    VAR period = 30
    VAR the_first_date =
        CALCULATE ( FIRSTDATE ( MyTable[Date] ), ALLSELECTED ( MyTable[Date] ) )
    VAR last_date =
        LASTDATE ( MyTable[Date] )
    VAR sum_in_period =
        CALCULATE (
            SUM ( MyTable[Value] ),
            DATESINPERIOD ( MyTable[Date], last_date, - period, DAY )
        )
    RETURN
        IF ( last_date - the_first_date >= period - 1, sum_in_period )

    Best Regards