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

The Power BI Data Visualization World Championships is back! It's time to submit your entry. Live now!

Reply
NilR
Post Patron
Post Patron

Create Automated Date Range

What is the best way to create below case statement in PBI? I do not want to hard code the date range. Would like to autmate the green font.

 

 

 

CASE 
WHEN DATE BETWEEN '2020-01-01' AND '2020-01-31' THEN 'January 2020'
WHEN DATE BETWEEN '2020-02-01' AND '2020-02-29' THEN 'February 2020'
WHEN DATE BETWEEN '2020-03-01' AND '2020-03-31' THEN 'March 2020'
WHEN DATE BETWEEN '2020-04-01' AND '2020-04-30' THEN 'April 2020'
WHEN DATE BETWEEN '2020-05-01' AND '2020-05-31' THEN 'May 2020'
WHEN DATE BETWEEN '2020-06-01' AND '2020-06-30' THEN 'June 2020'
WHEN DATE BETWEEN '2020-07-01' AND '2020-07-31' THEN 'July 2020'
WHEN DATE BETWEEN '2020-08-01' AND '2020-08-31' THEN 'August 2020'
WHEN DATE BETWEEN '2020-09-01' AND '2020-09-30' THEN 'September 2020'
WHEN DATE BETWEEN '2020-10-01' AND '2020-10-31' THEN 'October 2020'
WHEN DATE BETWEEN '2020-11-01' AND '2020-11-30' THEN 'November 2020'
WHEN DATE BETWEEN '2020-12-01' AND '2020-12-31' THEN 'December 2020'
WHEN DATE BETWEEN '2021-01-01' AND '2021-01-07' THEN 'January 1st - 7th 2021'
WHEN DATE BETWEEN '2021-01-08' AND '2021-01-14' THEN 'January 8th - 14th 2021'
WHEN DATE BETWEEN '2021-01-15' AND '2021-01-21' THEN 'January 15th - 21st 2021'
WHEN DATE BETWEEN '2021-01-22' AND '2021-01-31' THEN 'January 22nd - 31st 2021'
WHEN DATE BETWEEN '2021-02-01' AND '2021-02-07' THEN 'February 1st - 7th 2021'
WHEN DATE BETWEEN '2021-02-08' AND '2021-02-14' THEN 'February 8th - 14th 2021'
WHEN DATE BETWEEN '2021-02-15' AND '2021-02-21' THEN 'February 15th - 21st 2021'
WHEN DATE BETWEEN '2021-02-22' AND '2021-02-28' THEN 'February 22nd - 28th 2021'
ELSE 'OTHER' END

 

 

 

 Thanks!

1 ACCEPTED SOLUTION

@NilR Perhaps just set the display format for the DATE column to month and year, it's a default option under Column Tools when you have a date column.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

8 REPLIES 8
v-janeyg-msft
Community Support
Community Support

Hi, @NilR 

 

The above replies make sense, but it seems not what you want. Do you have a date column? Do you want to create a new date column in month format and week format? Or how do you want to use it? Can you share your thoughts or  your desired result? So we can help you soon.

 

Best Regards

Janey Guo

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

smpa01
Community Champion
Community Champion

@NilR 

new column= SWITCH(TRUE(), 
                [Column1]>=DATE(2021,1,1)&&[Column1]<=DATE(2021,1,31),"jan",
                [Column1]>=DATE(2021,2,1)&&[Column1]<=DATE(2021,2,28),"feb",
               ............................................................
               [Column1]>=DATE(2021,12,1)&&[Column1]<=DATE(2021,12,31),"dec",
               "others")
             
Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

I don't want to hard code the date. is it still possible?

smpa01
Community Champion
Community Champion

@NilR not sure what do you mean by that. You are hardcoding the date in SQL CASE statement as it stands

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
Greg_Deckler
Community Champion
Community Champion

@NilR - In DAX you could use a SWITCH TRUE statement. In Power Query you would need nested if then else statements, so something like the following in DAX:

SWITCH(TRUE(),
 [DATE] >= DATE(2020,1,1) && [DATE] <= DATE(2020,1,31),"January 2020",
 [DATE] >= DATE(2020,2,1) && [DATE] <= DATE(2020,2,29),"February 2020",
...,
 "Other"
)

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

I don't want to hard code the date. what do you suggest?

@NilR Perhaps just set the display format for the DATE column to month and year, it's a default option under Column Tools when you have a date column.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Does it drill down to weeks?

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! It's time to submit your entry.

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.