Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!
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.
Solved! Go to Solution.
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) ) )
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.
@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.
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 () )
@v-frfei-msftthanks. Any change to make a single column out of both so that filtering is easier?
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) ) )
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)
@tonyclifton - You can use the ABS function to get the Absolute Value and then get the minimum.
User | Count |
---|---|
139 | |
113 | |
103 | |
73 | |
63 |
User | Count |
---|---|
136 | |
125 | |
107 | |
70 | |
61 |