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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
crhisbergas
Regular Visitor

Column with SUMX and filter by dates and ids

Hi,

Im working to create a calculated column with the average sales of last 12 months.

But I don't know how to filter the last 12 months, I've tried in many ways but Powerbi alwys returns error. Can someone help me on this.

 

Sales_12Mth =
SUMX(
    FILTER(
    Sales,
          Sales[Cod_Cliente] = Clients[ID_Cliente] &&
          Sales[Cod_Prod] = Sales_Clients[ID_Produto]),
     Sales[Quantidade]
)
2 ACCEPTED SOLUTIONS

Hi @crhisbergas ,

Tweak the formula like this:

Sales_12Mth =
VAR __Today =
    TODAY ()
VAR __12MonthsAgo =
    EOMONTH ( __Today, -12 )
VAR __Begin =
    DATE ( YEAR ( __12MonthsAgo ), MONTH ( __12MonthsAgo ), DAY ( __Today ) )
RETURN
    SUMX (
        FILTER (
            Sales,
            Sales[Cod_Cliente] = Clients[ID_Cliente]
                && Sales[Cod_Prod] = Sales_Clients[ID_Produto]
                && Sales[Date] >= __Begin
                && Sales[Date] <= __Today
        ),
        Sales[Quantidade]
    )

Best Regards,
Community Support Team _ kalyj

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

View solution in original post

v-yanjiang-msft
Community Support
Community Support

Hi @crhisbergas ,

Is your problem solved? If so, would you mind accept the helpful replies as solutions? Then we are able to close the thread. More people who have the same requirment will find the solution quickly and benefit here, thank you!

 

Best Regards,
Community Support Team _ kalyj

View solution in original post

5 REPLIES 5
v-yanjiang-msft
Community Support
Community Support

Hi @crhisbergas ,

Is your problem solved? If so, would you mind accept the helpful replies as solutions? Then we are able to close the thread. More people who have the same requirment will find the solution quickly and benefit here, thank you!

 

Best Regards,
Community Support Team _ kalyj

Hi All,

with your support I can finish the job.

 

thanks a lot!!!

Greg_Deckler
Super User
Super User

@crhisbergas Try:

Sales_12Mth =
  VAR __Today = TODAY()
  VAR __12MonthsAgo = EOMONTH(__Today, -12)
  VAR __Begin = DATE(YEAR(__12MonthsAgo), MONTH(__12MonthsAgo), DAY(__Today))
  VAR __Result = 
SUMX(
    FILTER(
    Sales,
          Sales[Cod_Cliente] = Clients[ID_Cliente] &&
          Sales[Cod_Prod] = Sales_Clients[ID_Produto]) &&
          Sales[Date] >= __Begin && Sales[Date] <= __Today,
     Sales[Quantidade]
)


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Hi Greg,

When I tried to run its showing the message.

 

The syntax )' it's incorrect. --- Error message:

A sintaxe de ')' está incorreta. (DAX( VAR __Today = TODAY() VAR __12MonthsAgo = EOMONTH(__Today, -12) VAR __Begin = DATE(YEAR(__12MonthsAgo), MONTH(__12MonthsAgo), DAY(__Today)) VAR __Result = SUMX( FILTER( Sales, Sales[Cod_Cliente] = Clients[ID_Cliente] && Sales[Cod_Prod] = Sales_Clients[ID_Produto] && (Sales[Date] >= __Begin && Sales[Date] <= __Today), Sales[Quantidade])))).

 

 

Sales_12Mth =
  VAR __Today = TODAY()
  VAR __12MonthsAgo = EOMONTH(__Today, -12)
  VAR __Begin = DATE(YEAR(__12MonthsAgo), MONTH(__12MonthsAgo), DAY(__Today))
  VAR __Result =
SUMX(
    FILTER(
    Sales,
          Sales[Cod_Cliente] = Clients[ID_Cliente] &&
          Sales[Cod_Prod] = Sales_Clients[ID_Produto] &&
          (Sales[Date] >= __Begin && Sales[Date] <= __Today),
     Sales[Quantidade]
)

Hi @crhisbergas ,

Tweak the formula like this:

Sales_12Mth =
VAR __Today =
    TODAY ()
VAR __12MonthsAgo =
    EOMONTH ( __Today, -12 )
VAR __Begin =
    DATE ( YEAR ( __12MonthsAgo ), MONTH ( __12MonthsAgo ), DAY ( __Today ) )
RETURN
    SUMX (
        FILTER (
            Sales,
            Sales[Cod_Cliente] = Clients[ID_Cliente]
                && Sales[Cod_Prod] = Sales_Clients[ID_Produto]
                && Sales[Date] >= __Begin
                && Sales[Date] <= __Today
        ),
        Sales[Quantidade]
    )

Best Regards,
Community Support Team _ kalyj

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

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

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

Feb2025 NL Carousel

Fabric Community Update - February 2025

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

Top Solution Authors