Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
Scenario:
When using line charts to show the changes in different categories, we sometimes need to mark some special data, such as maximum and minimum values. However, in the default line chart in Power BI, we cannot set conditional formatting. Is there any other way?
Sample data:
Division |
Year |
Value |
Division A |
2016 |
84335 |
Division A |
2017 |
89426 |
Division A |
2018 |
93897.3 |
Division A |
2019 |
150235.7 |
Division A |
2020 |
180282.8 |
Division B |
2016 |
87572 |
Division B |
2017 |
92357 |
Division B |
2018 |
129299.8 |
Division B |
2019 |
193949.7 |
Division B |
2020 |
155159.8 |
Division C |
2016 |
74420 |
Division C |
2017 |
141398 |
Division C |
2018 |
155537.8 |
Division C |
2019 |
202199.1 |
Division C |
2020 |
242639 |
Division D |
2016 |
93804 |
Division D |
2017 |
73804 |
Division D |
2018 |
125466.8 |
Division D |
2019 |
188200.2 |
Division D |
2020 |
207020.2 |
Preparation:
We can download and install R for free from many locations, including the Microsoft R Application Network and the CRAN Repository.
Install.packages(“dplyr”)
For the details, please refer to this document: Create Power BI visuals using R.
Operations:
MaxValue =
CALCULATE (
MAX ( 'Table'[Value] ),
FILTER (
ALLSELECTED ( 'Table' ),
'Table'[Division] = MAX ( 'Table'[Division] )
)
)
MaxValueYear = IF ( MAX ( 'Table'[Value] ) = [MaxValue], MAX ( 'Table'[Year] ) )
MinValue =
CALCULATE (
MIN ( 'Table'[Value] ),
FILTER (
ALLSELECTED ( 'Table' ),
'Table'[Division] = MAX ( 'Table'[Division] )
)
)
MinValueYear = IF ( MAX ( 'Table'[Value] ) = [MinValue], MAX ( 'Table'[Year] ) )
# display the dataset
library(gridExtra)
# gridExtra package provides a number of user-level functions to work with "grid" graphics, notably to arrange multiple grid-based plots on a page, and draw tables.
grid.table(dataset)
# display dataset as a table
Note: Duplicated rows will be removed from the data. If you don’t want this, add a column with no duplicate values, such as an Index column.
# filter dataset with only MaxValueYear or MinValueYear
library(dplyr) # dplyr package is a fast, consistent tool for working with data frame like objects, both in memory and out of memory.
dataset_max<-dataset %>% filter(Year==MaxValueYear) # filter dataset with only MaxValueYear
dataset_min<-dataset %>% filter(Year==MinValueYear) # filter dataset with only MinValueYear
# plot a line chart
library(ggplot2) # ggplot2 package is used to create Elegant Data Visualisations Using the Grammar of Graphics
ggplot(data=dataset,aes(y=Value, x=Year, colour=Division))+geom_line(stat="identity") + # Create a line chart
geom_point(data=dataset_max, aes(x=MaxValueYear,y=Value),size = 5) + # add MaxValue points
geom_text(data=dataset_max, aes(label=Value),hjust=0, vjust=0, color="black")+ # add MaxValue label
geom_point(data=dataset_min, aes(x=MinValueYear, y=Value), size = 5)+ # add MinValue points
geom_text(data=dataset_min, aes(label=Value), hjust=0, vjust=0, color="black") # add MinValue label
Note: “hjust” and “vjust” are used to modify text alignment. These can either be a number between 0 (right/bottom) and 1 (top/left) or a character ("left", "middle", "right", "bottom", "center", "top"). There are two special alignments: "inward" and "outward". Inward always aligns text towards the center, and outward aligns it away from the center.
Now, let us test it.
It works great. Just try it!
Summary:
R has advantages in drawing static graphics and can produce publication-quality graphs. When we cannot achieve our needs in the default visualizations of Power BI, try to create an R visual, which will surprise us.
Author: Icey Zhang
Reviewer: Ula Huang, Kerry Wang
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.