Forum Discussion

Khalefa's avatar
Khalefa
Helper I
3 years ago
Solved

Iteration with multiple column conditions

Hi team

Need support in the below

a measure to calculate the overall attribute score on the review ID granularity considering the below:-
1- There are 6 attributes in each review id
2- The DAX should say that on each review id, if (attribute = compliance, and score =0 or attribute = image and score =0 ) so all the scores of all attributes within that review id must equal 0 regardless of scores of 100
3- If the above condition is False, so if the score =100, sum the weight otherwise give 0
4- The column weight is in another dimension table

 

Sample table

Review IDAttributeScoreWeight
1Validation10010
1Compliance10030
1Image10030
1Knowledge010
1Language010
1Accuracy10010
2Validation10010
2Compliance030
2Image10030
2Knowledge010
2Language10010
2Accuracy010
3Validation010
3Compliance030
3Image030
3Knowledge10010
3Language10010
3Accuracy010
4Validation10010
4Compliance10030
4Image10030
4Knowledge10010
4Language10010
4Accuracy10010

 

Desired result

 

Review IDOverall scoreWeightScore %
18010080%
201000%
301000%
4100100100%
Total18040045%

 

 

  • Hi Khalefa 
    Please refer to attached amended sample file

    Weighted Score = 
    SUMX (
        VALUES ( 'Fact'[Review ID] ),
        IF (
            0
                IN CALCULATETABLE (
                    VALUES ( 'Fact'[Score] ),
                    ALL ( Dim_Attribute[Attribute] ),
                    Dim_Attribute[Attribute] IN { "Compliance", "Image" }
                ),
            0,
            SUMX (
                CALCULATETABLE ( 'Fact' ),
                'Fact'[Score] * RELATED ( Dim_Attribute[Weight] ) / 100
            )
        )
    )
    Total Weight = 
    SUMX (
        VALUES ( 'Fact'[Review ID] ),
        SUMX (
            CALCULATETABLE ( 'Fact' ),
            RELATED ( Dim_Attribute[Weight] ) 
        )
    )
    % Score = 
    DIVIDE (
        [Weighted Score],
        [Total Weight]
    )

28 Replies

  • tamerj1's avatar
    tamerj1
    Community Champion

    Hi Khalefa 
    Please try

    % Score =
    IF (
        0
            IN CALCULATETABLE (
                VALUES ( 'Table'[Score] ),
                'Table'[Attribute] IN { "Compliance", "Image" }
            ),
        0,
        SUMX ( 'Table', 'Table'[Score] * 'Table'[Weight] / 100 ) / 100
    )
  • tamerj1's avatar
    tamerj1
    Community Champion

    Khalefa 
    Please refer to attached sample file with the final solution.

    Weighted Score = 
    SUMX (
        VALUES ( 'Fact'[Review ID] ),
        IF (
            0
                IN CALCULATETABLE (
                    VALUES ( 'Fact'[Score] ),
                    Dim_Attribute[Attribute] IN { "Compliance", "Image" }
                ),
            0,
            SUMX (
                CALCULATETABLE ( 'Fact' ),
                'Fact'[Score] * RELATED ( Dim_Attribute[Weight] ) / 100
            )
        )
    )
    Total Weight = 
    SUMX (
        VALUES ( 'Fact'[Review ID] ),
        SUMX (
            CALCULATETABLE ( 'Fact' ),
            RELATED ( Dim_Attribute[Weight] ) 
        )
    )
    % Score = 
    DIVIDE (
        [Weighted Score],
        [Total Weight]
    )
    • Khalefa's avatar
      Khalefa
      Helper I

      tamerj1 thanks a lot, it works but if you don't mind, i need to add a small part which is:-

      need to keep the condition of ( Compliance and Image ) active even if filtered by the slicer on the category.

      by other means the condition must be applied even if i filtered by category3 ( which doesn't contain either compliance or image )

    • Khalefa's avatar
      Khalefa
      Helper I

      CNENFRNL  thanks for the code yet i would need to understand why did add the overall weighted DAX to another iteration in overall DAX, rather than doing one iteration in one time.
      another part, the weight column is in a dimension table with a unique list of attributes

      • v-yueyunzh-msft's avatar
        v-yueyunzh-msft
        Community Support

        Hi , Khalefa 

        I see that the CNENFRNL  have been realized in his dax. But for your "another part, the weight column is in a dimension table with a unique list of attributes". But what  dimension table 's structure in your side?

        So is there a relationship between your dimension table and your sample data?
        Can you provide your original table structure? For example, provide your dimension table in the form of a table?

         

        Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

         

        Best Regards,

        Aniya Zhang

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

    • Khalefa's avatar
      Khalefa
      Helper I

      the weight column is in a dimension table with a unique list of attributes

  • tamerj1's avatar
    tamerj1
    Community Champion

    Hi Khalefa 
    Please refer to attached sample file with the solution (2 options)

    % Score = 
    AVERAGEX ( 
        VALUES ( Scores[Review ID] ),
        IF (
            0
                IN CALCULATETABLE (
                    VALUES ( Scores[Score] ),
                    Scores[Attribute] IN { "Compliance", "Image" }
                ),
            0,
            SUMX ( CALCULATETABLE ( Scores ), Scores[Score] * RELATED ( Weights[Weight] ) / 100 ) / 100
        )
    )
    % Score2 = 
    AVERAGEX ( 
        VALUES ( Scores[Review ID] ),
        VAR T1 = CALCULATETABLE ( Scores )
        RETURN
        IF (
            ISEMPTY ( 
                FILTER (
                    T1,
                    Scores[Attribute] IN { "Compliance", "Image" }
                        && Scores[Score] = 0
                )
            ),
            SUMX ( T1, Scores[Score] * RELATED ( Weights[Weight] ) / 100 ) / 100,
            0
        )
    )
    • tamerj1's avatar
      tamerj1
      Community Champion

      Hi Khalefa 
      You can upload to any file transfer service like dropbox, onedrive, googledrive etc.. and share the link.

  • i just need to upload the model sample to be clear enough

    • tamerj1's avatar
      tamerj1
      Community Champion

      Hi Khalefa 
      I hope this is what you're looking for. Please refer to attached sample file.

      % Score = 
      AVERAGEX ( 
          VALUES ( 'Fact'[Review ID] ),
          IF (
              0
                  IN CALCULATETABLE (
                      VALUES ( 'Fact'[Score] ),
                      Dim_Attribute[Attribute] IN { "Compliance", "Image" }
                  ),
              0,
              SUMX ( CALCULATETABLE ( 'Fact' ), 'Fact'[Score] * RELATED ( Dim_Attribute[Weight] ) / 100 ) / 100
          )
      )

       

      • Khalefa's avatar
        Khalefa
        Helper I

        tamerj1  can i ask why did you use calculatetable after sumx rather than just sumx
        on the other side, you referred to attribute from dim table rahter than attribute from fact. will this matter ?

        Thanks for your patiance

  • tamerj1 
    the results should be as follow

    total weighted score (145 ) / total weight (400) 

    Sumx output should be divided by total weight not just 100

    Review IDSiteChannelAttributeScoreWeight Weighted score
    1Site1CallValidation10015 15
    1Site1CallCompliance1000 0
    1Site1CallImage1000 0
    1Site1CallKnowledge040 0
    1Site1CallLanguage015 0
    1Site1CallAccuracy10030 30
    2Site1CallValidation10015 0
    2Site1CallCompliance00 0
    2Site1CallImage1000 0
    2Site1CallKnowledge040 0
    2Site1CallLanguage10015 0
    2Site1CallAccuracy030 0
    3Site2ChatValidation015 0
    3Site2ChatCompliance00 0
    3Site2ChatImage00 0
    3Site2ChatKnowledge10040 0
    3Site2ChatLanguage10015 0
    3Site2ChatAccuracy030 0
    4Site2ChatValidation10015 15
    4Site2ChatCompliance1000 0
    4Site2ChatImage1000 0
    4Site2ChatKnowledge10040 40
    4Site2ChatLanguage10015 15
    4Site2ChatAccuracy10030 30
    • tamerj1's avatar
      tamerj1
      Community Champion

      Khalefa 

      145/400 = 36% and this is exactly the result obtained at the total level. Am I missing something?

      • Khalefa's avatar
        Khalefa
        Helper I

        I just want to get the total score from your solution without percentage and another DAX for the total weight. this is what I meant.