我试图获取表视图 power bi 中的天数差异,但它没有显示 dax 每一行的差异。我想要实现的是天数的逐行差异,并且如果 one-Id 具有仅针对最大阶段的当前日期计算的最后一天。
期望这样的输出:
示例表:
ID Date Stage. Diff
101 2025-01-01 Stage 1 4
101 2025-01-05 Stage 2 5
101 2025-01-10 Stage 3 2025-01-10 - current date
102 2025-01-02 Stage 1 5
102 2025-01-07 Stage 2 3
102 2025-01-10 Stage 3 2025-01-10 - current date
VAR Id = SELECTEDVALUE('table'[ID])
-- Step 1: Get the current date for the selected ID
VAR CurrentDate =
CALCULATE(
MAX('table'[date]),
FILTER(
'table',
'table'[ID] = Id
)
)
-- Step 2: Get the current stage name for the selected Opportunity ID
VAR CurrentStage =
CALCULATE(
MAX('table'[stage]),
FILTER(
'table',
'table'[ID] = Id &&
'table'[date] = CurrentDate
)
)
-- Step 3: Find the next row date (earliest date greater than CurrentDate) for the selected Opportunity ID
VAR NextRowDate =
CALCULATE(
MIN('table'[date]),
FILTER(
'table',
'table'[ID] = Id &&
'table'[date] > CurrentDate
)
)
-- Step 4: Find the stage name associated with the NextRowDate
VAR NextStage =
CALCULATE(
MAX('table[stage]),
FILTER(
'table',
'table[ID] = Id &&
'table'[date] = NextRowDate
)
)
-- Step 5: Calculate the difference in days
VAR DaysDiff =
IF(
ISBLANK(NextRowDate),
DATEDIFF(CurrentDate, TODAY(), DAY), -- If it's the last entry, calculate difference from today
IF(
CurrentStage = NextStage,
0, -- No change in stage
DATEDIFF(CurrentDate, NextRowDate, DAY) -- Calculate days difference
)
)
-- Step 6: Return the final result if DaysDiff is positive
RETURN
IF(DaysDiff >= 0, DaysDiff)
在powerbi中,一列只能有一种日期类型。所以我认为你不能同时显示日期和数字,除非你将日期更改为数字类型
您可以创建一个专栏
Column=
VAR _date =
MINX (
FILTER (
'Table',
'Table'[ID] = EARLIER ( 'Table'[ID] )
&& 'Table'[Date] > EARLIER ( 'Table'[Date] )
),
'Table'[Date]
)
RETURN
IF ( ISBLANK ( _date ), 'Table'[Date], DATEDIFF ( 'Table'[Date], _date, DAY ) )
或措施
MEASURE =
VAR _date =
MINX (
FILTER (
ALL ( 'Table' ),
'Table'[ID] = MAX ( 'Table'[ID] )
&& 'Table'[Date] > MAX ( 'Table'[Date] )
),
'Table'[Date]
)
RETURN
IF (
ISBLANK ( _date ),
MAX ( 'Table'[Date] ),
DATEDIFF ( MAX ( 'Table'[Date] ), _date, DAY )
)