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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
sridharbabu
Helper I
Helper I

how to add last two month column data dynamically based on item number

for eg in nov 23 for product A having order of 50,

in dec 23 for product A having order of 45

output:

in jan 24 for product A order should be 50 + 40=90 (nov23 product A order + dec 23 product A order).

It has to go dynamically for feb 24, jan24+dec 23.

how to achieve this in power bi

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @sridharbabu 

 

For your question, here is the method I provided:

 

Here's some dummy data

 

"Table"

vnuocmsft_0-1706075213956.png

 

Set the date type you need, for example: mmmm d.

vnuocmsft_2-1706075538034.png

 

Create a measure. Calculate the sum of orders in the last two months.

total order = 
    var max_date = CALCULATE(MAX('Table'[monthyear]), FILTER(ALL('Table'), 'Table'[Product] = MAX('Table'[Product])))
    var start_date = EDATE(max_date, -2)
return 
    CALCULATE(
        SUM('Table'[order]), 
        FILTER(
            ALL('Table'), 
            'Table'[Product] = MAX('Table'[Product]) 
            &&
            'Table'[monthyear] >= start_date 
            &&
            'Table'[monthyear] < max_date))

 

Here is the result.

 

vnuocmsft_1-1706075415101.png

 

Regards,

Nono Chen

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

6 REPLIES 6
amustafa
Super User
Super User

It would help if you can provide a sample data and variations of date formats in your table.





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




sample data

ProductlocationmonthyearorderMonthSeq
3245abcNov-233423
3245abcOct-235622
5255xyzNov-234523
5255xyzOct-239622
Anonymous
Not applicable

Hi @sridharbabu 

 

For your question, here is the method I provided:

 

Here's some dummy data

 

"Table"

vnuocmsft_0-1706075213956.png

 

Set the date type you need, for example: mmmm d.

vnuocmsft_2-1706075538034.png

 

Create a measure. Calculate the sum of orders in the last two months.

total order = 
    var max_date = CALCULATE(MAX('Table'[monthyear]), FILTER(ALL('Table'), 'Table'[Product] = MAX('Table'[Product])))
    var start_date = EDATE(max_date, -2)
return 
    CALCULATE(
        SUM('Table'[order]), 
        FILTER(
            ALL('Table'), 
            'Table'[Product] = MAX('Table'[Product]) 
            &&
            'Table'[monthyear] >= start_date 
            &&
            'Table'[monthyear] < max_date))

 

Here is the result.

 

vnuocmsft_1-1706075415101.png

 

Regards,

Nono Chen

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

 

 

 

In continuation for above question, how can i display dynamically the previous two months data in table in power bi. loading data from sql to power bi. Every month add data to sql table. Dynamically has to show previous two months of data as table in power bi.

sridharbabu
Helper I
Helper I

Date in table is of string format like Nov-23. How to use it.

amustafa
Super User
Super User

Following DAX measure should correctly calculate the sum of orders for the two months preceding the month of the selected date. 

 

Last Two Months Orders =
VAR SelectedDate = SELECTEDVALUE(MyTable[Date])
VAR FirstDayOfSelectedMonth = DATE(YEAR(SelectedDate), MONTH(SelectedDate), 1)
VAR StartOfPreviousTwoMonths = EOMONTH(FirstDayOfSelectedMonth, -3) + 1
VAR EndOfPreviousTwoMonths = EOMONTH(FirstDayOfSelectedMonth, -1)

RETURN
CALCULATE(
    SUM(MyTable[Order]),
    FILTER(
        ALL(MyTable),
        MyTable[Date] >= StartOfPreviousTwoMonths && MyTable[Date] <= EndOfPreviousTwoMonths
    )
)
 
 




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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