Forum Discussion

itsranga's avatar
itsranga
Helper I
1 year ago
Solved

Need Help - DAX Code

Hi Team,  I have a Data like this (Top table), I need result like bottom table

 

  • If the Parent Ticket is not blank, the Relevant hrs should be blank.
  • If the Parent Ticket is blank, the result should be the sum of hours for all rows that the same Parent Ticket.

    how to achieve this using DAX
  • This is very simple but in your original data it was not like this, here is the table you showed

     

     

    Anyway I adjusted the data and now I have the following

     

     

    and now I have what you want without any hardwiring:

     

    Test =
    VAR Ticket = Tabella[Ticket No]
    RETURN
    SUMX ( FILTER ( Tabella, Tabella[Parent Ticket] = Ticket ), Tabella[Total Hrs] )
     
    The above is a column
     
    If you want a measure, let me know
     

    If this helped, please consider giving kudos and mark as a solution

    me in replies or I'll lose your thread

    Want to check your DAX skills? Answer my biweekly DAX challenges on the kubisco Linkedin page

    Consider voting this Power BI idea

    Francesco Bergamaschi

    MBA, M.Eng, M.Econ, Professor of BI

17 Replies

  • In your picture, in the results table, the first row has empty Parent Ticket, though you want to see 586?

     

    This is in contrast with your definition:

     

    • If the Parent Ticket is blank, the Relevant hrs should be blank.

    Please clarify and resend the result you want

     

    If this helped, please consider giving kudos and mark as a solution

    me in replies or I'll lose your thread

    Want to check your DAX skills? Answer my biweekly DAX challenges on the kubisco Linkedin page

    Consider voting this Power BI idea

    Francesco Bergamaschi

    MBA, M.Eng, M.Econ, Professor of BI

    • itsranga's avatar
      itsranga
      Helper I

      FBergamaschi 

      • If the Parent Ticket is blank, the result should be the sum of hours for all rows that the same Parent Ticket. (screenshot red color highlighted)
      • If the Parent Ticket is not blank, the Relevant hrs should be blank.
      • FBergamaschi's avatar
        FBergamaschi
        Super User

        Ok now it is clearer, please provide the table in a usable format, not an image

         

        And What if there are many different Parent Tickets? Like two differen parent tickets in 5 different lines, 3 with the first parent ticket and the other two with the other one?

         

        If this helped, please consider giving kudos and mark as a solution

        me in replies or I'll lose your thread

        Want to check your DAX skills? Answer my biweekly DAX challenges on the kubisco Linkedin page

        Consider voting this Power BI idea

        Francesco Bergamaschi

        MBA, M.Eng, M.Econ, Professor of BI

  • FBergamaschi 

    Ticket NoSummary Parent TicketTotal Hrs  
    1049039Data Conversion 678.5  
    1323146Table Conversion10493039346  
    1456862Table Conversion_110493039240  
    1140978Hourly Conversion 186.5  
          
          
    I need result like this    
          
    Ticket NoSummary Parent TicketTotal HrsRelevant hrsTotal Hrs
    1049039Data Conversion 678.55861264.5
    1323146Table Conversion10493039346  
    1456862Table Conversion_110493039240  
    1140978Hourly Conversion 186.5  

     

     

    many different parent ticket is not possible, one parent ticket only for one ticket

    • FBergamaschi's avatar
      FBergamaschi
      Super User

       

      calculated column code

       

      Colonna =
      IF ( ISBLANK( Tabella[Parent Ticket] ) && Tabella[Summary ] <> "Hourly Conversion", SUMX ( FILTER ( Tabella, NOT ISBLANK( Tabella[Parent Ticket] ) ), Tabella[Total Hrs] ) )
       

      If this helped, please consider giving kudos and mark as a solution

      me in replies or I'll lose your thread

      Want to check your DAX skills? Answer my biweekly DAX challenges on the kubisco Linkedin page

      Consider voting this Power BI idea

      Francesco Bergamaschi

      MBA, M.Eng, M.Econ, Professor of BI

      • itsranga's avatar
        itsranga
        Helper I

        FBergamaschi 

        Colonna =
        IF ( ISBLANKTabella[Parent Ticket] ) && Tabella[Summary ] <> "Hourly Conversion"SUMX ( FILTER ( TabellaNOT ISBLANKTabella[Parent Ticket] ) ), Tabella[Total Hrs] ) )


        This IF condition is hardcoded for 'Hourly conversion'. I tried to avoid this code and used a DAX expression instead, but it caused a circular dependency error. Please correct the error
  • Hi itsranga please try this measure

     

    Relevant Hrs Measure =
    VAR ThisTicket = SELECTEDVALUE('Sheet6'[Ticket No])
    VAR IsParent = ISBLANK(SELECTEDVALUE('Sheet6'[Parent Ticket]))
    RETURN
    IF (
        IsParent,
        CALCULATE (
            SUM ( 'Sheet6'[Total Hrs] ),
            FILTER (
                ALL('sheet6'),
                'Sheet6'[Parent Ticket] = ThisTicket
                    || 'Sheet6'[Ticket No] = ThisTicket
            )
        )
    )
     

     

    • itsranga's avatar
      itsranga
      Helper I

      techies  thanks for your response,  I need result like this
      its return wrong value

       

      • techies's avatar
        techies
        Super User

        ok, please try this 

         

        Relevant Child Hrs Measure =
        VAR ThisTicket = SELECTEDVALUE('Sheet6'[Ticket No])
        RETURN
        CALCULATE (
            SUM ( 'Sheet6'[Total Hrs] ),
            FILTER (
                ALL('Sheet6'),
                'Sheet6'[Parent Ticket] = ThisTicket
            )
        )
  • v-tejrama's avatar
    v-tejrama
    Community Support

    Hi itsranga ,

     

    Thanks for sharing the detailed process you’ve explained it quite well, and your approach is absolutely correct. Just to confirm, once you load your data into Power BI from Excel or your relevant source, you can go ahead and create the "Relevant Hrs" calculated column in the Data view.
    The DAX expression you've mentioned works fine  it checks if the Parent Ticket is not blank and then uses CALCULATE with ALLEXCEPT to sum the Total Hrs grouped by Parent Ticket. If the Parent Ticket is blank, it rightly returns a blank value. After that, adding the fields like Ticket No, Summary, Parent Ticket, Total Hrs, and the newly created Relevant Hrs into a Table visual in Report view is the right way to go.
    Based on your example, if multiple rows share the same Parent Ticket, the Relevant Hrs should show the correct total, like 586 for the Data Conversion case, while entries without a Parent Ticket (like Hourly Conversion) will show blank, which is expected. You can also apply conditional formatting or adjust the column widths to improve readability. Once everything looks good, saving the report and publishing it to the Power BI Service is the final step.
    Please feel free to text me back if you have any questions
    I’ve attached the .pbix file and screen shorts used in this test for your reference.

     



    Thank you.
    Tejaswi.

     

    • itsranga's avatar
      itsranga
      Helper I

      v-tejrama  thanks for your suppot,   somewhat okay  i slightly changed the formula it's okay but 


      This is a parent-child ticket concept. For example, one parent ticket can have multiple child tickets. I need to display  in a new column, show the sum of all the child ticket hours.
      In this case, ticket number 1043930 is the parent ticket (row 1), and rows 3 and 4 are its child tickets. So, I need to calculate the sum of hours where the ticket number matches the parent ticket, and display that sum in the parent ticket's row

       

       

      • FBergamaschi's avatar
        FBergamaschi
        Super User

        This is very simple but in your original data it was not like this, here is the table you showed

         

         

        Anyway I adjusted the data and now I have the following

         

         

        and now I have what you want without any hardwiring:

         

        Test =
        VAR Ticket = Tabella[Ticket No]
        RETURN
        SUMX ( FILTER ( Tabella, Tabella[Parent Ticket] = Ticket ), Tabella[Total Hrs] )
         
        The above is a column
         
        If you want a measure, let me know
         

        If this helped, please consider giving kudos and mark as a solution

        me in replies or I'll lose your thread

        Want to check your DAX skills? Answer my biweekly DAX challenges on the kubisco Linkedin page

        Consider voting this Power BI idea

        Francesco Bergamaschi

        MBA, M.Eng, M.Econ, Professor of BI