cancel
Showing results for
Did you mean:
Highlighted
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
Highlighted
Resident Rockstar

## Re: New field based on date range

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.

Highlighted
Solution Sage

## Re: New field based on date range

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.

Highlighted
Microsoft

## Re: New field based on date range

@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:

Regards,

Highlighted
Helper I

## Re: New field based on date range

Unfortunity it didn't work.

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

- Babinsky

Highlighted
Helper I

## Re: New field based on date range

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

Highlighted
Super User IV

## Re: New field based on date range

```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
)```

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.

Highlighted
Helper I

## Re: New field based on date range

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
)

Highlighted
Community Champion

## Re: New field based on date range

@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

Announcements

#### August 2020 Community Challenge: Can You Solve These?

We're excited to announce our first cross-community 'Can You Solve These?' challenge!

#### Community Blog

Visit our Community Blog for articles, guides, and information created by fellow community members.

#### Upcoming Events

Wondering what events you could join or have an event to promote yourself? Check out our Upcoming Events.

#### Community Summit Australia – Join Online!

Be a part of the leading Microsoft Business Applications digital event, curated for the APAC community.

Top Solution Authors
Top Kudoed Authors