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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
Chaitanya122
Regular Visitor

How to create Dax logic for this Month and Previous Month

Hi PBI Experts.

 

Here is my question for you in dax ,

 

I have total sales for 4 months but data is loading for 2 months instead of 4 months.
In this case i need to create logic using Dax function. where data is not loaded for that month,then i need to get data for the previous month.

If Previous month data also not loaded then get data for the before last month.

Here are the screenshots for example.

Chaitanya122_0-1650614765209.png

 

 

will give kudos

Advance Thanks,

Thanks 

Chaitanya

 

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Chaitanya122 ,

I created a sample pbix file(see attachment) for you, please check whether that is what you want. You can create a measure as below:

Measure = 
VAR _selyear =
    SELECTEDVALUE ( 'Date'[Year] )
VAR _selmonth =
    SELECTEDVALUE ( 'Date'[Month] )
VAR _selym =
    SELECTEDVALUE ( 'Date'[YearMonth] )
VAR _amount =
    CALCULATE (
        SUM ( 'Table'[Amount] ),
        FILTER (
            'Table',
            YEAR ( 'Table'[Date] ) = _selyear
                && FORMAT ( 'Table'[Date], "mmmm" ) = _selmonth
        )
    )
VAR _predate =
    CALCULATE (
        MAX ( 'Table'[Date] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            VALUE ( YEAR ( 'Table'[Date] ) & FORMAT ( 'Table'[Date], "mm" ) )
                < VALUE ( _selym )
        )
    )
VAR _preamount =
    CALCULATE (
        SUM ( 'Table'[Amount] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            YEAR ( 'Table'[Date] ) = YEAR ( _predate )
                && MONTH ( 'Table'[Date] ) = MONTH ( _predate )
        ),
        ALL ( 'Date' )
    )
RETURN
    IF ( ISBLANK ( _amount ), _preamount, _amount )

yingyinr_0-1650960031619.png

In addition, you can refer the following blog to get it.

Power BI DAX Getting the Value of Previous Non-NULL Row

If the above one can't help you get the desired result, please provide some sample data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples. It is better if you can share a simplified pbix file. Thank you.

How to upload PBI in Community

Best Regards

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Hi @Chaitanya122 ,

I created a sample pbix file(see attachment) for you, please check whether that is what you want. You can create a measure as below:

Measure = 
VAR _selyear =
    SELECTEDVALUE ( 'Date'[Year] )
VAR _selmonth =
    SELECTEDVALUE ( 'Date'[Month] )
VAR _selym =
    SELECTEDVALUE ( 'Date'[YearMonth] )
VAR _amount =
    CALCULATE (
        SUM ( 'Table'[Amount] ),
        FILTER (
            'Table',
            YEAR ( 'Table'[Date] ) = _selyear
                && FORMAT ( 'Table'[Date], "mmmm" ) = _selmonth
        )
    )
VAR _predate =
    CALCULATE (
        MAX ( 'Table'[Date] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            VALUE ( YEAR ( 'Table'[Date] ) & FORMAT ( 'Table'[Date], "mm" ) )
                < VALUE ( _selym )
        )
    )
VAR _preamount =
    CALCULATE (
        SUM ( 'Table'[Amount] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            YEAR ( 'Table'[Date] ) = YEAR ( _predate )
                && MONTH ( 'Table'[Date] ) = MONTH ( _predate )
        ),
        ALL ( 'Date' )
    )
RETURN
    IF ( ISBLANK ( _amount ), _preamount, _amount )

yingyinr_0-1650960031619.png

In addition, you can refer the following blog to get it.

Power BI DAX Getting the Value of Previous Non-NULL Row

If the above one can't help you get the desired result, please provide some sample data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples. It is better if you can share a simplified pbix file. Thank you.

How to upload PBI in Community

Best Regards

lbendlin
Super User
Super User

This is a standard "penultimate"  pattern. What have you tried and where are you stuck?

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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