Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now
I am trying to calculate a if condition for previousmonth and I got this message
MdxScript(Model) (13, 76) Calculation error as attached photo
Hi @WaelTalaat79 ,
You have to read the fine print here to understand how PREVIOUSMONTH (and similar functions) work:
"This function returns all dates from the previous month, using the first date in the column used as input. For example, if the first date in the dates argument refers to June 10, 2009, this function returns all dates for the month of May, 2009."
So, mainly you have to use this in a "context aware way". Here is the example from the page:
Example
The following sample formula creates a calculated field that calculates the 'previous month sales' for the Internet sales.
To see how this works, create a PivotTable and add the fields, CalendarYear and MonthNumberOfYear, to the Row Labels area of the PivotTable. Then add a calculated field, named Previous Month Sales, using the formula defined in the code section, to the Values area of the PivotTable.
=CALCULATE(SUM(InternetSales_USD[SalesAmount_USD]), PREVIOUSMONTH('DateTime'[DateKey]))
And you could use the measure to get last month data:
lastmonth =
IF (
MONTH ( MAX ( 'Date'[Date] ) ) = 1,
DATE ( YEAR ( MAX ( 'Date'[Date] ) ) - 1, 12, DAY ( MAX ( 'Date'[Date] ) ) ),
DATE ( YEAR ( MAX ( 'Date'[Date] ) ), MONTH ( MAX ( 'Date'[Date] ) ), DAY ( MAX ( 'Date'[Date] ) ) )
)
lastmon2 = IF (
MONTH ( MAX ( 'Date'[Date] ) ) = 1,12,0
)
And you could refer:
Did I answer your question? Mark my post as a solution!
Best Regards
Lucien
@WaelTalaat79
Modify your meusre as follows:
= IF( MONTH( PREVIOUSMONTH( 'Dates'[Date].[Date] ) ) = 12 , 0 , 1 )
⭕ Subscribe and learn Power BI from these videos
⚪ Website ⚪ LinkedIn ⚪ PBI User Group
error message MdxScript(Model) (13, 72)
= 12 mean December so should I select MonthNo instead of date
@WaelTalaat79
Where/what are you using this measure? Please share screenshot of the calcualtion
⭕ Subscribe and learn Power BI from these videos
⚪ Website ⚪ LinkedIn ⚪ PBI User Group
And this is the previous month fromula
Hi,
Please describe the question and show the expected result. Share the download link of your PBI file.
Check out the October 2024 Power BI update to learn about new features.
Learn from experts, get hands-on experience, and win awesome prizes.
User | Count |
---|---|
115 | |
112 | |
105 | |
95 | |
58 |
User | Count |
---|---|
174 | |
147 | |
136 | |
102 | |
82 |