Power BI Custom Label Trick (Dynamic Selection)
Syed Ahmed Ali ???? ????
Data Analyst @ Majid Al Futtaim | Data Analyst, BI Developer | Power BI, SQL, Python
In the realm of data visualization, the utilization of custom labels has unlocked a myriad of possibilities. However, a common challenge arises when multiple information is added to labels, resulting in excessive noise that hampers the viewer's ability to concentrate on the chart's key insights.
To address this issue, I have developed a custom label measure that empowers users to selectively display desired labels on their charts. In this article, we will explore the key features and potential improvements of this solution.
First, I created a DAX table with the following code:
__labels = {
? ? ("Actual", 1),
? ? ("MoM", 2),
? ? ("MoM|Icon", 3),
? ? ("Min|Max|Last", 4),
? ? ("No Labels", 5)
}
Then, I created two more measures, one for Month over Month calculation, and second for Min|Max|Last
onth over Month is simply created with quick measure
Total Complaints MoM% =
VAR __PREV_MONTH =
? ? CALCULATE(
? ? ? ? [Total Complaints],
? ? ? ? DATEADD('Calendar'[Date], -1, MONTH)
? ? )
RETURN
? ? DIVIDE([Total Complaints] - __PREV_MONTH, __PREV_MONTH)
As for Min|Max|Last, here is the DAX code for it
领英推荐
Total Complaints | Min Max Last =
// use all the selected months in the filter, and get the month with max number of complaints
VAR maxC =
? ? MAXX(
? ? ? ? ALLSELECTED(
? ? ? ? ? ? 'Calendar'[Month],'Calendar'[Month Number],
? ? ? ? ? ? 'Calendar'[Year]
? ? ? ? ),
? ? ? ? [Total Complaints]
? ? )
// use all the selected months in the filter, and get the month with min number of complaints
VAR minC = ?
? ? MINX(
? ? ? ? ALLSELECTED(
? ? ? ? ? ? 'Calendar'[Month],'Calendar'[Month Number],
? ? ? ? ? ? 'Calendar'[Year]
? ? ? ? ),
? ? ? ? [Total Complaints]
? ? )
VAR LastMonthC =
? ? // give the last date in the current filter context
? ? VAR MaxDate =
? ? ? ? MAXX(
? ? ? ? ? ? ALLSELECTED('Calendar'),
? ? ? ? ? ? 'Calendar'[Date]
? ? ? ? )
? ? // converting the last date into year month format
? ? VAR YearMonth = FORMAT(MaxDate,"MMM YYYY")
? ? // get the complaints for the last month in the current filter context
? ? VAR Result =
? ? ? ? CALCULATE(
? ? ? ? ? ? [Total Complaints],
? ? ? ? ? ? 'Calendar'[Year Month] = YearMonth
? ? ? ? )
? ? Return
? ? ? ? Result
VAR Result =
? ? if ([Total Complaints] in {minC,maxC,LastMonthC}, [Total Complaints])
Return
? ? Result
Now, all that is left is to create a custom label measure, which is simply a switch statement.
Custom Label =
VAR ActualVal = FORMAT([Total Complaints],"#,##0")
VAR MoM =" "& ?FORMAT([Total Complaints MoM%],"+0.0%;-0.0%")
VAR MoMIcon =" "& ?FORMAT([Total Complaints MoM%],"+0.0% ▲;-0.0% ▼")
VAR MinMaxLast = " "& FORMAT([Total Complaints | Min Max Last], "#,##0")
VAR NoLabel = " "
VAR Result =
? ? SWITCH(
? ? ? ? SELECTEDVALUE(__labels[Labels],"Actual"),
? ? ? ? "Actual",ActualVal,
? ? ? ? "MoM", MoM,
? ? ? ? "MoM|Icon", MoMIcon,
? ? ? ? "Min|Max|Last",MinMaxLast,
? ? ? ? "No Labels", NoLabel
? ? )
return
? ? Result
To implement the custom label functionality, we can create slicers for the Labels column from the previously created __labels table. These slicers will allow users to select the desired labels to be displayed on the line chart.
Next, we can incorporate the custom label measure into the chart by placing it in the custom label field. However, it's important to note that when using this measure, we need to include " "& in the formula. This addition is crucial to handle scenarios where the value is blank. Without " "& in place, the chart would directly display the value of the actual measure used, which can be misleading. By including " "&, we ensure that a blank space is shown when no label value is available, thus maintaining the chart's clarity.
The same logic can be applied to the "No Label" selection. When this option is chosen, the custom label measure will display a blank space for every month, indicating the absence of any values for that particular label.
By implementing these steps, we create a custom label feature that enhances the chart's readability and flexibility, allowing users to selectively display relevant information while effectively managing empty or missing label values.
Previously, achieving this level of functionality required the use of external tools like tabular editor, but the process was not as seamless as it is with the Custom Label option. This enhanced feature has not only improved the functionality but also made it more robust and streamlined.