Forum Discussion

Chaitanya122's avatar
Chaitanya122
Regular Visitor
4 years ago
Solved

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 funct...
  • Anonymous's avatar
    Anonymous
    4 years ago

    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 )

    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