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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Babinsky
Helper I
Helper I

New field based on date range

Hi everyone! 

 I have a question and need some assistance. I need to create a new field based on a date range. 

 Ex:

 

Column = IF (DATESBETWEEN  1/01/17, 01/31/17), VALUE1, IF (DATESBETWEEN 02/01/17, 02/28/17), VALUE2)) 

Any assistance would be appreciated.  Thanks!

Babinsky

8 REPLIES 8
v-sihou-msft
Employee
Employee

@Babinsky

 

Since you need to do comparison on row level, you should create a calculated column instead of measure with formula as @Baskar posted.

 

It can be like:

 

Column  = SWITCH(
TRUE(),
'Table'[Date]>=DATE(2017,1,1) && 'Table'[Date]<=DATE(2017,1,31),"Value1",
'Table'[Date]>=DATE(2017,2,1) && 'Table'[Date]<DATE(2017,2,28),"Value2",
....
)

See my sample below:

 

 

44.PNG

 

Regards,

Baskar
Resident Rockstar
Resident Rockstar

Cool dude,

 

Create one measure 

 

Measure =

Switch ( True() ,

              AND( Date >  1/01/17, Date < 01/31/17 ) , Value1,

              AND( Date >  2/01/17, Date < 02/28/17 ) , Value2,

              AND( Date >  3/01/17, Date < 03/31/17 ) , Value3,

.

.

. etc

)

 

 

Try this it will help u , if not let me know i will help u dude.

Unfortunity it didn't work. 

 

Baskar, can you contact me?  I sent you a message.

 

- Babinsky

Anyone who can help, for the following code below what is the syntax for the date field?

 

Measure =

Switch ( True() ,

              AND( Date >  1/01/17, Date < 01/31/17 ) , Value1,

              AND( Date >  2/01/17, Date < 02/28/17 ) , Value2,

              AND( Date >  3/01/17, Date < 03/31/17 ) , Value3)

 

For Example:

File - PM022BIWK

Field  - PDATCR

 

Measure =
Switch ( True() ,
              AND( Date > Date(2017,1,1), Date < Date(2017,1,31)),Value1,
              AND( Date > Date(2017,2,1), Date < Date(2017,02,28)) ,Value2,
              AND( Date > Date(2017,3,1), Date < Date(2017,03,31)) , Value3
)


Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

What I need to know if how do you specify in the code where you're pulling the date field from and what is the syntax?  Which date field below needs replaced?

 

For Example:

File - PM022BIWK

Field  - PDATCR

 

Measure =
Switch ( True() ,
              AND( Date > Date(2017,1,1), Date < Date(2017,1,31)),Value1,
              AND( Date > Date(2017,2,1), Date < Date(2017,02,28)) ,Value2,
              AND( Date > Date(2017,3,1), Date < Date(2017,03,31)) , Value3
)

 

 

 

@Babinsky

 

You can use a Variable

 

Measure =

VAR Date= Values('PM022BIWK'[PDATCR])

RETURN
Switch ( True() ,
              AND( Date > Date(2017,1,1), Date < Date(2017,1,31)),Value1,
              AND( Date > Date(2017,2,1), Date < Date(2017,02,28)) ,Value2,
              AND( Date > Date(2017,3,1), Date < Date(2017,03,31)) , Value3
)

 

 

Or you can replace in:

 

Measure =

Switch ( True() ,
              AND(  Values('PM022BIWK'[PDATCR])> Date(2017,1,1), Values('PM022BIWK'[PDATCR]) < Date(2017,1,31)),Value1,
              AND( Values('PM022BIWK'[PDATCR]) > Date(2017,2,1), Values('PM022BIWK'[PDATCR]) < Date(2017,02,28)) ,Value2,
              AND( Values('PM022BIWK'[PDATCR]) > Date(2017,3,1), Values('PM022BIWK'[PDATCR]) < Date(2017,03,31)) , Value3
)




Lima - Peru
Anonymous
Not applicable

@Babinsky,

If you're only interested in Jan 2017 and Feb 2017 and Mar 2017, then the approach from @Baskar should work, though you might need to adapt it a little with a MAX and  >= and <= in place of > and < respectively - e.g. 

 

 

DateValue = 
SWITCH ( TRUE() ,
AND ( MAX([Date]) >= DATE(2017,1,1), MAX([Date]) <= DATE(2017,1,31) ), "Value1",
AND ( MAX([Date]) >= DATE(2017,2,1), MAX([Date]) <= DATE(2017,2,28) ), "Value2",
AND ( MAX([Date]) >= DATE(2017,3,1), MAX([Date]) <= DATE(2017,3,31) ), "Value3",
BLANK()
)

If you're interested in years aside from 2017 or months past March, there may be a better solution depending on what you're trying to do.

 

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.