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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

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
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

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

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors