Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

incluir sólo los mismos valores que están en el mes anterior

Hola

Im strugling con el cálculo que debe incluir solo los mismos valores que están en el mes anterior.

Necesito calcular % de cambio de precio durante meses, pero tengo que incluir sólo los valores que se rellenan en el mes anterior (si incluyo todos los valores, el resultado está distorsionado).

Ejemplo:

para 2020/04 sólo se incluye el tipo B y C en el precio actual (el tipo D no estaba en el mes anterior)
precio anterior incluye sólo B y C también (el tipo A no está en el mes actual)

% de variación será de 110/90 a 20%

image_2020-11-30_104038.png

TipoMesPrecio
A2020/01100
B2020/0150
C2020/0150
D2020/0110
A2020/02110
B2020/0250
C2020/0250
D2020/0210
A2020/03120
B2020/0330
C2020/0350
B2020/0450
C2020/04100
D2020/0410

hasta ahora he intentado "allselected" y "values", pero no es un buen resultado.

Gracias por cualquier ayuda de antemano.

Zuzana

1 ACCEPTED SOLUTION
Icey
Community Support
Community Support

Hola @Zuzana ,

Prueba esto:

1. Cree una columna "StartDayofMonth" en su tabla Fact.

StartDayofMonth =
CONVERT ( [month] & "/1", DATETIME )

startdayofmonth.JPG

2. Cree otra tabla Fechas.

Dates =
ADDCOLUMNS (
    CALENDAR ( DATE ( 2020, 1, 1 ), DATE ( 2020, 4, 30 ) ),
    "YearMonth", FORMAT ( [Date], "YYYY/MM" )
)

datestable.JPG

3. Cree otra tabla Tipos.

Types =
DISTINCT ( 'Fact Table'[type] )

typestable.JPG

4. Crear relaciones.

relationships.JPG

5. Crear medidas.

ModifiedCurrentMonthPrice = 
VAR CurrentMonthPrice =
    SUM ( 'Fact Table'[price] )
VAR PreviousMonthPrice =
    CALCULATE ( SUM ( 'Fact Table'[price] ), PREVIOUSMONTH ( Dates[Date] ) )
VAR ModifiedCurrentMonthPrice =
    IF ( PreviousMonthPrice = BLANK (), BLANK (), CurrentMonthPrice )
RETURN
    ModifiedCurrentMonthPrice
ModifiedPreviousMonthPrice = 
VAR CurrentMonthPrice =
    SUM ( 'Fact Table'[price] )
VAR PreviousMonthPrice =
    CALCULATE ( SUM ( 'Fact Table'[price] ), PREVIOUSMONTH ( Dates[Date] ) )
VAR ModifiedPreviousMonthPrice =
    IF ( CurrentMonthPrice = BLANK (), BLANK (), PreviousMonthPrice )
RETURN
    ModifiedPreviousMonthPrice
% MoM change = 
VAR SumCurrent =
    SUMX ( Types, [ModifiedCurrentMonthPrice] )
VAR SumPrevious =
    SUMX ( Types, [ModifiedPreviousMonthPrice] )
RETURN
    DIVIDE ( SumCurrent - SumPrevious, SumPrevious )

percent.JPG

BTW, archivo .pbix adjunto.

Saludos

Icey

Si este post ayuda, entonces considere Aceptarlo como la solución para ayudar a otros miembros a encontrarlo más rápido.

View solution in original post

4 REPLIES 4
Icey
Community Support
Community Support

Hola @Zuzana ,

Prueba esto:

1. Cree una columna "StartDayofMonth" en su tabla Fact.

StartDayofMonth =
CONVERT ( [month] & "/1", DATETIME )

startdayofmonth.JPG

2. Cree otra tabla Fechas.

Dates =
ADDCOLUMNS (
    CALENDAR ( DATE ( 2020, 1, 1 ), DATE ( 2020, 4, 30 ) ),
    "YearMonth", FORMAT ( [Date], "YYYY/MM" )
)

datestable.JPG

3. Cree otra tabla Tipos.

Types =
DISTINCT ( 'Fact Table'[type] )

typestable.JPG

4. Crear relaciones.

relationships.JPG

5. Crear medidas.

ModifiedCurrentMonthPrice = 
VAR CurrentMonthPrice =
    SUM ( 'Fact Table'[price] )
VAR PreviousMonthPrice =
    CALCULATE ( SUM ( 'Fact Table'[price] ), PREVIOUSMONTH ( Dates[Date] ) )
VAR ModifiedCurrentMonthPrice =
    IF ( PreviousMonthPrice = BLANK (), BLANK (), CurrentMonthPrice )
RETURN
    ModifiedCurrentMonthPrice
ModifiedPreviousMonthPrice = 
VAR CurrentMonthPrice =
    SUM ( 'Fact Table'[price] )
VAR PreviousMonthPrice =
    CALCULATE ( SUM ( 'Fact Table'[price] ), PREVIOUSMONTH ( Dates[Date] ) )
VAR ModifiedPreviousMonthPrice =
    IF ( CurrentMonthPrice = BLANK (), BLANK (), PreviousMonthPrice )
RETURN
    ModifiedPreviousMonthPrice
% MoM change = 
VAR SumCurrent =
    SUMX ( Types, [ModifiedCurrentMonthPrice] )
VAR SumPrevious =
    SUMX ( Types, [ModifiedPreviousMonthPrice] )
RETURN
    DIVIDE ( SumCurrent - SumPrevious, SumPrevious )

percent.JPG

BTW, archivo .pbix adjunto.

Saludos

Icey

Si este post ayuda, entonces considere Aceptarlo como la solución para ayudar a otros miembros a encontrarlo más rápido.

amitchandak
Super User
Super User

@Zuzana , Es por eso que agregué este

MTD price á sumx(values(Table[Type]) , if(isblank([last MTD price]) , blank(),[MTD price 1]))

id el mes pasado está en blanco, tome este mes también como en blanco

amitchandak
Super User
Super User

@Zuzana , Trate con una tabla de fechas e inteligencia de tiempo

MTD precio 1 - CALCULATE(max(Table[price]),DATESMTD('Date'[Date]))
el último precio MTD - CALCULATE(max(Table[price]),DATESMTD(dateadd('Date'[Date],-1,MONTH)))

MTD price á sumx(values(Table[Type]) , if(isblank([last MTD price]) , blank(),[MTD price 1]))

diff á [Precio MTD] -[último precio MTD]

Para obtener lo mejor de la función de inteligencia del tiempo. Asegúrese de que tiene un calendario de fechas y que se ha marcado como la fecha en la vista de modelo. Además, únete a ella con la columna de fecha de tus hechos. Consulte :radacad sqlbi My Video Series Aprecia tus Felicitaciones.

Anonymous
Not applicable

Hola

gracias por su respuesta, no tengo problema para calcular el valor del período anterior.

Tengo problemas con la inclusión de solo los tipos seleccionados (como se describe en el ejemplo)

Zuzana

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.