Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
Gaspar89
Frequent Visitor

Calcular suma quitando valores duplicados

Buenas tardes,

 

Tengo una consulta a la hora de calcular un dato.

 

Tengo una tabla con número de expedientes(referencias) estos expedientes no son únicos ya que llevan unos códigos de mensaje. Cada código de mensaje es un registro con su expediente.

He creado una columna en la cual me calcula la diferencia en días desde el alta del expediente a la fecha de envío del mensaje (con su código).

Necesitaría calcular la suma de los días totales para 4 códigos expecíficos. (los códigos serían 922,976,227 y 980) si estos códigos se repitieran tendría que coger el primero (sería el valor mínimo en días desde el alta. (por ejemplo puedo tener dos registros con el código 922 uno con 9 días y otro con 18 debería tener en cuenta solo el de los 9 días.

 

La finalidad es calcular una media de cuantos días se tarda desde el alta del expediente hasta la emisión de uno de esos códigos.

 

 

17 REPLIES 17
AlB
Super User
Super User

@Gaspar89 

See it all at work in the attached file.

 

Días3 V2 = 
VAR relevant_ = { "227", "922", "976", "980" }
VAR auxT_ =
    ADDCOLUMNS (
        SUMMARIZE (
            FILTER ( TABLE3, TABLE3[CO_TMENSAJE] IN relevant_ ),
            TABLE3[CO_TMENSAJE],
            [CO_DIALOGO_LES]
        ),
        "@Res", CALCULATE ( MIN ( TABLE3[Dias LIQ tramitacion] ) )
    )
RETURN
    SUMX ( auxT_, [@Res] )

 

 

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

Gaspar89
Frequent Visitor

@AlBNo me sigue saliendo el resultado esperado..

 

Me está sumando todos los "Dias LIQ tramitacion" que contienen los CO_TMENSAJE "922, 976, 227 y 980.

 

Cada CO_DIALOGO_LES puede tener alguno o varios de esos Co_tmensaje hay seleccionar el menor valor tenga en dias liq tramitación de cualquiera de esos cuatro mensajes por cada expediente.

 

La lógica es calcular el menor valor para cada expediente de cada uno de esos mensajes y sumarlo. En total tendría que sumar 7720 registros que deben ser el menor de cualquiera de esos cuato co_tmensaje.

 

El resultado esperado son 288859

AlB
Super User
Super User

@Gaspar89 

I am using the same measures as before and they are still working on this data. Not sure what you did earlier, probably did not copy/paste correctly.  See it all at work in the attached file, Page 2. Note the new table is Table2. Table1 is what we had from the previous go.

I am copying the new measures here again but, like just mentioned, do note the only thing that changes is that they operate in Table2 

It's 6 "expedientes", not 7.

 

Días2 = 
VAR relevant_ = { 227, 922, 976, 980 }
VAR auxT_ =
    ADDCOLUMNS (
        SUMMARIZE (
            FILTER ( Table2, Table2[CO_TMENSAJE] IN relevant_ ),
            [CO_DIALOGO_LES]
        ),
        "@Res", CALCULATE ( MIN ( Table2[Dias LIQ tramitacion] ) )
    )
RETURN
    SUMX ( auxT_, [@Res] )

 

 

 

Total expt2 = 
VAR relevant_ = { 227, 922, 976, 980 }
VAR auxT_ =
    ADDCOLUMNS (
        SUMMARIZE (
            FILTER ( Table2, Table2[CO_TMENSAJE] IN relevant_ ),
            [CO_DIALOGO_LES]
        ),
        "@Res", CALCULATE ( MIN ( Table2[Dias LIQ tramitacion] ) )
    )
RETURN
    COUNTX ( auxT_, [@Res] )

 

 

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

Gaspar89
Frequent Visitor

@AlB @v-yangliu-msft Adjunto el fichero con la tabla y todos los datos junto con tu código. Como puedes ver el resultado es -23 cuando el resultado esperado sería 288859

 

La función me coje solo los días negativos y al resto de registros los marca como 0.

 

¿Puedes ayudarme?

 

https://drive.google.com/file/d/1JC6kvNRW7NjXIZdUp5fxT8jGQBHW2vOF/view?usp=share_link   

AlB
Super User
Super User

@Gaspar89 

Link does not work

I haven't understood what the problem is with the previous version of the code. You can either explain it better or adapt the code provided yourself

 

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

Gaspar89
Frequent Visitor

Pongo de nuevo el ejemplo solicitando vuestra ayuda.

Solo necesitaría calcular la suma de la columna dias liq tramitación teniendo en cuenta lo siguiente:

 

El valor tiene que ser el menor de entre dialogo_les y las comunicaciones 922,976,227 y 980.

 

solo se debe cojer el valor inferior para cada dialogo_les de una de esos 4 tmensajes.

 

Resultado experado serían 7 expedientes_les y 261 días.

 

CO_COMUNICACION CO_EXP CO_TMENSAJE CO_DIALOGO_LES FEC_ALTA_LES Dias LIQ tramitacion
1362 202180000025 980 202180000026 19/01/2021 42
394 202180000029 980 202180000030 19/01/2021 21
4983 202180000033 227 202180000034 20/01/2021 90
3762 202180000033 980 202180000034 20/01/2021 79
1063 202180000033 980 202180000034 20/01/2021 35
2504 202180000035 980 202180000036 20/01/2021 63
3000 202180000038 980 202180000039 20/01/2021 68
14403 202180000038 977 202180000039 20/01/2021 147
11120 202180000038 201 202180000039 20/01/2021 128
1272 202180000047 922 202180000048 22/01/2021 38
1014 202180000047 201 202180000048 22/01/2021 32

v-yangliu-msft
Community Support
Community Support

Hi  @Gaspar89 ,

Here are the steps you can follow:

1. Create calculated column.

Count =
var _1=
COUNTX(FILTER(ALL('CO_TMENSAJE922-976-227-980'),
'CO_TMENSAJE922-976-227-980'[CO_DIALOGO_LES]=EARLIER('CO_TMENSAJE922-976-227-980'[CO_DIALOGO_LES])),[Dias LIQ tramitacion])
var _dias=
MINX(FILTER(ALL('CO_TMENSAJE922-976-227-980'),'CO_TMENSAJE922-976-227-980'[CO_DIALOGO_LES]=EARLIER('CO_TMENSAJE922-976-227-980'[CO_DIALOGO_LES])),[Dias LIQ tramitacion])
return
IF(
    _1=1,_1,
    IF(
    _1=2&&'CO_TMENSAJE922-976-227-980'[Dias LIQ tramitacion]=_dias,1,_1)
)

2. Enter data.

vyangliumsft_0-1672023837455.png

Create calculated column:

Result =
var _tabel1=FILTER('CO_TMENSAJE922-976-227-980','CO_TMENSAJE922-976-227-980'[CO_TMENSAJE] in {922,976,227,980} && [Count]=1)
return
SWITCH(
    TRUE(),
'Flag_Table'[Flag]="dias",SUMX(_tabel1,[Dias LIQ tramitacion]),
'Flag_Table'[Flag]="total expt",COUNTX(_tabel1,[Dias LIQ tramitacion]),
'Flag_Table'[Flag]="resultado",AVERAGEX(_tabel1,[Dias LIQ tramitacion])
)

3. Result:

vyangliumsft_1-1672023837456.png

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

No me vale la solución ya que cada expediente tiene varios códigos Tmensaje, en la tabla solo puse una muestra.

AlB
Super User
Super User

@Gaspar89 

And similarly for [Total expt]:

 

Total expt = 
VAR relevant_ = { 227, 922, 976, 980 }
VAR auxT_ =
    ADDCOLUMNS (
        SUMMARIZE (
            FILTER ( Table1, Table1[CO_TMENSAJE] IN relevant_ ),
            [CO_DIALOGO_LES]
        ),
        "@Res", CALCULATE ( MIN ( Table1[Dias LIQ tramitacion] ) )
    )
RETURN
    COUNTX ( auxT_, [@Res] )

 

Then [Resultado] uses the two previous measures

 

Resultado = 
DIVIDE([Días], [Total expt])

 

See it all at work in the attached file

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

 

Gaspar89
Frequent Visitor

Al trasladar la fórmula dax a mi modelo me dan los días negativos. El campo Tmensaje lo tengo definido como si texo. No se si es ahí el error.

AlB
Super User
Super User

@Gaspar89 

Try this for [Días]

Note we are hard-coding the relevant values for Table1[CO_TMENSAJE]. Another option, potentially better depending on your requirements, would be to set these in a slicer 

 

Días = 
VAR relevant_ = { 227, 922, 976, 980 }
VAR auxT_ =
    ADDCOLUMNS (
        SUMMARIZE (
            FILTER ( Table1, Table1[CO_TMENSAJE] IN relevant_ ),
            [CO_DIALOGO_LES]
        ),
        "@Res", CALCULATE ( MIN ( Table1[Dias LIQ tramitacion] ) )
    )
RETURN
    SUMX ( auxT_, [@Res] )

 

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

Gaspar89
Frequent Visitor

No me da el resultado esperado..

@Gaspar89 

The file attached in my previous post yields exactly the expected result that you showed above.

What is the difference?

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

 

AlB
Super User
Super User

@Gaspar89 

Thanks. That helps.

If you can paste the table here in text-tabular format instead of on a screen capture, I can build it for you

 

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

Gaspar89
Frequent Visitor

CO_DIALOGO_LES FEC_ALTA_LES CO_TMENSAJE FE_ENVIO Dias LIQ tramitacion
202280014504 13/10/2022 202 23/10/2022 10
202280014550 17/10/2022 922 27/10/2022 10
202280014684 20/10/2022 976 30/10/2022 10
202280014634 19/10/2022 227 07/11/2022 19
202280014684 20/10/2022 922 07/11/2022 18
202280014550 21/11/2022 922 15/12/2022 24
202280015572 21/11/2022 922 15/12/2022 24
202280015824 30/11/2022 922 10/12/2022 10
202280015866 01/12/2022 227 11/12/2022 10
202280015955 05/12/2022 922 17/12/2022 12
202180002110 21/06/2021 227 15/07/2021 24
202180002478 12/07/2021 202 22/07/2021 10
202180002875 02/08/2021 202 12/08/2021 10
202180003346 20/08/2021 202 30/08/2021 10
202180003347 20/08/2021 922 15/09/2021 26
202180003348 20/08/2021 202 30/08/2021 10

Gaspar89
Frequent Visitor

Gaspar89_2-1671825766897.png

 

Hay que calcular la media de los días con Las comunicaciones de la tabla CO_TMENSAJE922, 976, 227 y 980. y si CO_DIALOGO_LES está duplicado con esos mensajes cojer el que menor LIQ_dias tramitación

 

AlB
Super User
Super User

@Gaspar89 

Can you show some sample data and the expected result?

 

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors