You’ve picked colors for each product category and want Power BI to follow them. But the table of colors you received only has RGB values, and Power BI takes HEX codes. That leaves you stuck—unless you can convert RGB to HEX inside Power Query. This post shows how to do just that using M code, with a simple binary trick to get the format Power BI needs.
If you’re unsure what RGB and HEX mean or how they relate, read my other article on converting RGB to HEX values. Once you’re up to speed, come back here to see how to handle the conversion inside Power BI.
What is an RGB Value
An RGB value is a way to describe a color using three numbers:
- R for Red
- G for Green
- B for Blue
Each number tells how much of that color to use. The range goes from 0 (none) to 255 (full brightness). So:
- if you see
RGB(255, 0, 0), that means full red, no green, and no blue. You get pure red. - If you see
RGB(0, 0, 0), that’s black—no light at all. - And
RGB(255, 255, 255)is white—full light from all three channels.
So why 0 to 255? Because computers store each of these as one byte, which can hold values from 0 to 255. This format is common in image editing tools, CSS styling, and—unfortunately —Power BI does not use it. Power BI uses HEX values instead.
What is a HEX Value
A HEX value (short for hexadecimal) is another way to describe a color, using six characters instead of three numbers.
It always starts with a #, followed by six digits or letters—like #FF0000 or #4B0082. These six characters are grouped into three pairs:
- The first two are for Red
- The next two are for Green
- The last two are for Blue
Each pair is a hexadecimal number. That just means it uses base 16, not base 10. So instead of digits 0–9, it goes: 0 1 2 3 4 5 6 7 8 9 A B C D E F
Here’s how it maps:
| Decimal | Hex |
|---|---|
| 0 | 00 |
| 255 | FF |
| 128 | 80 |
So #FF0000 means:
- Red = 255 (
FF) - Green = 0 (
00) - Blue = 0 (
00)
Same color as RGB(255, 0, 0)—pure red.
So why use HEX? Because it’s shorter, easy to copy-paste, and most tools that deal with visuals (like HTML or Power BI visuals) expect HEX codes, not RGB numbers.
What Does It Take to Turn an RGB to a HEX Value
To convert an RGB value into a HEX code, you need to take three numbers—Red, Green, and Blue—and represent them in hexadecimal form. There are two common ways to do this:
- Convert each number (0–255) separately to a two-digit hex string
- Or, pack all three into a single binary value, and convert that to hex in one go
We’ll use the second method in our code. It’s shorter, faster, and avoids the need to pad individual values. Here’s how it works, step by step:
- Start with an RGB text string
A value like"173, 216, 230"holds the RGB values as text. - Split the string by commas
→ You get a list:{"173", "216", "230"} - Convert each to a number
→ This becomes a list of numbers:{173, 216, 230} - Pack the numbers into binary
→ That gives you a single 3-byte binary value. - Convert the binary to a hex string
→ Power Query returns"ADD8E6" - Add the # in front and uppercase it
→ Final result:#ADD8E6
This approach avoids converting each color separately. It’s clean and efficient—and we’ll show you exactly how to build it in the next section.
Converting RGB values to HEX codes
So how can we convert RGB values to HEX codes using Power Query M? Let’s use below table as a starting point. It is stored in a step called Source.

Each row holds a color, written as text. The goal is to turn each of these into a HEX color code like #FF5733. We’ll use the binary conversion method we explained earlier. To turn these values into HEX we can do the following:
[
// 1) Split into three numbers
parts = Text.Split([RGB], ","),
// 2) Remove possible leading/lagging spaces and turn text into numbers.
nums = List.Transform(parts, each Number.From(Text.Trim(_))),
// 3) Pack into a binary and render as hex
hx = Binary.ToText(Binary.FromList(nums), BinaryEncoding.Hex),
// 4) Uppercase and prefix
result = "#" & Text.Upper(hx)
][result]
What this code does is, it:
- Reads in the list of RGB strings.
- Text.Split splits each RGB string into a list of three numbers.
- List.Transform removes any spaces and turns text values into numbers.
- Binary.FromList packs the numbers into binary.
- Binary.ToText turns the binary as a HEX string.
- Text.Upper: formats the HEX string in uppercase and adds the
#symbol.
The result is a clean table with your original RGB text and the new HEX code side by side—ready to use in Power BI for formatting your visuals.

Try it yourself
You can try the code yourself in the advanced editor using:
let
// Your original input table
RGBcode = #table(
type table [RGB = text],
{
{"255, 87, 51"},
{"51, 161, 255"},
{"168, 224, 99"},
{"255, 51, 168"},
{"107, 91, 149"},
{"255, 195, 0"},
{"0, 128, 128"},
{"199, 0, 57"},
{"75, 0, 130"},
{"46, 46, 46"}
}
),
// Add the HexCode column by calling our helper
#"Added HexCode" = Table.AddColumn(RGBcode, "HexCode", each [
// 1) Split into three numbers
parts = Text.Split( [RGB], ","),
// 2) Remove possible leading/lagging spaces and turn text into numbers.
nums = List.Transform(parts, each Number.From(Text.Trim(_))),
// 3) Pack into a binary and render as hex
hx = Binary.ToText(Binary.FromList(nums), BinaryEncoding.Hex),
// 4) Uppercase and prefix
result = "#" & Text.Upper(hx)
][result],
type text )
in
#"Added HexCode"
Conclusion
If you’ve ever been stuck trying to color your Power BI visuals with RGB values, you now have a solid workaround. You now know exactly how to turn RGB into HEX using Power Query M. The post showed what RGB and HEX values are, how they relate, and how to use M’s Binary functions to make the switch.
If you want to achieve the reverse, you can read my other article on converting RGB to HEX values. Knowing both methods allows you to work with either HEX or RGB values.
Recommended Reading…
Power Query Foundations
Beginners Guide to Power Query
List Functions (200+ Examples)
Text Functions (150+ Examples)
Creating Tables (40+ Examples)
Generate the Ultimate Date Table
Advanced Topics
Master List.Generate with Easy Examples
Deep Dive into List.Accumulate