The ultimate Microsoft Fabric, Power BI, Azure AI, and SQL learning event: Join us in Stockholm, September 24-27, 2024.
Save €200 with code MSCUST on top of early bird pricing!
Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started
Hello,
I'm struggling with DAX. I am trying to create a calculated column that count rows in a table where the date is either blank OR within the past 2 calendar months based on today's date. How would I write out the DAX for this? The main part I am struggling with is if the date is within the current month or previous month.
ID | Date | Cal. Column |
1 | 1 | |
2 | 4/15/2021 | |
3 | 3/20/2020 | |
4 | 5/3/2021 | 1 |
5 | 6/2/2021 | 1 |
6 | 4/28/2021 |
Thank you -
Solved! Go to Solution.
@lilych - Ah! I see what you mean. You can do as an IF:
Column =
IF (
AND (
MONTH ( TableName[Date] )
> ( MONTH ( TODAY () ) - 2 ),
YEAR ( TableName[Date] ) = YEAR ( TODAY () )
)
|| ISBLANK ( TableName[Date] ),
1,
BLANK ()
)
or you can do as SWITCH:
SWITCHColumn =
SWITCH (
TRUE (),
ISBLANK ( TableName[Date] ), 1,
AND (
MONTH ( TableName[Date] )
> ( MONTH ( TODAY () ) - 2 ),
YEAR ( TableName[Date] ) = YEAR ( TODAY () )
), 1,
BLANK ()
)
Proud to be a Super User!
@lilych - Try:
Column =
IF(
MONTH(TableName[Date]) > (MONTH(TODAY()) - 2)
|| ISBLANK(TableName[Date]),
1,
BLANK()
)
Proud to be a Super User!
Thanks @ChrisMendoza! It works for dates in this year where it excludes rows from Jan-April 2021, however in my file, it seems to be including rows where the date is in 2020. How can I exclude that?
@lilych - Ah! I see what you mean. You can do as an IF:
Column =
IF (
AND (
MONTH ( TableName[Date] )
> ( MONTH ( TODAY () ) - 2 ),
YEAR ( TableName[Date] ) = YEAR ( TODAY () )
)
|| ISBLANK ( TableName[Date] ),
1,
BLANK ()
)
or you can do as SWITCH:
SWITCHColumn =
SWITCH (
TRUE (),
ISBLANK ( TableName[Date] ), 1,
AND (
MONTH ( TableName[Date] )
> ( MONTH ( TODAY () ) - 2 ),
YEAR ( TableName[Date] ) = YEAR ( TODAY () )
), 1,
BLANK ()
)
Proud to be a Super User!
Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.
Check out the August 2024 Power BI update to learn about new features.
Learn from experts, get hands-on experience, and win awesome prizes.
User | Count |
---|---|
114 | |
79 | |
78 | |
44 | |
39 |
User | Count |
---|---|
150 | |
116 | |
68 | |
64 | |
58 |