Forum Discussion
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.
will give kudos
Advance Thanks,
Thanks
Chaitanya
- Anonymous4 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
2 Replies
- AnonymousNot 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 )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
This is a standard "penultimate" pattern. What have you tried and where are you stuck?