Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Use parameters to create conditional columns

Hi all, new to powerBI. Can you please help me here?

 

I have two columns

 

Student Marks

A          50

B         60

C        70

 

I need to assign grade based on marks.  Say if marks > 65 then A else B. this 65 needs to be dynamic and come from user input

 

Then I need to have a summary table

 

Grade   total students

A            1

B           2

 

How do i go about doing this ? any help will be appreciated

 

 

Thanks

 

  • hi Anonymous 

    You could try this as below:

    Step1:

    Use What if parameter to create a dynamic value for "65"

    https://docs.microsoft.com/en-us/power-bi/desktop-what-if

    Step2:

    Create a simple Grades table that only contains Grade column

    Step3:

    Then use adjust the formula that  Ashish_Mathur provided.

    Measure = VAR _table =
         ADDCOLUMNS (
            Grades,
            "Lower", IF ( Grades[Grade] = "B", [Parameter Value],  SUM(Data[Marks])),
            "Upper", IF ( Grades[Grade] = "B", 0, [Parameter Value] )
        )
    RETURN
    
    COUNTROWS (
        FILTER (
            ADDCOLUMNS (
                VALUES ( Data[Student] ),
                "XYZ", CALCULATE (
                    [Scores],
                    CALCULATETABLE ( VALUES ( Data[Student] ) ),
                    ALLSELECTED ()
                )
            ),
            COUNTROWS (
                FILTER ( _table, [Scores]>=[Upper]&&[Scores]<=[Lower] )
            ) > 0
        )
    )
    

     

    here is sample pbix file, please try it.

     

    Regards,

    Lin

13 Replies

    • Anonymous's avatar
      Anonymous
      Not applicable

      thank you so much. can you also please show how i can dynamically change my grade threshold ?

      • Ashish_Mathur's avatar
        Ashish_Mathur
        Super User

        Hi,

        Go to Home > Edit Queries > Click on the Grades Table and double click on Source.  Edit the contents of the Table there.

        Hope this helps.

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

    hi Anonymous 

    You could try this as below:

    Step1:

    Use What if parameter to create a dynamic value for "65"

    https://docs.microsoft.com/en-us/power-bi/desktop-what-if

    Step2:

    Create a simple Grades table that only contains Grade column

    Step3:

    Then use adjust the formula that  Ashish_Mathur provided.

    Measure = VAR _table =
         ADDCOLUMNS (
            Grades,
            "Lower", IF ( Grades[Grade] = "B", [Parameter Value],  SUM(Data[Marks])),
            "Upper", IF ( Grades[Grade] = "B", 0, [Parameter Value] )
        )
    RETURN
    
    COUNTROWS (
        FILTER (
            ADDCOLUMNS (
                VALUES ( Data[Student] ),
                "XYZ", CALCULATE (
                    [Scores],
                    CALCULATETABLE ( VALUES ( Data[Student] ) ),
                    ALLSELECTED ()
                )
            ),
            COUNTROWS (
                FILTER ( _table, [Scores]>=[Upper]&&[Scores]<=[Lower] )
            ) > 0
        )
    )
    

     

    here is sample pbix file, please try it.

     

    Regards,

    Lin

  • kentyler's avatar
    kentyler
    Solution Sage

    Consider having 3 technical tables: each holding a range of possible marks for a grade level.

    The table for A might hold 50,55,60,65,70,75,80, 85,90

    You base a slicer on that table.

    the user uses the slicer to select the value they want for "A"

    and your DAX code references the technical table to get the value to use to calculate who got an A

    same for B and C

    • kentyler's avatar
      kentyler
      Solution Sage

      Let me know if you need help writing the DAX code to figure out what grade users get depending on their score.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Power BI is mainly a visualization tool, not for lots of data entry via parameters. For an education institution, the grades for all students should be in another database system or at least an Excel document. The grades then typically get pulled into Power BI as a data source rather than parameters.

     

    To answer your question, though, you can accomplish your goal in a couple of ways.

     

    1. Instead of your range table, create a table with one row per score and mark. For example, if you have scores from 0-100, you would have 101 records, with Scores 0-50 being Mark = A, 51-60 being Mark = B, and so on. This provides the simplest matching for your grades. Now you have a one-to-one match between a record in your Student Marks table and the score that is entered or pulled from the other data source.

     

    2. Alternatively, changing Student Marks to have ranges simplifies the coding. So, instead of the pair { A, 50 } in your table, you change this to { A, 50, 59 }, and instead of { B, 60 } you have { B, 60, 69 }. Having the full range with begin and end values makes your calculations easier. Let's assume that you also have a Student Score table. This lists the Student Name and a Score that they received. Now, you can add a Calculated Column to that Student Score table with the following DAX:

     

    Student Mark =
    CALCULATE (
        MAX ( 'Student Marks'[Mark] ),
        FILTER (
            ALL ( 'Student Marks' ),
            'Student Score'[Score] >= 'Student Marks'[Begin Score]
                && 'Student Score'[Score] <= 'Student Marks'[End Score]
        )
    )

  • Anonymous's avatar
    Anonymous
    Not applicable

    Thank you all for the replies, appreciate it. 

     

    This is a sample data set and the actual data has million records with revenue ranging from 0 to several billion. I need to classify customer dynamically based on their total revenue. I could do this pretty easily in other viz. tools so assuming it could be straightforward in powerBI. All i want to do is add a column whose value is determined by comparing the user provided parameter value to an existing column for every row. Is this possible in powerBI?

    • kentyler's avatar
      kentyler
      Solution Sage

      When you say "user provided Parameter value" in power bi, you mean the user makes a selection from a parameter table. The question is when you say "dynamically", do you also mean the user should be able to set the range of the parameter. Do you want the user to set the range that should be considered an "A", or do you know the range, and just want the user to be able to select that they want to see "A" values.

      • Anonymous's avatar
        Anonymous
        Not applicable

        The user can enter any value for threshold that he wants. I will use a conditional statement to check the column i wish to check against ( revenue) and assign a value for that new column. In the above example, user can enter any number , my code will check if the marks are less or greater than the number he entered and assign a grade. Hope that explains