Forum Discussion

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

Hot to apply Conditional Formatting to Calculation Groups and Calculation items

Hi,

The following report has been created with a matrix visual using Two Calculation Groups namely : "TimeIntelligence" and "Metric".

The calculation items are visible in the above screen shot. Now, for "CY 2008", Calculation item "YOY" shows both positive and negative values for Sales Amt, Sales Qty, Total Cost and Margin Pct.

 

However, the requirement is to display all the positive values including Decimal and Percentages in Green and Negative values in Red font color. 

Pls let us know how the same can be done in Power BI Desktop using Conditional formatting / DAX. 

For farther clarity the definition of Calculation Items are shown below:

 

01. Calculation Item : TimeIntelligence

CY = SelectedMeasure

PY =
    CALCULATE(
        SELECTEDMEASURE(),
        SAMEPERIODLASTYEAR(
            'Date'[Date]))
YOY Pct =

YOY = 

AVG =

 

However, calculation items of "Metric" i.e. Sales Amt, Sales Qty, TotalCost, MarginPct are all measures.

Pls let us know how the Positive and Negative values can be formatted with Green and Red font colors ?

Regards,

Somnath

 

 

 

  • Hi somnath6309 ,

    We wanted to kindly follow up to check if the solution provided for the issue worked? or Let us know if you need any further assistance?
    If our response addressed, please mark it as Accept as solution and click Yes if you found it helpful.

     

    Regards,
    Chaithra.



9 Replies

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

      Hi Sam,

      Many many thanks for your help.  I have applied the rules as shown above and got the desired result.

      However, I had created a measure named VALUE which equal to BLANK() and used the same in Matrix Value filed. When nothing is selected from the slicer Matric, it shows a blank table. Pls see below screen shot:

       

       

      The Conditional formatting rules applied to the cell elements of the matrix, are actually getting applied to the measure VALUE and the formatting is getting applied to all the values. Pls vide the screen shot for reference:

       

       

      However, this is to be mentioned that Sales Amount, Sales Qty, Total Cost cannot be negative for CY, PY as shown below. Mainly, YOY Pct and YOY can be both positive or negative. 

      Can you pls let me know whether there is any process / trick by which I can display amounts in CY and PY in Black ( regular color) and YOY Pct and YOY, both, in Red ( for negative value ) and Green ( for positive values) ?

      Thanks. 

      Regards,

      Somnath

       

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

        Hi somnath6309 

        You need to create a measure that defines which to change colour:

        Formatted font measure =
         IF(
            Not(SELECTEDVALUE('Calculation group'[Calculation group column])
            IN {"YTD", "Time Calculations"}) --Leave these two black
            ,[Sum Qty]
            ,BLANK()
         )
         
        Then use this as your conditional format field:

        Enjoy!

         

  • Hi,

    Thanks for the reply. I had applied the tric in my report. Howver, the same is not producing the desired result. Pls find below the screen shots that shows the "Columns", "Values" of the Matrix. The code of the measure FormatFont is also shown below. The Martix does not use only single meaure, it uses four measures that are listed as calculation items under "CalculationGroup2" column name : "Matrix". 

    My Report has three Calculation Groups :

    01. Metric ( Group Name) that includes Calculation items ( all these are measures) :

       a. Sales Amount 

       b. Sales Qty

       c. TotalCost

       d. Margin Pct

     

    02. Time Intelligence ( Group Name) that includes calculation Items: 

         a. CY

         b. PY

         c. YOY Pct

        d. YOY

     

    03. Average that includes calculation item : AVG

     

     

    Now let me put the requirement precisely :

    Sales Amount, Sales Quantity and Total Cost cannot be negative for CY ( Current Year ) and PY ( Previous year) and hence those figures are to be shown in BLACK. However, the aforesaid items can be both Positive and Negative for YOY ( Year on Year) and YOY Pct and hence the values should appear Green and Red respectively. 

    However, Margin Pct can be both positive and Negative for CY, PY, YOY & YOY Pct.

     

    I tried to use the trick as shown above but the following result is appearing:

    Note: in the visual, CY and PY of Sales Amt should be Black and YOY and YOY PCT should be red but the same is not appearing. The conditional formatting rule applied to Cell Elements Font Color is as follows:

     

    SQLBI video tutorials available in Youtube named "Using Field Parameters and Calculation Groups in Conditional Formatting" shows some tricks (attached below) but the same is requirement specific and some changes are to be made therein to make it suitable for my particular requirement. However, I do not have achieved that much of skill till date.

    Hence, requesting for help so that the specific purpose can be accomplished.

    Regards,

    Somnath

    Using Field Parameters and Calculation Groups in Conditional Formatting 

     

     

  • Hi, Sam,

    Thanks for your reply and support. 

    I used the process as shown above. In the below matrix visual I put Sales Amount measure in values area, in row area I put Calendar Year and Months i.e. Calendar Hierarchy. I created Time Calculations  Calculation group having column name TimeIntelligence which has calculation items like CY, PY, YOY, YOY Pct.  Following is the screen shot for reference. The precedence is 4 (highest) 

     

    Now my goal is to show CY and PY Vales of Sales in BLACK (regular color) as the same can not be less than zero. However, YOY and YOY Pct are to shown in Red and Green for Negative and Positive values respectively. I used the following measure :

     

    and used the same to do conditional formatting like below :

     

    and every thing is working fine as desired :

    However, I had created anothere calculation Group named Calculation group 2 having column name Metric with calculation items Sales Amount, Sales Qty, Total Cost  and MarginPct as shown below :

     

    all the calculation items are measures :

    I created a slicer and put the 'Calculation Group2'[Metirc] to the Field area of the Slicer :

     

    Now the user can choose the items to be displayed in Matrix value area. However, when the "Sales Amount " is selected from the slicer the figures in CY and PY column got Green color. Obviously I put some conditional formatting as shown below screen shot:

    the measure named "FormatSales" is as follows:

    however, the requirement is still the same as previous: 

    when Sales Amt , Sales Qty, Total Cost are selected by the user from the slicer, figures in CY and PY column should have regular black color, however, values in YOY and YOY Pct columns should be displayed in Red ( for Negative value) or Green ( Positive value). The report should look like the following :

     

    Please let me know what needs to be done to get the above result.

    Thanks and Regards,

    Somnath6309

     

     

  • v-echaithra's avatar
    v-echaithra
    Icon for Community Support rankCommunity Support

    Hi somnath6309 ,

    Thank you for reaching out Microsoft Community.

    Create a new measure that returns the color based on the result of SELECTEDMEASURE(),
    You can use SELECTEDMEASURE() in a Calculation Group context because this measure will evaluate dynamically across calculation items.

    Apply Conditional Formatting in Matrix

    Click on the Matrix visual in Power BI.
    In the Values area, click the dropdown arrow next to one of your metrics (like Sales Amt).
    Choose Conditional Formatting > Font color.
    In the dialog: Select "Format by" > "Field value"
    Choose the measure you created: FontColor Format

    Repeat this for each measure in the "Metric" calculation group if needed.

    If this post helps, please give us Kudos and consider marking it Accept as solution to assist other members in finding it more easily.

    Regards,
    Chaithra




  • v-echaithra's avatar
    v-echaithra
    Icon for Community Support rankCommunity Support

    Hi somnath6309 ,

    We wanted to kindly follow up to check if the solution provided for the issue worked? or Let us know if you need any further assistance?
    If our response addressed, please mark it as Accept as solution and click Yes if you found it helpful.

     

    Regards,
    Chaithra.

  • v-echaithra's avatar
    v-echaithra
    Icon for Community Support rankCommunity Support

    Hi somnath6309 ,

    As we haven’t heard back from you, we wanted to kindly follow up to check if the solution provided for the issue worked? or Let us know if you need any further assistance?
    If our response addressed, please mark it as Accept as solution and click Yes if you found it helpful.

     

    Regards,
    Chaithra E

  • v-echaithra's avatar
    v-echaithra
    Icon for Community Support rankCommunity Support

    Hi somnath6309 ,

    We wanted to kindly follow up to check if the solution provided for the issue worked? or Let us know if you need any further assistance?
    If our response addressed, please mark it as Accept as solution and click Yes if you found it helpful.

     

    Regards,
    Chaithra.