Forum Discussion

yamina's avatar
yamina
Icon for Helper I rankHelper I
1 year ago
Solved

Help dax while adding column

Hi 

 

I need to duplicate these two excel formulas in power bi deskstop by adding two news colums in my table 

Can you help Me ? 

 

Date_creation_DACRIN_dossier_NUDOSN_prestation_RGOUVMin dateCondition retour 
10/04/202420030385=SI(ET(I1=I2; I2<>""); MIN(SI(I:I=I2; A:A)); "")=SIERREUR(SI(ET(A2 - L2 >= 0; A2 - L2 <= 30; J2 >= J1 + 1); "Retour dans les 30 jours"; "");"")
13/04/202470235251  
10/04/202471903631  
13/04/202472555223  
09/04/202482541481  
08/04/202483333471  
10/04/202492266552  
09/04/202494276913  
11/04/2024101433082  
11/04/2024101433094  
13/04/20241218948811  
13/04/2024121894881113/04/2024 
13/04/2024121894881113/04/2024 
13/04/2024121894881113/04/2024 
  • Anonymous's avatar
    Anonymous
    1 year ago

    Hi yamina ,

     

    Please try following Dax formulars.

    Min date = 
    VAR CurrentIndex = 'Table'[Index]
    VAR PreviousDossier = MAXX(FILTER('Table', 'Table'[Index] = CurrentIndex -1), 'Table'[N_dossier_NUDOS])
    VAR CurrentDossier = 'Table'[N_dossier_NUDOS]
    RETURN
    IF(
        PreviousDossier = CurrentDossier && CurrentDossier <> BLANK(),
        MINX(FILTER('Table', 'Table'[N_dossier_NUDOS] = CurrentDossier), 'Table'[Date_creation_DACRI]),
        BLANK()
    )
    Condition retour = 
    VAR CurrentDossier = 'Table'[N_dossier_NUDOS]
    VAR CurrentIndex = 'Table'[Index]
    VAR FirstDossierIndex = MINX(FILTER('Table', 'Table'[N_dossier_NUDOS] = CurrentDossier),'Table'[Index])
    VAR FirstDossierCreationDate = CALCULATE(SELECTEDVALUE('Table'[Date_creation_DACRI]), 'Table'[Index] = FirstDossierIndex , ALL())
    VAR CurrentPresentation = 'Table'[N_prestation_RGOUV]
    VAR PreviousPresentation =  MAXX(FILTER('Table', 'Table'[Index] = CurrentIndex -1), 'Table'[N_prestation_RGOUV])
    RETURN
    
    IFERROR(
        IF( 'Table'[Date_creation_DACRI] - FirstDossierCreationDate >= 0 && 'Table'[Date_creation_DACRI] - FirstDossierCreationDate <= 30 && CurrentPresentation >= PreviousPresentation +1,
            "Retour dans les 30 jours",
            BLANK()
        ),
        BLANK()
    )

     

    The final result is as follows. Hopefully it will meet your needs.

     

    Please see the attached pbix for reference.

     

    Best Regards,
    Dengliang Li

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

8 Replies

  • Hi yamina  - you can create two calculated columns using DAX.

     

    Min date =
    VAR CurrentDossier = 'YourTable'[N_dossier_NUDOS]
    RETURN
    IF(
    COUNTROWS(FILTER('YourTable', 'YourTable'[N_dossier_NUDOS] = CurrentDossier)) > 1,
    CALCULATE(MIN('YourTable'[Date_creation_DACRI]),
    ALLEXCEPT('YourTable', 'YourTable'[N_dossier_NUDOS])
    ),
    BLANK()
    )

     

    This column checks if the difference between Date_creation_DACRI and Min date is between 0 and 30, and if N_prestation_RGOUV has increased.

     

    Condition retour =
    VAR MinDate = 'YourTable'[Min date]
    VAR CurrentDate = 'YourTable'[Date_creation_DACRI]
    VAR CurrentDossier = 'YourTable'[N_dossier_NUDOS]
    VAR CurrentPrestation = 'YourTable'[N_prestation_RGOUV]

    VAR PreviousPrestation =
    CALCULATE(
    MAX('YourTable'[N_prestation_RGOUV]),
    FILTER(
    'YourTable',
    'YourTable'[N_dossier_NUDOS] = CurrentDossier &&
    'YourTable'[Date_creation_DACRI] < CurrentDate
    )
    )

    RETURN
    IF(
    NOT ISBLANK(MinDate) &&
    CurrentDate - MinDate >= 0 &&
    CurrentDate - MinDate <= 30 &&
    CurrentPrestation >= PreviousPrestation + 1,
    "Retour dans les 30 jours",
    BLANK()
    )

     

    replace with your table name as per the model. hope this helps.

    • yamina's avatar
      yamina
      Icon for Helper I rankHelper I

      hi rajendraongole1 

      See the error message when I try your solution. The Min date is ok but the condition retour returns this error  " Les opérations DAX de comparaison ne prennent pas en charge la comparaison des valeurs de type Text avec des valeurs de type Number. Utilisez la fonction VALUE ou FORMAT pour convertir une des valeurs." because the N_dossier is in text format. 

      • rajendraongole1's avatar
        rajendraongole1
        Icon for Super User rankSuper User

        Hi yamina - can you try the modified one.

        Condition retour =
        VAR MinDate = 'YourTable'[Min date]
        VAR CurrentDate = 'YourTable'[Date_creation_DACRI]
        VAR CurrentDossier = 'YourTable'[N_dossier_NUDOS] -- Ensure it's text
        VAR CurrentPrestation = 'YourTable'[N_prestation_RGOUV]

        VAR PreviousPrestation =
        CALCULATE(
        MAX('YourTable'[N_prestation_RGOUV]),
        FILTER(
        'YourTable',
        FORMAT('YourTable'[N_dossier_NUDOS], "@") = FORMAT(CurrentDossier, "@") && -- Ensure text comparison
        'YourTable'[Date_creation_DACRI] < CurrentDate
        )
        )

        RETURN
        IF(
        NOT ISBLANK(MinDate) &&
        CurrentDate - MinDate BETWEEN 0 AND 30 &&
        NOT ISBLANK(PreviousPrestation) &&
        CurrentPrestation > PreviousPrestation,
        "Retour dans les 30 jours",
        BLANK()
        )

         

        here, FORMAT(N_dossier_NUDOS, "@") to force text comparison.Ensured PreviousPrestation isn't blank before checking the condition.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Thanks for the reply from rajendraongole1, please allow me to provide another insight.

    Hi yamina ,

     

    Please refers to the following steps.

    Add an index column to the table in the Power Query editor.

     

    Use the following DAX formulas to create the 'Min date' column and the 'Condition retour' column.

     

     

    Min date = 
    VAR CurrentIndex = [Index]
    VAR PreviousDossier = MAXX(FILTER('Table', [Index] = CurrentIndex -1),[N_dossier_NUDOS])
    VAR CurrentDossier = 'Table'[N_dossier_NUDOS]
    RETURN
    IF(
        PreviousDossier = CurrentDossier && CurrentDossier <> BLANK(),
        MINX(FILTER('Table', [N_dossier_NUDOS] = CurrentDossier), [Date_creation_DACRI]),
        BLANK()
    )

     

    Condition retour  = 
    VAR CurrentIndex ='Table'[Index]
    VAR CurrentPresentation = 'Table'[N_prestation_RGOUV]
    VAR PreviousPresentation =  MAXX(FILTER('Table', [Index] = CurrentIndex -1), [N_prestation_RGOUV])
    RETURN
    IFERROR(
        IF( [Date_creation_DACRI] - [Min date] >=0  && [Date_creation_DACRI] - [Min date] <= 30 && CurrentPresentation >= PreviousPresentation +1,
            "Retour dans les 30 jours",
            BLANK()
        ),
        BLANK()
    )

     

    The final result is as follows. Hopefully it will meet your needs.

     

    Please see the attahced pbix for reference.

     

    Best Regards,
    Dengliang Li

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

  • Sorry , 

    I reformulate my request with more lines in the file. I would like to indicate for the same file number the return condition if the Presta number increases by at least 1 and if the creation date is within 30 days of the date of the 1st file

    Date_creation_DACRIN_dossier_NUDOSN_prestation_RGOUVMin dateCondition retour 
    10/04/202420030385=SI(ET(I1=I2; I2<>""); MIN(SI(I:I=I2; A:A)); "")=SIERREUR(SI(ET(A2 - L2 >= 0; A2 - L2 <= 30; J2 >= J1 + 1); "Retour dans les 30 jours"; "");"")
    13/04/202429803685213/04/2024Retour dans les 30 jours
    13/04/202429803685313/04/2024Retour dans les 30 jours
    14/04/202429803685413/04/2024Retour dans les 30 jours
    14/04/202429803685513/04/2024Retour dans les 30 jours
    14/04/202429803685613/04/2024Retour dans les 30 jours
    14/04/202429803685713/04/2024Retour dans les 30 jours
    14/04/202429803685813/04/2024Retour dans les 30 jours
    14/04/202429803685913/04/2024Retour dans les 30 jours
    14/04/2024298036851013/04/2024Retour dans les 30 jours
    14/04/2024298036851113/04/2024Retour dans les 30 jours
    11/04/202437670929311/04/2024Retour dans les 30 jours
    12/04/2024382449741208/04/2024Retour dans les 30 jours
    12/04/202447918139312/04/2024Retour dans les 30 jours
    11/04/2024489926881411/04/2024Retour dans les 30 jours
    12/04/2024489926881511/04/2024Retour dans les 30 jours
    12/04/2024489926881611/04/2024Retour dans les 30 jours
    12/04/2024489926881811/04/2024Retour dans les 30 jours
    11/04/2024513192791110/04/2024Retour dans les 30 jours
    10/04/202451477461609/04/2024Retour dans les 30 jours
    13/04/202451829240410/04/2024Retour dans les 30 jours
    11/04/202453353783409/04/2024Retour dans les 30 jours
    13/04/202453353783509/04/2024Retour dans les 30 jours
    13/04/202454456236509/04/2024Retour dans les 30 jours
    08/04/202454846145408/04/2024Retour dans les 30 jours
    13/04/202454909754509/04/2024Retour dans les 30 jours
    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi yamina ,

       

      Please try following Dax formulars.

      Min date = 
      VAR CurrentIndex = 'Table'[Index]
      VAR PreviousDossier = MAXX(FILTER('Table', 'Table'[Index] = CurrentIndex -1), 'Table'[N_dossier_NUDOS])
      VAR CurrentDossier = 'Table'[N_dossier_NUDOS]
      RETURN
      IF(
          PreviousDossier = CurrentDossier && CurrentDossier <> BLANK(),
          MINX(FILTER('Table', 'Table'[N_dossier_NUDOS] = CurrentDossier), 'Table'[Date_creation_DACRI]),
          BLANK()
      )
      Condition retour = 
      VAR CurrentDossier = 'Table'[N_dossier_NUDOS]
      VAR CurrentIndex = 'Table'[Index]
      VAR FirstDossierIndex = MINX(FILTER('Table', 'Table'[N_dossier_NUDOS] = CurrentDossier),'Table'[Index])
      VAR FirstDossierCreationDate = CALCULATE(SELECTEDVALUE('Table'[Date_creation_DACRI]), 'Table'[Index] = FirstDossierIndex , ALL())
      VAR CurrentPresentation = 'Table'[N_prestation_RGOUV]
      VAR PreviousPresentation =  MAXX(FILTER('Table', 'Table'[Index] = CurrentIndex -1), 'Table'[N_prestation_RGOUV])
      RETURN
      
      IFERROR(
          IF( 'Table'[Date_creation_DACRI] - FirstDossierCreationDate >= 0 && 'Table'[Date_creation_DACRI] - FirstDossierCreationDate <= 30 && CurrentPresentation >= PreviousPresentation +1,
              "Retour dans les 30 jours",
              BLANK()
          ),
          BLANK()
      )

       

      The final result is as follows. Hopefully it will meet your needs.

       

      Please see the attached pbix for reference.

       

      Best Regards,
      Dengliang Li

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

      • yamina's avatar
        yamina
        Icon for Helper I rankHelper I

        Hello 

        As i understand I need to add an index column, but it takes a very long time because my table has 2 millions lines. so it is impossible. In addition I have to sort before the columns N_dossier_ num . and N_prestation_ RGOUV by asc. It is very heavy because my report use summarize  table from an another semantic model. so I don't know how to simplify this.