Skip to content

Instantly share code, notes, and snippets.

@tonmcg
Last active May 28, 2018 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonmcg/93d56132f8a25b51f83eac751610d8e8 to your computer and use it in GitHub Desktop.
Save tonmcg/93d56132f8a25b51f83eac751610d8e8 to your computer and use it in GitHub Desktop.
M Language Custom Report Themes Functions
let
Theme.PopularPalettes = (palette as text) as text =>
let
Source = Text.Clean(Web.BrowserContents("http://www.color-hex.com/color-palettes/popular.php/")),
colorPalettes = Table.FromList(
List.Generate(
()=>
[
n = 0,
title = Text.BetweenDelimiters(Source,"""clearfix""></div></div>","</a>",n),
paletteNode = Text.BetweenDelimiters(Source,"""palettecolordivcon"">","<div class=""clearfix""",n),
colorList = List.Generate(
()=>
[
t = 0,
color = Text.BetweenDelimiters(paletteNode,"style=""background-color:","""></div>",t)
],
each [color] <> "",
each
[
t = [t] + 1,
color = Text.BetweenDelimiters(paletteNode,"style=""background-color:","""></div>",t)
],
each [color]
)
], // initial
each [title] <> "", // condition
each
[
n = [n] + 1,
title = Text.BetweenDelimiters(Source,"""clearfix""></div></div>","</a>",n),
paletteNode = Text.BetweenDelimiters(Source,"""palettecolordivcon"">","<div class=""clearfix""",n),
colorList = List.Generate(
()=>
[
t = 0,
color = Text.BetweenDelimiters(paletteNode,"style=""background-color:","""></div>",t)
],
each [color] <> "",
each
[
t = [t] + 1,
color = Text.BetweenDelimiters(paletteNode,"style=""background-color:","""></div>",t)
],
each [color]
)
], // next
each [[title],[paletteNode],[colorList]] // selector
),
Splitter.SplitByNothing(), {"palette"}, null, ExtraValues.Error),
ExpandedcolorPalettes = Table.ExpandRecordColumn(colorPalettes, "palette", {"title", "colorList"}, {"name", "dataColors"}),
FilteredPalette = Table.SelectRows(ExpandedcolorPalettes, each ([name] = palette)),
ExpandedColors = Table.ExpandListColumn(FilteredPalette, "dataColors"),
CreatedTable = #table({"name", "dataColors", "background", "foreground", "tableAccent"},{{palette, ExpandedColors[dataColors], "#ffffff", ExpandedColors[dataColors]{1}, ExpandedColors[dataColors]{0}}}),
ConvertedToJSON = Text.FromBinary(Json.FromValue(CreatedTable)),
customReportFormat = Text.TrimStart(Text.TrimEnd(ConvertedToJSON, {"]"}), {"["})
in
customReportFormat,
DefineDocs = [
Documentation.Name = " Theme.PopularPalettes",
Documentation.Description = " Returns a JSON string, formatted for use as a Custom Report Theme in Power BI.",
Documentation.LongDescription = " Returns a properly-formatted JSON string containing the hexadecimal representation of colors that comprise popular palettes on the color-hex.com website. The palette parameter is the name of the chosen palette to return colors for. The JSON string can be used as a Custom Report Theme in Power BI.",
Documentation.Category = " Text.Manipulation",
Documentation.Source = " ",
Documentation.Author = " Tony McGovern, emdata.ai",
Documentation.Examples = {
[
Description = " Return the Twitter color palette as a JSON-formatted string to use as a Report Theme in Power BI.",
Code = " PopularPalettes(""Twitter"")",
Result = "{""name"":""Twitter"",""dataColors"":[""#326ada"",""#d4d8d4"",""#433e90"",""#a19c9c"",""#d2d2d2""],""background"":""#ffffff"",""foreground"":""#d4d8d4"",""tableAccent"":""#326ada""}"
]
}
]
in
Value.ReplaceType(
Theme.PopularPalettes,
Value.ReplaceMetadata(
Value.Type(Theme.PopularPalettes),
DefineDocs
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment