Forum Discussion
Average date difference between two dates in same column
- 6 years ago
Hi Anonymous
Sorry for replying late.
Please create measures
lastmonth = CALCULATE(MAX('date'[year-month]),FILTER(ALLSELECTED('Table'),'Table'[CustomerID]=MAX('Table'[CustomerID]))) measure in days = VAR days1 = CALCULATE ( SUM ( 'date'[days_m] ), VALUES ( 'date'[year-month] ), FILTER ( ALLSELECTED ( 'Table' ), 'Table'[CustomerID] = MAX ( 'Table'[CustomerID] ) ) ) RETURN IF ( [lastmonth] = MAX ( 'date'[year-month] ) && [lastmonth] <> BLANK (), MAX ( 'date'[days_m] ) / 2, days1 ) each days = SUMX(FILTER(ALLSELECTED('Table'),'Table'[CustomerID]=MAX('Table'[CustomerID])),[measure in days]) average dyas = SUMX(ALLSELECTED('Table'),[measure in days])/2The date table used above
date = ADDCOLUMNS ( CALENDARAUTO (), "year", YEAR ( [Date] ), "month", MONTH ( [Date] ), "year-month", FORMAT ( [Date], "yyyy-mm" ) ) add a column in date table days_m = CALCULATE(COUNT('date'[Date]),ALLEXCEPT('date','date'[year-month]))Best Regards
Maggie
Community Support Team _ Maggie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. - 6 years ago
Hi Anonymous
Is this ok?
lastmonth = CALCULATE ( MAX ( 'date'[year-month] ), FILTER ( ALLSELECTED ( 'Table' ), 'Table'[CustomerID] = MAX ( 'Table'[CustomerID] ) && 'Table'[country] = MAX ( 'Table'[country] ) ) ) measure in days = VAR days1 = CALCULATE ( SUM ( 'date'[days_m] ), VALUES ( 'date'[year-month] ), FILTER ( ALLSELECTED ( 'Table' ), 'Table'[CustomerID] = MAX ( 'Table'[CustomerID] ) && 'Table'[country] = MAX ( 'Table'[country] ) ) ) RETURN IF ( [lastmonth] = MAX ( 'date'[year-month] ) && [lastmonth] <> BLANK (), MAX ( 'date'[days_m] ) / 2, days1 ) each days = SUMX ( FILTER ( ALLSELECTED ( 'Table' ), 'Table'[CustomerID] = MAX ( 'Table'[CustomerID] ) && 'Table'[country] = MAX ( 'Table'[country] ) ), [measure in days] ) total eachdays = IF ( ISINSCOPE ( 'Table'[CustomerID] ), [each days], AVERAGEX ( SUMMARIZE ( 'Table', 'Table'[CustomerID], "m", [each days] ), [m] ) )Best Regards
Maggie
Hi Anonymous
Sorry for replying late.
Please create measures
lastmonth = CALCULATE(MAX('date'[year-month]),FILTER(ALLSELECTED('Table'),'Table'[CustomerID]=MAX('Table'[CustomerID])))
measure in days =
VAR days1 =
CALCULATE (
SUM ( 'date'[days_m] ),
VALUES ( 'date'[year-month] ),
FILTER (
ALLSELECTED ( 'Table' ),
'Table'[CustomerID]
= MAX ( 'Table'[CustomerID] )
)
)
RETURN
IF (
[lastmonth]
= MAX ( 'date'[year-month] )
&& [lastmonth]
<> BLANK (),
MAX ( 'date'[days_m] ) / 2,
days1
)
each days = SUMX(FILTER(ALLSELECTED('Table'),'Table'[CustomerID]=MAX('Table'[CustomerID])),[measure in days])
average dyas = SUMX(ALLSELECTED('Table'),[measure in days])/2
The date table used above
date =
ADDCOLUMNS (
CALENDARAUTO (),
"year", YEAR ( [Date] ),
"month", MONTH ( [Date] ),
"year-month", FORMAT (
[Date],
"yyyy-mm"
)
)
add a column in date table
days_m = CALCULATE(COUNT('date'[Date]),ALLEXCEPT('date','date'[year-month]))
Best Regards
Maggie
Community Support Team _ Maggie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi v-juanli-msft ,
I was wondering if it is possible to add another specification in the measure. For example, if you add a column to specifiy the country for each customer, let's say Spain and France (see image). I tried to change the first measure:
But that makes no difference compared to the measure 'each days'... Is there a way to create this measure?
- v-juanli-msft6 years agoCommunity Support
Hi Anonymous
Is this ok?
lastmonth = CALCULATE ( MAX ( 'date'[year-month] ), FILTER ( ALLSELECTED ( 'Table' ), 'Table'[CustomerID] = MAX ( 'Table'[CustomerID] ) && 'Table'[country] = MAX ( 'Table'[country] ) ) ) measure in days = VAR days1 = CALCULATE ( SUM ( 'date'[days_m] ), VALUES ( 'date'[year-month] ), FILTER ( ALLSELECTED ( 'Table' ), 'Table'[CustomerID] = MAX ( 'Table'[CustomerID] ) && 'Table'[country] = MAX ( 'Table'[country] ) ) ) RETURN IF ( [lastmonth] = MAX ( 'date'[year-month] ) && [lastmonth] <> BLANK (), MAX ( 'date'[days_m] ) / 2, days1 ) each days = SUMX ( FILTER ( ALLSELECTED ( 'Table' ), 'Table'[CustomerID] = MAX ( 'Table'[CustomerID] ) && 'Table'[country] = MAX ( 'Table'[country] ) ), [measure in days] ) total eachdays = IF ( ISINSCOPE ( 'Table'[CustomerID] ), [each days], AVERAGEX ( SUMMARIZE ( 'Table', 'Table'[CustomerID], "m", [each days] ), [m] ) )Best Regards
Maggie
- Anonymous6 years agoNot applicable
Thank you so much!