Traffic lights in Power Bi which work also for totals

这一生的挚爱 提交于 2019-12-10 16:47:34

问题


How to make traffic light in table visual which works also for totals? Let's say our KPI is some sort of a ratio or a share like GDP per capita by countries, or the difference from the budget by stores. It is interesting to have traffic lights for individual categories but it would be even more interesting to have a traffic light for summary of all categories.

I followed and example shown here: The idea is based on adding a DAX measure:

TrafficLight = UNICHAR(11044)

And then we set up conditional formatting for traffic lights based on other column or measure. But the conditional formatting seems not to affect totals. How to have a traffic light which also shows red, yellow, green color for totals?

Edit. Seems to be a dream feature you can vote for. Please do vote for it!

https://ideas.powerbi.com/forums/265200-power-bi-ideas/suggestions/17401381-conditional-formatting-for-total-and-subtotals-in


回答1:


You're halfway there with UNICHAR(). The next step is to give up on the conditional formatting in Power BI. Here are three Unicode characters that you can use for traffic lights. I sourced them from Emojipedia. These three are "red circle", "yellow circle", and "green circle".

RedLight = UNICHAR ( 128308 )

YellowLight = UNICHAR ( 128993 )

GreenLight = UNICHAR ( 128994 )

These look like this:

Now that you have measures returning the right colors, you can reference these in another measure that has your range logic. Here's a sample. I've used your measure names. My image is from a demo workbook with different names.

Sales Amount KPI = 
VAR SalesAmount = [SalesAmount]
RETURN
SWITCH (
    TRUE (),
    ISBLANK ( SalesAmount ), BLANK (),
    SalesAmount < 50, [GreenLight],
    SalesAmount < 150, [YellowLight],
    [RedLight]
)

This leverages SWITCH, wherein we do something only slightly clever. SWITCH tests its first argument for equality with subsequent arguments. By testing against TRUE (), we can have arbitrary boolean expressions. The value following the matching expression is returned.

The first test, ISBLANK is to guard against displaying this KPI measure for all dimension values. It will only return a value for dimension attributes that have data for the base measure.

Then it's just a series of inequality tests to define my KPI boundaries, as you would do in conditional formatting.

You can go as complex as you want. Here's what it looks like:

Since this is just a regular measure returning a regular text value, you can use this anywhere that displays text including cards, multicards, tooltips, tables, and matrices. There's nothing special about the grand total level for this measure.




回答2:


Traffic lights for totals cannot be done by standard means, but there is a workaround. You may achieve something like this:

The idea is based on the following points:

  1. Have a dictionary for category and add to it value "Total (avg)" or name it according to thy wish.
  2. Uncheck displaying totals in the format pane of the table visual.
  3. Add DAX measure which returns different calculation for ordinary labels and different for "Total (avg)". Use here SWITCH(TRUE()... See examples at the bottom.
  4. For convenience add SortColumn to your category dictionary. So that your "Total (avg)" will always be at the bottom of the table visual, regardless as you name your total.
  5. Unfortunately there is no way to set up bold fonts in conditional formatting. You can only play with background and fonts. Do conditional formatting based on rules applied to SortColumn. I grayed out background of "Total (avg)", and I set up fonts to black color. You have to set up the same conditional formatting for every column in a table.
  6. In format of table visual set up a slightly grey font for ordinary mortal values. It is done to get more contrast with black fonts for totals set up in previous point.

Here are DAX measures I used: Measure:

KPI = SWITCH(TRUE(),
MAX(DimCountry[SortColumn]) = 999, [GDP per capita Total],
[GDP per capita]
)

And measure for total:

GDP per capita Total = CALCULATE([GDP per capita], ALL(FactTable))

For those of you who have read to this point, here is pbix file for download.

TrafficLightsForTotals.pbix



来源:https://stackoverflow.com/questions/57202628/traffic-lights-in-power-bi-which-work-also-for-totals

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!