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

Shape the future of the Fabric Community! Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions. Take survey.

Reply
powerbi2srm
Resolver II
Resolver II

Calcular la suma total anual para el año anterior

Hola!

¿Cómo puedo calcular con una medida el total anual para el año anterior al actual? En concreto, necesito calcular el total de ventas del año pasado. De esta forma, si a esto le sumo otra medida con las ventas del año actual (y voy filtrando por meses) conseguiré el acumulado del año pasado y lo que va de éste.

Os pongo un ejemplo para que quede más claro: lo que busco es filtrar con un slicer por marzo de 2022 y obtener como resultado de la medida: la suma acumulada de todo 2021, enero de 2022, febrero de 2022 y marzo de 2022


Esto es lo que he intentado

 

total_sales = 

VAR sales_last_year =
CALCULATE(
    SUM(fact_sale_order[sales_amount]),
    DATEADD(dim_date[formatted_date], -1, YEAR),
    REMOVEFILTERS(dim_date[month_name])
)

VAR sales_current_year =
CALCULATE(
    SUM(fact_sale_order[sales_amount]),
    DATESYTD(dim_date[formatted_date])
)

RETURN sales_last_year + sales_current_year

 

 


Sin embargo, si yo filtro esto por meses, el resultado de esta medida se verá afectado, dado que REMOVEFILTERS no me funciona como esperaba

Muchas gracias!!

1 ACCEPTED SOLUTION

Acabo de encontrar la solución al problema:

total_sales = 
VAR last_year = YEAR(MAX(dim_date[formatted_date]))-1

VAR sales_last_year =
    CALCULATE(
        SUM(fact_sale_order[sales_amount]),
        FILTER(ALLEXCEPT(dim_date, dim_date[year]), YEAR(dim_date[formatted_date]) = last_year )
    )

VAR sales_current_year =
CALCULATE(
    SUM(fact_sale_order[sales_amount]),
    DATESYTD(dim_date[formatted_date])
)
RETURN sales_last_year + sales_current_year

 

View solution in original post

4 REPLIES 4
lbendlin
Super User
Super User

You can (for example)  add a REMOVEFILTERS  filter to your CALCULATE statements to remove the month filter. 

 

Please provide sanitized sample data that fully covers your issue.
https://community.powerbi.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-Forum/ba-...

Acabo de encontrar la solución al problema:

total_sales = 
VAR last_year = YEAR(MAX(dim_date[formatted_date]))-1

VAR sales_last_year =
    CALCULATE(
        SUM(fact_sale_order[sales_amount]),
        FILTER(ALLEXCEPT(dim_date, dim_date[year]), YEAR(dim_date[formatted_date]) = last_year )
    )

VAR sales_current_year =
CALCULATE(
    SUM(fact_sale_order[sales_amount]),
    DATESYTD(dim_date[formatted_date])
)
RETURN sales_last_year + sales_current_year

 

Note that ALLEXCEPT is a rather blunt instrument.  REMOVEFILTERS is more gentle.

Sorry, but I did't see your reply. I just created an example .pbix for you. Thank you so much!! https://drive.google.com/file/d/1Rrun57vMLM7tfF-SmIpsWrohRBcZvTUh/view?usp=sharing

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.