cancel
Showing results for
Did you mean:
Helper II

## Formula for Date closest to Today()

Hello community,

this is probably a trivial question but I could not find a solution yet.

Example: A product can have multiple shipping dates. I want to create a page level filter where the shipping date is closest to Today() so that only one row is being displayed.

If there were three dates (three rows) the colum "ClosestShippingDate" would contain two 0 values and one row with a 1 as value.

In a meassure I did something like this:

`isClosestShippingDate = DATEDIFF(TODAY();MIN('Products'[ShippingDate]);DAY) `

which is not correct because I need the value closest to zero instead of MIN().

How can I achieve this? Ideally with PowerQuery since I don't think a measure can be a page level filter.

Thank you.

1 ACCEPTED SOLUTION
Community Support

Hi @tonyclifton ,

Combined one as below.

```isClosestShippingDate =
VAR mindate =
CALCULATE (
MIN ( 'Table1'[shipping date] ),
FILTER ( Table1, Table1[product] = EARLIER ( Table1[product] ) && Table1[shipping date]>=TODAY() )
)
VAR maxdate =
CALCULATE (
MAX('Table1'[shipping date] ),
FILTER ( Table1, Table1[product] = EARLIER (Table1[product]) && Table1[shipping date]<=TODAY() )
)
RETURN
IF (
'Table1'[shipping date] = mindate,
DATEDIFF ( TODAY (), mindate, DAY ),
IF(Table1[shipping date]=maxdate,DATEDIFF(TODAY(),maxdate,DAY)
)
)```

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.
7 REPLIES 7
Community Support

Hi @tonyclifton ,

We can create a calculated column instead of meaure to work on it.

```isClosestShippingDate =
VAR mindate =
CALCULATE (
MIN ( 'Table1'[shipping date] ),
FILTER ( Table1, Table1[product] = EARLIER ( Table1[product] ) )
)
RETURN
IF (
'Table1'[shipping date] = mindate,
DATEDIFF ( TODAY (), mindate, DAY ),
BLANK ()
)
```

Then we can filter the blank value out to get the excepted result as we need.

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.
Helper II

@v-frfei-msftthank you for the sample - it is working great.
However, I forgot to mention that only ShippingDates greater or equal than today should be considered so I added an AND to the filter method.
But this way I have no information about products with dates in the past which is definitely needed sooner or later.

Is there a way to also deal with Projects that only have ShippingDates in the past, so that only the closest value to today is being displayed?

Here is how I modified your formula for the filter function:

```isClosestShippingDate =
VAR mindate =
CALCULATE ( MIN ( 'Table1'[shipping date] ),
FILTER ( Table1, Table1[product] = EARLIER ( Table1[product] )
&& 'Table1'[shipping date] >= TODAY()
))
RETURN IF ( 'Table1'[shipping date] = mindate, 1, 0 )```

Thank you.

Community Support

Hi @tonyclifton ,

```isClosestShippingDatepast =
VAR maxdate =
CALCULATE (
MAX('Table1'[shipping date] ),
FILTER ( Table1, Table1[product] = EARLIER (Table1[product]) && Table1[shipping date]<=TODAY() )
)
RETURN
IF (
'Table1'[shipping date] = maxdate,
DATEDIFF ( TODAY (), maxdate, DAY ),
BLANK ()
)
```

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.
Helper II

@v-frfei-msftthanks. Any change to make a single column out of both so that filtering is easier?

Community Support

Hi @tonyclifton ,

Combined one as below.

```isClosestShippingDate =
VAR mindate =
CALCULATE (
MIN ( 'Table1'[shipping date] ),
FILTER ( Table1, Table1[product] = EARLIER ( Table1[product] ) && Table1[shipping date]>=TODAY() )
)
VAR maxdate =
CALCULATE (
MAX('Table1'[shipping date] ),
FILTER ( Table1, Table1[product] = EARLIER (Table1[product]) && Table1[shipping date]<=TODAY() )
)
RETURN
IF (
'Table1'[shipping date] = mindate,
DATEDIFF ( TODAY (), mindate, DAY ),
IF(Table1[shipping date]=maxdate,DATEDIFF(TODAY(),maxdate,DAY)
)
)```

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.
Helper II

Thanks alot @v-frfei-msft I finally got it working by changing the return statement to:

`RETURN SWITCH(TRUE() ; mindate <> BLANK() && Table1[shipping date] = mindate; 1 `
`; maxdate <> BLANK() && mindate = BLANK() && 'Table1'[shipping date] = maxdate ;1 ;0)`
Anonymous
Not applicable

@tonyclifton - You can use the ABS function to get the Absolute Value and then get the minimum.

Announcements

#### Check it Out!

Mark your calendars and join us on Thursday, August 25 at 11a PDT for a great session with Ted Pattison!

#### The Power BI Community Show

Watch the playback when Priya Sathy and Charles Webb discuss Datamarts! Kelly also shares Power BI Community updates.