Forum Discussion
NaveenMD
2 years agoHelper I
calculating date difference based on condition
Using the data wated to calculate how many days does it take the system to become green to yellow, green to red and yellow to red incase there are duplicate reds then then the calculation should be t...
- 2 years ago
result :
in order to achieve this, you need 2 columns helperr : index and path.
in power query create the undex using : addcolumns --> add index -- . start index from 1 .
in power bi, create path column as follow :
path = var latest_green = SELECTCOLUMNS( INDEX( 1, DISTINCT( SELECTCOLUMNS( FILTER( Table4 , Table4[Date] <= EARLIER( Table4[Date]) && Table4[color code] = "green" ), [Date], [Index] ) ) ,ORDERBY([Index] , DESC ) ), [Date] ) var ds = FILTER( Table4 , Table4[Date] < EARLIER( Table4[Date]) && Table4[Date] >=latest_green ) var ds_colors = SELECTCOLUMNS(ds , [color code] ) RETURN CONCATENATEX(ds_colors , [color code] , "|")then create the column 2 as follow :
Column 2 = var currentcolor = Table4[color code] var currentdate = Table4[Date] var currentindex = Table4[Index] var prev_color = SELECTCOLUMNS(FILTER(Table4,Table4[Index] = currentindex -1 ) , [color code] ) var currentpath = Table4[path] var path_contains_green = CONTAINSSTRING( currentpath,"green" ) var path_contains_yellow = CONTAINSSTRING(currentpath,"yellow" ) var path_contains_red = CONTAINSSTRING(currentpath,"red" ) var res = SWITCH( TRUE(), currentpath = BLANK() , blank() , currentcolor = prev_color , blank() , currentcolor = "red" && path_contains_red , SWITCH( TRUE(), prev_color ="red" , BLANK() , var prev_date = SELECTCOLUMNS(FILTER(Table4,Table4[Index] = currentindex -1 ) , [Date] ) return INT(currentdate - prev_date) ), currentcolor = "red" && path_contains_green , var ds_green = FILTER( Table4, Table4[Date]<currentdate && Table4[color code] = "green" ) var green_date = SELECTCOLUMNS(INDEX(1,distinct(SELECTCOLUMNS(ds_green ,[Date], [Index])),ORDERBY([Index] , DESC ) ) ,[Date]) return INT( currentdate - green_date ) , currentcolor = "red" && path_contains_yellow , var ds_yellow = FILTER( Table4, Table4[Date]<currentdate && Table4[color code] = "yellow" ) var yellow_date = SELECTCOLUMNS(INDEX(1,distinct(SELECTCOLUMNS(ds_yellow ,[Date], [Index])),ORDERBY([Index] , DESC ) ) ,[Date]) return INT( currentdate - yellow_date ) , currentcolor = "yellow" && path_contains_red , blank(), currentcolor = "yellow" && path_contains_green , var ds_green = FILTER( Table4, Table4[Date]<currentdate && Table4[color code] = "green" ) var green_date = SELECTCOLUMNS(INDEX(1,distinct(SELECTCOLUMNS(ds_green ,[Date], [Index])),ORDERBY([Index] , DESC ) ) ,[Date]) return INT( currentdate - green_date ) ) return reslet me know if this works.
Daniel29195
2 years agoCommunity Champion
result :
in order to achieve this, you need 2 columns helperr : index and path.
in power query create the undex using : addcolumns --> add index -- . start index from 1 .
in power bi, create path column as follow :
path =
var latest_green =
SELECTCOLUMNS(
INDEX(
1,
DISTINCT(
SELECTCOLUMNS(
FILTER(
Table4 , Table4[Date] <= EARLIER( Table4[Date]) && Table4[color code] = "green"
),
[Date], [Index]
)
)
,ORDERBY([Index] , DESC )
),
[Date]
)
var ds =
FILTER(
Table4 , Table4[Date] < EARLIER( Table4[Date]) && Table4[Date] >=latest_green
)
var ds_colors = SELECTCOLUMNS(ds , [color code] )
RETURN
CONCATENATEX(ds_colors , [color code] , "|")
then create the column 2 as follow :
Column 2 =
var currentcolor = Table4[color code]
var currentdate = Table4[Date]
var currentindex = Table4[Index]
var prev_color = SELECTCOLUMNS(FILTER(Table4,Table4[Index] = currentindex -1 ) , [color code] )
var currentpath = Table4[path]
var path_contains_green = CONTAINSSTRING( currentpath,"green" )
var path_contains_yellow = CONTAINSSTRING(currentpath,"yellow" )
var path_contains_red = CONTAINSSTRING(currentpath,"red" )
var res =
SWITCH(
TRUE(),
currentpath = BLANK() , blank() ,
currentcolor = prev_color , blank() ,
currentcolor = "red" && path_contains_red ,
SWITCH(
TRUE(),
prev_color ="red" , BLANK() ,
var prev_date = SELECTCOLUMNS(FILTER(Table4,Table4[Index] = currentindex -1 ) , [Date] )
return INT(currentdate - prev_date)
),
currentcolor = "red" && path_contains_green ,
var ds_green =
FILTER(
Table4,
Table4[Date]<currentdate && Table4[color code] = "green"
)
var green_date = SELECTCOLUMNS(INDEX(1,distinct(SELECTCOLUMNS(ds_green ,[Date], [Index])),ORDERBY([Index] , DESC ) ) ,[Date])
return INT( currentdate - green_date ) ,
currentcolor = "red" && path_contains_yellow ,
var ds_yellow =
FILTER(
Table4,
Table4[Date]<currentdate && Table4[color code] = "yellow"
)
var yellow_date = SELECTCOLUMNS(INDEX(1,distinct(SELECTCOLUMNS(ds_yellow ,[Date], [Index])),ORDERBY([Index] , DESC ) ) ,[Date])
return INT( currentdate - yellow_date ) ,
currentcolor = "yellow" && path_contains_red , blank(),
currentcolor = "yellow" && path_contains_green ,
var ds_green =
FILTER(
Table4,
Table4[Date]<currentdate && Table4[color code] = "green"
)
var green_date = SELECTCOLUMNS(INDEX(1,distinct(SELECTCOLUMNS(ds_green ,[Date], [Index])),ORDERBY([Index] , DESC ) ) ,[Date])
return INT( currentdate - green_date )
)
return res
let me know if this works.