Reputation: 37
I am creating a dashboard to compare the performance of some loads we are executing over a database. The dashboard gets the data on the date of the load, the entity to load, the type of load, the number of records and the number of rejections. In the dashboard you are allowed to select the day of the load to examine, the entity and the type of the load. To be able to compare, I created to KPIs: the first one comparing the number of rejections of the first load against the number of rejections of the selected load (the day selected) and the second one comparing the number of rejections on the selected load against the last one.
Both the number of loads of the first and the last load are created by measures. First, calculating the first and the last load:
Primera Carga = CALCULATE(MIN(Rechazos[CARGA]),FILTER(ALL(Rechazos),Rechazos[TIPO_CARGA]==SELECTEDVALUE(Rechazos[TIPO_CARGA]) && Rechazos[TIPO_CONTADOR]==SELECTEDVALUE(Rechazos[TIPO_CONTADOR])))
Última Carga = CALCULATE(MAX(Rechazos[CARGA]),FILTER(ALL(Rechazos),Rechazos[TIPO_CARGA]==SELECTEDVALUE(Rechazos[TIPO_CARGA]) && Rechazos[TIPO_CONTADOR] == SELECTEDVALUE(Rechazos[TIPO_CONTADOR])))
'Primera Carga' stands for first load and 'Última Carga' stands for the last load. Both are codes related with the day of the execution and codified as "yyyymmdd".
Once defined the loads, the number of rejections in both...
Nº Rechazos =
var valor_codigo_venta = SELECTEDVALUE(CODIGO_VENTA[CODIGO_VENTA])
RETURN (
IF(ISBLANK(valor_codigo_venta),
IF(ISBLANK(SUMX(FILTER(all(Rechazos),Rechazos[CARGA]==SELECTEDVALUE(Rechazos[CARGA])
&& Rechazos[TIPO_CONTADOR]==SELECTEDVALUE(Tipo_Contador[TIPO_CONTADOR])
&& Rechazos[TIPO_CARGA]==SELECTEDVALUE(TIPO_CARGA[TIPO_CARGA])
),Rechazos[NUM_RECHAZOS])),
0,
SUMX(FILTER(all(Rechazos),Rechazos[CARGA]==SELECTEDVALUE(Rechazos[CARGA])
&& Rechazos[TIPO_CONTADOR]==SELECTEDVALUE(Tipo_Contador[TIPO_CONTADOR])
&& Rechazos[TIPO_CARGA]==SELECTEDVALUE(TIPO_CARGA[TIPO_CARGA])
),Rechazos[NUM_RECHAZOS]))
,IF(ISBLANK(SUMX(FILTER(all(Rechazos),Rechazos[CARGA]==SELECTEDVALUE(Rechazos[CARGA])
&& Rechazos[TIPO_CONTADOR]==SELECTEDVALUE(Tipo_Contador[TIPO_CONTADOR])
&& Rechazos[TIPO_CARGA]==SELECTEDVALUE(TIPO_CARGA[TIPO_CARGA])
&& Rechazos[CODIGO_VENTA]==valor_codigo_venta
),Rechazos[NUM_RECHAZOS])),0,SUMX(FILTER(all(Rechazos),Rechazos[CARGA]==SELECTEDVALUE(Rechazos[CARGA])
&& Rechazos[TIPO_CONTADOR]==SELECTEDVALUE(Tipo_Contador[TIPO_CONTADOR])
&& Rechazos[TIPO_CARGA]==SELECTEDVALUE(TIPO_CARGA[TIPO_CARGA])
&& Rechazos[CODIGO_VENTA]==valor_codigo_venta
),Rechazos[NUM_RECHAZOS]))
)
)
First I calculate a measure giving me the number of rejections according the selections. Later, for the last and the first load...
Nº Rechazos Primera =
var valor_codigo_venta = SELECTEDVALUE(CODIGO_VENTA[CODIGO_VENTA])
RETURN(
IF(ISBLANK(valor_codigo_venta),
CALCULATE([Nº Rechazos],
FILTER(all(Rechazos),
Rechazos[TIPO_CONTADOR]==SELECTEDVALUE(Tipo_Contador[TIPO_CONTADOR])
&& Rechazos[TIPO_CARGA]==SELECTEDVALUE(TIPO_CARGA[TIPO_CARGA])
&& Rechazos[CARGA]==**[Primera Carga]**
)
),
CALCULATE([Nº Rechazos],
FILTER(all(Rechazos),
Rechazos[TIPO_CONTADOR]==SELECTEDVALUE(Tipo_Contador[TIPO_CONTADOR])
&& Rechazos[TIPO_CARGA]==SELECTEDVALUE(TIPO_CARGA[TIPO_CARGA])
&& Rechazos[CARGA]==**[Primera Carga]**
&& Rechazos[CODIGO_VENTA]==SELECTEDVALUE(Rechazos[CODIGO_VENTA])
)
)
)
)
Nº Rechazos Última =
var valor_codigo_venta = SELECTEDVALUE(CODIGO_VENTA[CODIGO_VENTA])
RETURN(
IF(ISBLANK(valor_codigo_venta),
CALCULATE([Nº Rechazos],
FILTER(all(Rechazos), Rechazos[TIPO_CONTADOR]==SELECTEDVALUE(Tipo_Contador[TIPO_CONTADOR])
&& Rechazos[TIPO_CARGA]==SELECTEDVALUE(TIPO_CARGA[TIPO_CARGA])
&& Rechazos[CARGA]==**[Última Carga]**
)
),
CALCULATE([Nº Rechazos],
FILTER(all(Rechazos), Rechazos[TIPO_CONTADOR]==SELECTEDVALUE(Tipo_Contador[TIPO_CONTADOR])
&& Rechazos[TIPO_CARGA]==SELECTEDVALUE(TIPO_CARGA[TIPO_CARGA])
&& Rechazos[CARGA]==**[Última Carga]**
&& Rechazos[CODIGO_VENTA]==SELECTEDVALUE(Rechazos[CODIGO_VENTA])
)
)
)
)
Now I would like to compare the selected load against the previous. To do so, I created a measure giving me the previous load as well:
Anterior Carga = CALCULATE(MAX(Rechazos[CARGA])
,FILTER(ALL(Rechazos)
,Rechazos[TIPO_CARGA]==SELECTEDVALUE(Rechazos[TIPO_CARGA])
&& Rechazos[TIPO_CONTADOR] == SELECTEDVALUE(Rechazos[TIPO_CONTADOR])
&& **Rechazos[FECHA_CARGA]<SELECTEDVALUE(Rechazos[FECHA_CARGA])**
)
)
So, with this calculation it would be so simple as to change the filter in one of the previous measures to obtain my result,...
Nº Rechazos Anterior =
var valor_codigo_venta = SELECTEDVALUE(CODIGO_VENTA[CODIGO_VENTA])
RETURN(
IF(ISBLANK(valor_codigo_venta),
CALCULATE([Nº Rechazos],
FILTER(all(Rechazos), Rechazos[TIPO_CONTADOR]==SELECTEDVALUE(Tipo_Contador[TIPO_CONTADOR])
&& Rechazos[TIPO_CARGA]==SELECTEDVALUE(TIPO_CARGA[TIPO_CARGA])
&& Rechazos[CARGA]==**[Anterior Carga]**
)
),
CALCULATE([Nº Rechazos],
FILTER(all(Rechazos), Rechazos[TIPO_CONTADOR]==SELECTEDVALUE(Tipo_Contador[TIPO_CONTADOR])
&& Rechazos[TIPO_CARGA]==SELECTEDVALUE(TIPO_CARGA[TIPO_CARGA])
&& Rechazos[CARGA]==**[Anterior Carga]**
&& Rechazos[CODIGO_VENTA]==valor_codigo_venta
)
)
)
)
As you can see, the only difference is the bold code. But it gives me always 0. If I introduce a hardcoded code (as 20200917, for example), it works giving me the results. So I suspect about the measure "Anterior Carga", concretely in the bolded code. But I can not find the answer.
Can someone see where is the problem on this code?
Thanks in advance.
Upvotes: 0
Views: 21
Reputation: 5542
I don't get why you are doing ALL(Rechazos) and then applying the filters again, but probably your error can be fixed by saving the last load into a variable:
Nº Rechazos Anterior =
var valor_codigo_venta = SELECTEDVALUE(CODIGO_VENTA[CODIGO_VENTA])
var anterior_carga = [Anterior Carga]
RETURN(
IF(ISBLANK(valor_codigo_venta),
CALCULATE([Nº Rechazos],
FILTER(all(Rechazos), Rechazos[TIPO_CONTADOR]==SELECTEDVALUE(Tipo_Contador[TIPO_CONTADOR])
&& Rechazos[TIPO_CARGA]==SELECTEDVALUE(TIPO_CARGA[TIPO_CARGA])
&& Rechazos[CARGA]== anterior_carga
)
),
CALCULATE([Nº Rechazos],
FILTER(all(Rechazos), Rechazos[TIPO_CONTADOR]==SELECTEDVALUE(Tipo_Contador[TIPO_CONTADOR])
&& Rechazos[TIPO_CARGA]==SELECTEDVALUE(TIPO_CARGA[TIPO_CARGA])
&& Rechazos[CARGA]== anterior_carga
&& Rechazos[CODIGO_VENTA]==valor_codigo_venta
)
)
)
)
Upvotes: 1